문제

I have a wrapper class called Wrapper with its interface IWrapper, when my wrapper class is created in one of my controller I want it to create the Wrapper class with one kind of input parameters and otherwise with some other input parameters.

My code looke likes this:

public Wrapper(string uri)
{
    base.BaseAddress = new Uri(uri);
}

That I want to achieve is that the Uri parameter in my Wrapper class is injected with different values dependent on in which controller it will be injected from.

I'm using AutoFac 2 with WebApi integration.

도움이 되었습니까?

해결책

I solved it by do like this:

builder.RegisterType<Wrapper>().As<IWrapper>().WithParameter(new NamedParameter("uri", "http://test.com")).InstancePerApiRequest();
builder.Register(x => new RestClientWrapper("https://auth.test.com/")).Named<IWrapper>("auth");
builder.Register(x => new AuthController(x.ResolveNamed<IRestClientWrapper>("auth")));

You can read more aboit it here: https://code.google.com/p/autofac/wiki/TypedNamedAndKeyedServices

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top