开发者

How to do IServiceLocator constructor injection via config file?

开发者 https://www.devze.com 2023-01-25 23:51 出处:网络
How to inject IServiceLocator to my class constructor? When I tried to do this via my config, described above I got an Exception that it could not to create a RequestHandlersFactory class because uni

How to inject IServiceLocator to my class constructor?

When I tried to do this via my config, described above I got an Exception that it could not to create a RequestHandlersFactory class because unity could't find the constructor with serviceLocator and assemblyName.

I got two interfaces

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;
}

and two classes:

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);
    }

    ...
}

Now I want to create unity config file:

  <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>

My config code:

        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);


Try swapping the order of the constructor parameters in the config file so they line up with the actual parameter list in the class.

0

精彩评论

暂无评论...
验证码 换一张
取 消