【问题标题】:Error using Autofac with ASP.NET Web API RC and ASP.NET MVC3将 Autofac 与 ASP.NET Web API RC 和 ASP.NET MVC3 一起使用时出错
【发布时间】:2012-06-28 11:21:11
【问题描述】:

我使用 NuGet 将 ASP.NET Web API RC 添加到我的 MVC3 项目中:

Install-Package AspNetWebApi

然后我配置了它。在Global.asax.cs

// configure regular controllers
var configuration = GlobalConfiguration.Configuration;
var container = Bootstrapper.ConfigureContainer(configuration);
containterProvider = new ContainerProvider(container);
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

// Set the dependency resolver implementation for Web API.
var resolver = new AutofacWebApiDependencyResolver(container);
configuration.DependencyResolver = resolver;

我在Boostrapper.ConfigureContainer(...) 中添加了:

// I register my types as InstancePerLifetimeScope() 
// but I also tried .InstancePerHttpRequest().InstancePerApiRequest()
// to the same end
builder.RegisterType<SomeService>()
    .AsImplementedInterfaces().InstancePerLifetimeScope();
// Register API controllers using assembly scanning.
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
builder.RegisterWebApiFilterProvider(config);

这被描述为herehere

我还使用 NuGet 更新了 AutofacAutofac.WebAutofac.Mvc3 包并安装了 Autofac.WebApi 包。

使用此配置,我尝试运行我的 ApiController 并收到以下错误:

从请求实例的范围中看不到标签匹配 'httpRequest' 的范围。这通常表明注册为 per-HTTP 请求的组件正在被 SingleInstance() 组件(或类似场景)请求。在 Web 集成下,始终从 DependencyResolver.Current 或 ILifetimeScopeProvider.RequestLifetime 请求依赖项,而不是从容器本身.

然后当我阅读 Alex Meyer-Gleaves 的评论时:

我怀疑您正在使用带有 MVC3 的 Web API 包,这是导致问题的原因。在 MVC3 集成中,InstancePerHttpRequest 生命周期范围的标记是“httpRequest”。在 MVC4 Beta 和 Web API Beta 包中,我更改了 InstancePerHttpRequest 和 InstancePerApiRequest 以使用通用标签“AutofacWebRequest”。您可以使用 Autofac.Mvc4 从 NuGet 获取 MVC4 集成包。

source article with comment

所以按照 Alex 的建议,我得到了包 Autofac.Mvc4,但它只适用于 Mvc4,我的项目无法构建。然后我抓取了 Autofac 的源代码来针对 Mvc3 构建Autofac.Mvc4

hg clone https://code.google.com/p/autofac/ --branch "MVC4 beta" C:\my\path

使用此程序集作为我的参考后,ApiController 开始工作,但常规的Controllers 仅适用于单个控制器操作调用。当视图调用 Html.RenderAction(...) 并且当我刷新或导航到另一个控制器操作时,它会因以下错误而崩溃:

从请求实例的范围中看不到标签匹配 'AutofacWebRequest' 的范围。这通常表明注册为 per-HTTP 请求的组件正在被 SingleInstance() 组件(或类似场景)请求。在 Web 集成下,始终从 DependencyResolver.Current 或 ILifetimeScopeProvider.RequestLifetime 请求依赖项,而不是从容器本身.

我认为从针对 Mvc3 的 Autofac.Mvc4 版本 2.6.2.859 的最新源构建可能会有所帮助,但我找不到源。还是这里有其他问题?

【问题讨论】:

    标签: asp.net-mvc-3 asp.net-web-api autofac


    【解决方案1】:

    我发现了问题。我还使用Autofac.Integration.Web 将依赖项注入自定义成员资格和角色提供程序。但是在WebLiftime.cs 中有这样一行:

    public static readonly object Request = "httpRequest";
    

    一旦我将其更改为:

    public static readonly object Request = "AutofacWebRequest";
    

    并使用构建的程序集一切正常,我没有收到任何错误:)

    我相信这个常数值应该与所有项目中的相同 Autofac.Integration.WebAutofac.Integration.Mvc 和 'Autofac.Integration.WebApi for Mvc4 所以这应该是一个错误。

    【讨论】:

    • 感谢您报告问题。对于实际的 MVC 4 和 Web API 版本,这些生命周期范围标记都是相同的。集成的早期版本实际上只针对 MVC 4,因为此时大多数人都会使用它。
    • 我在使用 MVC4 和 autofac.integration.web + autofac.integration.mvc(4) 版本 2.6.2.859 时遇到了同样的错误。我可以在 code.google 看到上述常量仍然不同。在 WebLifitetime.cs 有 'httpRequest' code.google.com/p/autofac/source/browse/src/Source/… ,在 RequestLifetimeScopeProvider 有 'AutofacWebRequest' code.google.com/p/autofac/source/browse/src/Source/… 。问题还是没有改正吗?
    猜你喜欢
    • 2012-07-14
    • 2016-01-09
    • 2011-11-27
    • 2014-10-29
    • 2015-10-10
    • 1970-01-01
    • 2015-04-08
    • 2018-07-20
    • 1970-01-01
    相关资源
    最近更新 更多