加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

Autofac和ASP.NET Web API ApiController

发布时间:2020-12-15 20:41:42 所属栏目:asp.Net 来源:网络整理
导读:我一直在使用autofac与MVC 3一段时间,并喜欢它.我最近将一个项目升级到MVC 4,除了Web Api ApiController之外,所有内容似乎都很有效.我得到以下例外. An error occurred when trying to create a controller of type 'MyNamespace.Foo.CustomApiController'.
我一直在使用autofac与MVC 3一段时间,并喜欢它.我最近将一个项目升级到MVC 4,除了Web Api ApiController之外,所有内容似乎都很有效.我得到以下例外.
An error occurred when trying to create a controller of type 'MyNamespace.Foo.CustomApiController'. Make sure that the controller has a parameterless public constructor.

这似乎是DI通过autofac的问题.我错过了一些东西,还是在作品中有东西.我知道,MVC4刚刚出来,是一个beta,所以我不期望很多,但想到我可能会缺少一些东西.

解决方法

我已经在NuGet上发布了用于测试版MVC 4和Web API的Autofac集成包.集成将根据控制器请求(MVC控制器或API控制器,取决于集成)创建一个Autofac使用寿命范围.这意味着控制器及其依赖将在每次调用结束时自动处理.这两个软件包可以并排安装在同一个项目中.

MVC 4

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

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

Web API

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

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

链接现在已修复.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读