Autofac和ASP.NET Web API ApiController [英] Autofac and ASP.NET Web API ApiController

查看:297
本文介绍了Autofac和ASP.NET Web API ApiController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用autofac与MVC 3一会儿,喜欢它。我最近升级一个项目,MVC 4,一切似乎除了网页API ApiControllers是工作。我得到了以下异常。

I have been using autofac with MVC 3 for a while and love it. I recently upgraded a project to MVC 4 and everything seems to be working except for Web Api ApiControllers. I am getting the following exception.

An error occurred when trying to create a controller of type 'MyNamespace.Foo.CustomApiController'. Make sure that the controller has a parameterless public constructor.

这在我看来是通过autofac与DI的问题。我缺少的东西或者是有什么在作品中。我知道,MVC4刚出来的时候,是一个测试版,所以我不期望太多,但我能够失去了一些东西。

This seems to me to be an issue with DI via autofac. Am I missing something or is there something in the works. I know, MVC4 just came out and is a beta so I don't expect much but figured I could be missing something.

推荐答案

我已经发布的NuGet Autofac集成包MVC 4和Web API的测试版。该集成将创造每个控制器请求(取决于整合MVC控制器或API控制器)的Autofac寿命范围。这意味着控制器及其依赖将被自动设置在每个呼叫的结束。两种封装可以安装一侧并排在同一个项目。

I have released Autofac integration packages on NuGet for the Beta versions of MVC 4 and Web API. The integrations will create an Autofac lifetime scope per controller request (MVC controller or API controller depending on the integration). This means that the controller and its dependencies will be automatically disposed at the end of each call. Both packages can be installed side-by-side in the same project.

MVC 4

https://nuget.org/packages/Autofac.Mvc4

http://alexmg.com/post/2012/03/09/Autofac-ASPNET-MVC-4-(Beta)-Integration.aspx

网络API

https://nuget.org/packages/Autofac.WebApi/

的http:// alexmg.com/post/2012/03/09/Autofac-ASPNET-Web-API-(Beta)-Integration.aspx

链接,现在固定的。

这篇关于Autofac和ASP.NET Web API ApiController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆