Question

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.

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top