我有一个称为包装器的类,它的接口是IWrapper,当我的包装类在我的控制器中创建时,我希望它用一种输入参数创建包装类,或者用其他一些输入参数创建包装类。
我的代码看起来像这样:
public Wrapper(string uri)
{
base.BaseAddress = new Uri(uri);
}
我想要实现的是,我的包装器类中的Uri参数被注入了不同的值,取决于它将从哪个控制器中注入。
我正在使用AutoFac 2和WebApi集成。
发布于 2014-04-16 08:33:13
我就是这样解决的:
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")));
您可以在这里阅读更多的内容:https://code.google.com/p/autofac/wiki/TypedNamedAndKeyedServices
https://stackoverflow.com/questions/23102440
复制相似问题