嗨,我想在我的asp.net mvc应用程序中使用Autofac,这是我在global.asxc文件中的代码:
protected void Application_Start()
{
....
var builder = new ContainerBuilder();
builder.RegisterControllers(Assembly.GetExecutingAssembly());
IContainer container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}但是当我运行这个项目时,我看到了这个错误:
此模块要求HttpApplication (全局应用程序类)实现IContainerProviderAccessor
出什么问题了?
发布于 2011-05-16 16:28:56
用于asp.net mvc3的autofac的最小global.asax.cs设置可能如下所示:(从代码中删除了RegisterRoutes)。与(来自http://code.google.com/p/autofac/wiki/Mvc3Integration的)早期版本的asp.net mvc不同
HttpApplication类不再需要实现IContainerProviderAccessor接口,如ASP.NET集成文档中所述。所有与实现接口相关的代码都应该从Global.asax.cs文件中删除。
您还需要对Autofac.Integration.Mvc.dll的引用
using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Autofac;
using Autofac.Integration.Mvc;
namespace ApplicationX
{
public class MvcApplication : HttpApplication
{
private static IContainer _container;
/// <summary>
/// Gets the container.
/// </summary>
public IContainer Container
{
get { return _container; }
}
// RegisterRoutes and RegisterGlobalFilters removed ...
/// <summary>
/// Fired when the first resource is requested from the web server and the web application starts
/// </summary>
protected void Application_Start()
{
// Register: create and configure the container
_container = BootstrapContainer();
DependencyResolver.SetResolver(new AutofacDependencyResolver(_container));
// MVC Stuff
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
/// <summary>
/// Fired when the web application ends
/// </summary>
public void Application_End()
{
// Release: remember to dispose of your container when your application is about to shutdown to let it gracefully release all components and clean up after them
_container.Dispose();
}
/// <summary>
/// Bootstrapper is the place where you create and configure your container
/// </summary>
/// <returns>An Autofac container</returns>
private IContainer BootstrapContainer()
{
var builder = new ContainerBuilder();
// You can make property injection available to your MVC views by adding the ViewRegistrationSource to your ContainerBuilder before building the application container.
builder.RegisterSource(new ViewRegistrationSource());
// An example of a module that registers the dependencies for a ServiceLayer of your application
builder.RegisterModule(new ServiceModule());
builder.RegisterControllers(typeof(MvcApplication).Assembly);
return builder.Build();
}
}
}发布于 2013-11-21 01:30:31
我和操作员有同样的问题,但我的解决方案不同。
来自here
删除旧项目
来自
https://stackoverflow.com/questions/6013976
复制相似问题