var resolver = new AutofacWebApiDependencyResolver(container);
configuration.ServiceResolver.SetResolver(resolver);
更新到ASP.NET MVC4(RC)后,我收到以下错误:
' System.Web.Http.HttpConfiguration'不包含的定义 ' ServiceResolver'没有扩展方法' ServiceResolver'验收 类型为#System; Web.Http.HttpConfiguration'的第一个参数。可能 发现(您是否缺少using指令或程序集引用?)
我在阅读完这篇(http://www.asp.net/web-api/overview/extensibility/using-the-web-api-dependency-resolver)后发现这些界面已经改变,但我不确定如何将此更改应用于我如何使用Autofac。
我是否需要等待Autofac的新版本,或者是否有其他方法可以解决这个问题。
答案 0 :(得分:5)
修改强> 正如James Bradt在下面的帖子中提到的,Autofac软件包现在已经更新以解决这个问题,因此未来遇到这个帖子的任何人都应该首先尝试新的软件包:)
基本上,使用新包你只需要在global.asax.cs中执行此操作:
GlobalConfiguration.Configuration.DependencyResolver = new Autofac.Integration.WebApi.AutofacWebApiDependencyResolver(container);
<强> /修改
我刚遇到同样的问题 - 通过创建一个包装现有AutofacDependencyResolver的简单IDependencyResolver实现,我能够在我的情况下解决它。
正如类名所示,我将此视为临时解决方案 - BeginScope和Dispose方法需要一些工作,显然不适合生产环境,但这使我能够继续开发,直到找到合适的解决方案。
因此,有了这些警告,IDependencyResolver实现如下所示:
public class TemporaryDependencyResolver : IDependencyResolver
{
private readonly AutofacDependencyResolver _autofacDependencyResolver;
public TemporaryDependencyResolver(AutofacDependencyResolver autofacDependencyResolver)
{
_autofacDependencyResolver = autofacDependencyResolver;
}
public void Dispose()
{
}
public object GetService(Type serviceType)
{
return _autofacDependencyResolver.GetService(serviceType);
}
public IEnumerable<object> GetServices(Type serviceType)
{
return _autofacDependencyResolver.GetServices(serviceType);
}
public IDependencyScope BeginScope()
{
return this;
}
}
我在Global.asax.cs中设置了这个:
var container = builder.Build();
var resolver = new AutofacDependencyResolver(container);
GlobalConfiguration.Configuration.DependencyResolver = new TemporaryDependencyResolver(resolver);
答案 1 :(得分:4)
AutoFac.WebApi包已更新为(RC) - 版本2.6.2.859
这似乎已针对RC和Beta
之间的依赖关系的变化进行了调整答案 2 :(得分:0)
I tried above solutions but didn't worked for me. Removing and Reinstalling these 2 specific packages solved the issue for me.
Microsoft.AspNet.WebApi.Tracing
Microsoft.AspNet.WebApi.OData