Skip to content

Cannot use middleware dependencies when using custom ModelLoader on AddPredictionEnginePool #4392

@NeoXtreem

Description

@NeoXtreem

If a custom ModelLoader is used for the PredictionEnginePool that has a dependency on a configured service, there is no possibility to add this dependency via AddPredictionEnginePool as it does not offer an overload that accepts an implementation factory, unlike AddSingleton, AddScoped and AddTransient.

This is how it should be expected to happen in Startup:

services.AddPredictionEnginePool<Foo, Bar>(serviceProvider =>
{
    services.AddOptions<PredictionEnginePoolOptions<Foo, Bar>>().Configure(options =>
    {
        options.ModelLoader = new MyModelLoader(serviceProvider.GetService<IMyService>());
    });
    return new PredictionEnginePool<Foo, Bar>();
});

Since this is not possible with the current code, the creation of a model via middleware is therefore very difficult. The only option is to use ServiceProvider.BuildServiceProvider which causes a warning as it results in an additional copy of singleton services being created:

services.AddPredictionEnginePool<Foo, Bar>()
services.AddOptions<PredictionEnginePoolOptions<Foo, Bar>>().Configure(options =>
{
    var serviceProvider = services.BuildServiceProvider();
    options.ModelLoader = new MyModelLoader(serviceProvider.GetService<IMyService>());
});

A new AddPredictionEnginePool overload should be added that accepts an implementation factory delegate to allow middleware services to be accessed via the IServiceProvider.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Priority of the issue for triage purpose: Needs to be fixed at some point.enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions