.Net core generic service registration

Register a generic service in .Net core

public interface IService<T>
{
    async Task DoAction();
}


public class Service<T> : IService<T>
{
    public async Task DoAction()
    {
        .... do action
    }
}


ServiceCollection.AddScoped<typeof(IService<>), typeof(Service<>)>();
ServiceCollection.AddScoped<typeof(ITInterface<>), typeof(TImplement<>)>(typeof(Service<>));
ServiceCollection.AddScoped(typeof(TImplement<>));

Guess you like

Origin blog.csdn.net/Helloantoherday/article/details/115207891