vNext Dependency Injection Generic Interface

2020-02-05 03:32发布

I have created an ASP.Net vNext Web API.

I have successfully used the dependency inection with a simple interface such as this:

services.AddScoped<ILinearRegressionCalculator, LinearRegressionCalculator>();

However, I can't figure out how to do dependency inection with a Generic Interface.

How do I setup dependency injection for this interface:

public interface IMongoConnectionHandler<T> where T : IMongoEntity

1条回答
小情绪 Triste *
2楼-- · 2020-02-05 04:20

Following is an example:

services.TryAdd(ServiceDescriptor.Singleton(typeof(IOptions<>), typeof(OptionsManager<>)));

Source: https://github.com/aspnet/Options/blob/1.0.0-rc1/src/Microsoft.Extensions.OptionsModel/OptionsServiceCollectionExtensions.cs#L23

查看更多
登录 后发表回答