如何通过配置文件进行IServiceLocator构造函数注入?

时间:2010-11-21 17:04:23

标签: dependency-injection enterprise-library unity-container service-locator

如何将IServiceLocator注入我的类构造函数?

当我尝试通过我的配置执行此操作时,如上所述我得到一个异常,它无法创建RequestHandlersFactory类,因为Unity无法找到 serviceLocator assemblyName的构造函数即可。

我有两个接口

public interface IPublicService 
{
    [OperationContract]
    [ServiceKnownType("GetKnownTypes", typeof(KnownTypeProvider))]
    Response Handle(Request request);
}

public interface IRequestHandlersFactory
{
    IRequestHandler GetHandler(Type requestType);
    IRequestHandler GetHandler<T>() 
        where T  : Request;
    IRequestHandler<T, TK> GetHandler<T, TK>()
        where T  : Request
        where TK : Response;
}

和两个班级:

public sealed class PublicService: IPublicService
{
    private readonly IRequestHandlersFactory _requestHandlersFactory;

    public PublicService(IRequestHandlersFactory requestHandlersFactory)
    {
        _requestHandlersFactory = requestHandlersFactory;
    }

    public Response Handle(Request request)
    {
        var handler = _requestHandlersFactory.GetHandler(request.GetType());
        return handler.Handle(request);
    }
}


public sealed class RequestHandlersFactory : IRequestHandlersFactory
{
    private readonly IServiceLocator _serviceLocator;

    private RequestHandlersFactory(IServiceLocator serviceLocator)
    {
        _serviceLocator = serviceLocator;
        ...
    }
    public RequestHandlersFactory(IServiceLocator serviceLocator, String assemblyName) : this(serviceLocator)
    {
        AddHandlersFromAssembly(Assembly.Load(assemblyName));
    }

    public RequestHandlersFactory(IServiceLocator serviceLocator, Assembly assembly) : this(serviceLocator)
    {
        AddHandlersFromAssembly(assembly);
    }

    ...
}

现在我要创建统一配置文件:

  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<alias alias="IPublicService" type="MyAssembly.IPublicService, MyAssembly"/>
<alias alias="PublicService" type="MyAssembly.PublicService, MyAssembly"/>
<alias alias="IRequestHandlersFactory" type="MyAssembly.IRequestHandlersFactory, MyAssembly"/>
<alias alias="RequestHandlersFactory" type="MyAssembly.RequestHandlersFactory, MyAssembly"/>
<container>
  <register type="IPublicService" mapTo="PublicService">
    <lifetime type="singleton"/>
  </register>
  <register type="IRequestHandlersFactory" mapTo="RequestHandlersFactory">
    <lifetime type="singleton"/>
    <constructor>
      <param name="assemblyName">
        <value value="MyAssemblyWithHandlers" />
      </param>
      <param name="serviceLocator" dependencyName="WcfServiceLocator" dependencyType="Microsoft.Practices.ServiceLocation.IServiceLocator, Microsoft.Practices.ServiceLocation"/>
    </constructor>
  </register>
</container>

我的配置代码:

        var container = new UnityContainer();

        //configure container
        var unitySection = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
        var serviceLocator = new UnityServiceLocator(container );
        container.RegisterInstance<IServiceLocator>("WcfServiceLocator", serviceLocator, new ContainerControlledLifetimeManager());
        unitySection.Configure(container);

1 个答案:

答案 0 :(得分:1)

尝试交换配置文件中构造函数参数的顺序,使它们与类中的实际参数列表对齐。