Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for dependency injection via constructor #32

Closed
MrDave1999 opened this issue Mar 18, 2024 · 0 comments · Fixed by #33
Closed

Add support for dependency injection via constructor #32

MrDave1999 opened this issue Mar 18, 2024 · 0 comments · Fixed by #33
Labels
feature New feature or request

Comments

@MrDave1999
Copy link
Owner

MrDave1999 commented Mar 18, 2024

Problem reproduction

Plugin project:

[assembly: Plugin(typeof(TestService))]

namespace MyPluginProject;

public class TestService(ILogger<TestService> logger) : ITestService
{
    public int Print()
    {
        logger.LogInformation("TestService Plugin");
        return 0;
    }
}

Host application

namespace MyHostApplication;

public class PluginStartup
{
    public void ConfigureServices(WebApplicationBuilder builder)
    {
        var envConfiguration = new CPluginEnvConfiguration();
        PluginLoader.Load(envConfiguration);
        var testServices = TypeFinder.FindSubtypesOf<ITestService>();
        foreach (ITestService testService in testServices)
            testService.Print();
    }
}

If the host application is executed, the code will fail with an exception, because the method called FindSubtypesOf uses Activator.CreateInstance and the TestService type is required to have a constructor without parameters in order to create the instance.

Solution proposal

Use the Microsoft.Extensions.DependencyInjection package to create the instances and resolve the dependencies of a service.
This solves the limitation of TypeFinder.FindSubtypesOf.

API proposal

public class PluginStartup
{
    public void ConfigureServices(WebApplicationBuilder builder)
    {
        var envConfiguration = new CPluginEnvConfiguration();
        PluginLoader.Load(envConfiguration);
        builder
          .Services
          .AddSubtypesOf<ITestService>(ServiceLifetime.Transient);
    }

    public void Configure(IApplicationBuilder app)
    {
        var testServices = app
            .Services
            .GetServices<ITestService>();

        foreach (ITestService testService in testServices)
            testService.Print();
    }
}
@MrDave1999 MrDave1999 added the feature New feature or request label Mar 18, 2024
@MrDave1999 MrDave1999 changed the title Add support for dependency injection Add support for dependency injection by constructor Mar 19, 2024
@MrDave1999 MrDave1999 changed the title Add support for dependency injection by constructor Add support for dependency injection via constructor Mar 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant