jeudi 16 septembre 2021

.NET Core plugin mixed dependency injection (sharing a dependency)

  • I have created a plugin with some services inside it.
  • The plugin library has its very own dependency injection container where all dependencies are registered.

Now, the problem.

I would like the plugin library to share a dependency with the "main" application. So, I designed the plugin boostrapping method so it provides you with an Action<ContainerBuilder> in which you could register extra dependencies for the plugin library (to share a dependency between them).

This is how it looks in a test:

[Fact]
public void Test()
{
    var pluginLocation = "somepath\\bin\\Debug\\net5.0\\Functions.dll";
    var functions = Core.Functions.Load(pluginLocation, containerBuilder => containerBuilder.RegisterType<Something>().As<ISomething>());
            functions.Should().NotBeEmpty();
}

The problem is that, even though ISomething is registered in the same container builder (I'm using AutoFac) it's not found when resolving the functions.

The following exception is thrown:

Autofac.Core.DependencyResolutionException : An exception was thrown while activating Deployer.Functions.Unzip.Unzip. ---- Autofac.Core.DependencyResolutionException : None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'Deployer.Functions.Unzip.Unzip' can be invoked with the available services and parameters: Cannot resolve parameter 'Share.ISomething something' of constructor 'Void .ctor(Deployer.Compression.IZipExtractor, Zafiro.Network.IDownloader, Shared.ISomething, System.IO.Abstractions.IFileSystem)'.

It looks like the ISomething used in the test isn't the same the plugin uses!!

NOTE: ISomething resides inside its own project called "Shared".

How do I fix this? Is it possible to do it?





Aucun commentaire:

Enregistrer un commentaire