Autofac / MVC4 /的WebAPI(RC)的依赖注入问题,从测试版升级后 [英] Autofac / MVC4 / WebApi (RC) Dependency Injection issue after upgrading from beta

查看:248
本文介绍了Autofac / MVC4 /的WebAPI(RC)的依赖注入问题,从测试版升级后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var resolver = new AutofacWebApiDependencyResolver(container);
configuration.ServiceResolver.SetResolver(resolver);

更新到ASP.NET MVC4(RC)我得到以下错误后:

after updating to ASP.NET MVC4 (RC) I get the following error:

System.Web.Http.HttpConfiguration'不包含一个定义
  ServiceResolver',没有扩展方法'ServiceResolver接受
  类型'System.Web.Http.HttpConfiguration'的第一个参数可能是
  (是否缺少using指令或程序集引用?)

'System.Web.Http.HttpConfiguration' does not contain a definition for 'ServiceResolver' and no extension method 'ServiceResolver' accepting a first argument of type 'System.Web.Http.HttpConfiguration' could be found (are you missing a using directive or an assembly reference?)

我读这(http://www.asp.net/web-api/overview/extensibility/using-the-web-api-dependency-resolver),这些接口已经更改后实现的,但我不知道怎样应用这个变化我如何使用Autofac。

I realize after reading this (http://www.asp.net/web-api/overview/extensibility/using-the-web-api-dependency-resolver) that these interfaces have changed, but I am not sure how to apply this change to how I use Autofac.

我是否需要等待Autofac新版本或有另一种方式,我可以突破这个。

Do i need to wait for a new release from Autofac or is there another way I can get past this.

推荐答案

编辑:
正如詹姆斯布拉特在下面他的职位提到,在Autofac包现已更新为解决这个问题,所以任何人都碰到这个线程即将在未来可能应该先尝试新的软件包:)

As James Bradt mentions in his post below, the Autofac package has now been updated to fix this issue, so anyone coming across this thread in the future should probably try the new package first :)

基本上,新的软件包,你只需要做到这一点在你的global.asax.cs:

Basically, with the new package you just need to do this in your global.asax.cs:

GlobalConfiguration.Configuration.DependencyResolver = new Autofac.Integration.WebApi.AutofacWebApiDependencyResolver(container);

/修改

我刚刚遇到了同样的问题 - 我能够通过创建一个包装现有AutofacDependencyResolver一个简单的IDependencyResolver实施解决它在我的处境

I just came across the same issue - I was able to resolve it in my situation by creating a simple IDependencyResolver implementation that wraps the existing AutofacDependencyResolver.

作为类名所暗示的,我这当作一个临时的解决 - BeginScope和Dispose方法将需要一些工作,显然不适合于生产环境,但是这让我直到一个妥善的解决办法出现了持续的发展。

As the class name suggests, I'm treating this as a temporary resolution - the BeginScope and Dispose methods will need some work and are obviously not suitable for a production environment but this allows me to continue development until a proper solution emerges.

所以,有这些警告,在执行的IDependencyResolver如下:

So, with those caveats, the IDependencyResolver implementation looks like this:

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中设置这样的:

and I set it like this in Global.asax.cs:

var container = builder.Build();
var resolver = new AutofacDependencyResolver(container);
GlobalConfiguration.Configuration.DependencyResolver = new TemporaryDependencyResolver(resolver);

这篇关于Autofac / MVC4 /的WebAPI(RC)的依赖注入问题,从测试版升级后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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