Autofac将不同的实现注册到不同的控制器



我有一个ITestService接口TestService1TestServices2实现ITestService。我想要的是对Controller1使用TestService1ontroller2则使用TestService2

我正在使用ASP.NET MVC4。

我试过这个,但没用

builder.RegisterType<Controller1>().WithParameter(ResolvedParameter.ForNamed<ITestService>("TestService1")).InstancePerDependency();
builder.RegisterType<Controller2>().WithParameter(ResolvedParameter.ForNamed<ITestService>("TestService2")).InstancePerDependency();

我也试过这个,没有成功

    builder.RegisterType<TestService1>().Named<ITestService>("TestService1").As<ICategoryService>();
    builder.RegisterType<TestService2>().Named<ICategoryService>("TestService2").As<ICategoryService>();
    builder.RegisterType<Nop.Web.Controllers.Controller1>().WithParameter(ResolvedParameter.ForNamed<ITestService>("TestService1"));
    builder.RegisterType<Nop.Admin.Controllers.Controller2>().WithParameter(ResolvedParameter.ForNamed<ITestService>("TestService2"));

您是否分配了名称"TestService1"one_answers"TestService2"?例如

builder.RegisterType<TestService1>().As<ITestService1>().Named("TestService1");
builder.RegisterType<TestService2>().As<ITestService2>().Named("TestService2");

最新更新