JBe*_*ton 5 autofac asp.net-web-api2
我刚刚将我的所有Autofac软件包更新到最新版本以获得对web api 2的支持.在我的api控制器中,我设置了一个构造函数,询问服务层类的实例,这与我使用autofac的所有mvc的方式相同控制器,它工作正常.
在app启动时执行的ioc配置中,我已经注册了web api控制器.
builder.RegisterType<UsersApiController>().InstancePerApiRequest();
Run Code Online (Sandbox Code Playgroud)
我也试过了
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
Run Code Online (Sandbox Code Playgroud)
但autofac不能与我的api控制器一起使用,要求注入服务类的构造函数甚至不会被执行.如果我在api控制器上不包含默认参数less constructor,我也会收到错误.
web api 2和auto fac是否存在已知问题,或者我遗漏了什么?
小智 -1
这个博客对我有用
http://danielhindrikes.se/cloud/azure/azure-mobile-services-and-autofac/
所以我的代码现在看起来是
public static void Register(){
ConfigOptions options = new ConfigOptions();
//Prepare Dependency Injection Container
var configBuilder = new ConfigBuilder(options, DependencyInjectionConfig);
// Use this class to set WebAPI configuration options
HttpConfiguration config = ServiceConfig.Initialize(configBuilder);
}
private static void DependencyInjectionConfig(ContainerBuilder containerBuilder)
{
containerBuilder.Register(component => new EFProductRepository()).As<IProductRepository>().SingleInstance();
containerBuilder.RegisterType<ProductsManager>().As<IProductsManager>();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4592 次 |
| 最近记录: |