使用ctor注入和ServiceProvider创建实例

时间:2017-08-18 11:43:49

标签: c# .net-core inversion-of-control

我知道有IServiceCollection接口可以注册我的服务,IServiceProvider可以实例化服务。

如何根据使用已注册服务的指定Type实例化一个类?

class MyClass
{
    public MyClass(ISomeService someService) { }
}

var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton<ISomeService, SomeService>()

MyClass instance = CreateInstance(typeof(MyClass));

object CreateIntance(Type type)
{
   ???
}

例如,ASP.NET Core如何创建控制器实例?

我已经初步实现了激活器但是在.NET Core中已经没有这样的东西吗?

private static object CreateInstance(Type type, IServiceProvider serviceProvider)
{
    var ctor = type.GetConstructors()
        .Where(c => c.IsPublic)
        .OrderByDescending(c => c.GetParameters().Length)
        .FirstOrDefault()
        ?? throw new InvalidOperationException($"No suitable contructor found on type '{type}'");

    var injectionServices = ctor.GetParameters()
        .Select(p => serviceProvider.GetRequiredService(p.ParameterType))
        .ToArray();

    return ctor.Invoke(injectionServices);
}

}

编辑:这是我的情景。我重构了一些实现此接口的遗留代码。

public interface IEventDispatcher
{
    void RegisterHandler(Type handler);
    void Dispatch(DomainEvent @event);
}

在Dispatch方法实现中,我创建了处理程序的实例:

public class InMemoryBus : IEventDispatcher
{
    private readonly List<Type> _handlers = new List<Type>();
    private readonly Func<Type, object> activator;

    /// <param name="activator">Used to create instance of message handlers</param>
    public InMemoryBus(Func<Type, object> activator)
    {
        this.activator = activator;
    }

    public void Dispatch(DomainEvent @event)
    {
        Type messageType = message.GetType();
        var openInterface = typeof(IHandleMessages<>);
        var closedInterface = openInterface.MakeGenericType(messageType);

        var handlersToNotify = from h in _handlers
                               where closedInterface.GetTypeInfo().IsAssignableFrom(h.GetTypeInfo())
                               select h;
        foreach (Type h in _handlersToNotify)
        {
            //this is the tricky part
            var handlerInstance = activator(h);
            closedInterface.GetTypeInfo()
                .GetMethod(nameof(IHandleMessages<T>.Handle))
                .Invoke(handlerInstance, new[] { message });
        }
    }

    public void RegisterHandler(Type type) => _handlers.Add(type);
}

2 个答案:

答案 0 :(得分:6)

鉴于

public class MyClass {
    public MyClass(ISomeService someService) { 
        //...
    }
}

您需要构建整个对象图,以便提供者知道它可以创建什么

var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton<ISomeService, SomeService>()
serviceCollection.AddTransient<MyClass>();

IServiceProvider provider = serviceCollection.BuildServiceProvider();

MyClass instance = provider.GetService<MyClass>();

当调用provider.GetService<MyClass>()时,提供程序将初始化MyCLass,解析该进程中的所有依赖项。

参考Introduction to Dependency Injection in ASP.NET Core

答案 1 :(得分:3)

https://stackoverflow.com/a/45756192/45091是做事的正确方法。

  

例如,ASP.NET Core如何创建控制器实例?

如果您不想注册服务,可以使用我们所称的&#34;键入激活&#34;。这是一种名为ActivatorUtilities的类型,它具有您想要的辅助方法。您调用ActivatorUtilities.CreateInstance(serviceProvider, type),它将使用指定的服务提供商激活该类型。