In a previous post on application parts I created the basic library file to drop into a plugin directory which was then dynamically loaded up through reflection to be consumed in the host application. In this post I will cover what is needed if for example you have services that need injecting via .Net Core Dependency Injection mechanisms.

Add custom attributes to plugin services

As we want these classes and interfaces to be discoverable by our host application, the best way to do this is by decorating them with custom attributes. Custom attributes are simply declared in a class that inherits from System.Attribute

This takes in a name for the plugin service so that it can be differentiated from other plugins.

Then decorate both your service classes and interfaces with this new attribute.

Search the assembly for the custom attribute

So now in the host we can load up the assembly and instead of having to deal with all the other types that are not anything to do with our service, we can just search for our new attribute type.

We want to find all service classes and then find the interfaces that they implement. Our design is very simple and our service class only implement the one interface.

Here we have the class as a Type object and with this we can get the interfaces as a collection; we only need the first item which in our simple case would be IMyService.

As we cannot return a collection from the ConfigureApplicationParts() method, we simply add both the class and interface to a collection defined outside it.

Now use this collection and inject each class and interface into the application as transients.

Happy coding


HTTP Endpoint Monitoring for both Windows and Android
WebUp App