ASP .NET 4的网页API RC + Autofac手动解决 [英] ASP .Net 4 Web Api RC + Autofac manual resolving

查看:214
本文介绍了ASP .NET 4的网页API RC + Autofac手动解决的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Web API方法里面depedency解析器。这个工作很好,正常工作与经典的ASP.NET MVC与DepedencyResolver.GetService()

但我不能得到这个WepApi方法内工作。

我的登记注册所有实例作为InstancePerApiRequest,如果我添加的所有类型的任何我有registred在我在我的WebAPiConroller大公的构造函数里面引导程序时调用它们注入罚款,但现在不是了。

这样在我说GET方法

  VAR userRepository =(IUserRepositoryu)GlobalConfiguration
       .Configuration.DependencyResolver.GetService(typeof运算(IUserRepository));

我得到了无用武之地的WebRequest错误。奇怪的是,它在测试工作得很好,他们这一切更改为GlobalConfiguration之前。

我的问题是,如何才能激活我的Autofac注册组件在我的WebAPI的寿命范围为前

我的错误:

不带标签匹配'AutofacWebRequest'的范围是从其中要求该实例的范围是可见的。这通常表明组件注册为每个HTTP请求被reqested由SingleInstance()组件(或类似的场景。)在网络整合总是要求依赖从DependencyResolver.Current或ILifetimeScopeProvider.RequestLifetime,永远不会从容器本身。

  VAR解析器=新AutofacWebApiDependencyResolver(容器);
 configuration.DependencyResolver =解析器;


解决方案

在网络API的全局依赖解析器用于访问全局实例。每个请求的服务来自依赖作用域的Web API创建来处理请求。我不知道有是Web API的任何方式来访问当前的依赖性范围 - 这将是有趣的,知道

在这里最好的办法是只使用依赖注入,而不是直接调用解析器这样。您code的哪一部分需要作出这一呼吁?

I'm trying to use depedency resolver inside a Web Api method. This worked fine and works fine with classic ASP.NET MVC with the DepedencyResolver.GetService()

But I can't get this to work inside WepApi methods.

My registration register all instances as InstancePerApiRequest and if I add any of all the types I have registred in my bootstrapper on the constructor of my WebAPiConroller thay inject fine but not anymore when calling them inside.

Like this in my say Get Method

var userRepository = (IUserRepositoryu)GlobalConfiguration
       .Configuration.DependencyResolver.GetService(typeof(IUserRepository));

I got the no scope WebRequest error. The strange thing is that it worked fine in Beta before they change it all to the GlobalConfiguration.

So my question is, how can I activate my Autofac registered assemblies in the lifetime scope of my webAPi as before?

My error:

"No scope with a Tag matching 'AutofacWebRequest' is visible from the scope in which the instance was requested. This generally indicates that a component registered as per-HTTP request is being reqested by a SingleInstance() component (or a similar scenario.) Under the web integration always request dependencies from the DependencyResolver.Current or ILifetimeScopeProvider.RequestLifetime, never from the container itself."

 var resolver = new AutofacWebApiDependencyResolver(container);
 configuration.DependencyResolver = resolver;

解决方案

In Web API the global dependency resolver is used to access global instances. Per-request services come from a dependency scope that Web API creates to handle the request. I'm not sure that there is any way in Web API to access the current dependency scope - it would be interesting to know.

The best option here is to just use dependency injection rather than calling the resolver directly like this. Which part of your code needs to make this call?

这篇关于ASP .NET 4的网页API RC + Autofac手动解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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