如何解决Autofac InstancePerHtt prequest [英] How to resolve Autofac InstancePerHttpRequest

查看:209
本文介绍了如何解决Autofac InstancePerHtt prequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经注册这样的成分在我的Global.asax.cs:

I have registered a component like this in my Global.asax.cs:

ContainerBuilder builder = new ContainerBuilder();
builder.RegisterControllers(Assembly.GetExecutingAssembly());

builder.RegisterType<WebWorkContext>().As<IWorkContext>().InstancePerHttpRequest();

IContainer container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

// This is where my error happens, not sure why?
var workContext = container.Resolve<IWorkContext>();

WebWorkContext 类:

public class WebWorkContext : IWorkContext
{
     // Left out other code
}

IWorkContext 接口:

public interface IWorkContext
{
     // Left out other code
}

这是我收到的错误是:

没有一个标签匹配'HTT prequest'的范围是其中要求该实例的范围可见。这通常表示登记为每个HTTP请求的组件是由一个SingleInstance()成分reqested(或类似的情形。)在幅积分总是请求从DependencyResolver.Current或ILifetimeScopeProvider.RequestLifetime依赖关系,不会从容器本身

No scope with a Tag matching 'httpRequest' 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.

我如何得到这个工作?这个原因,我想这样说是因为工作环境处理的东西,如获得当前用户等。

How do I get this to work? This reason why I want it this way is because the work context handles stuff like getting the current customer etc.

一些更多的问题。它是一次登记每聪明/最佳做法?请问是我需要在另一个阶段中添加更多组件的情景?

Some more questions. Is it wise/best practices to register every at once? Will the be scenarios that I will need to add more components at another stage?

推荐答案

这是标有 InstancePerHtt prequest 注册预计将来自特定嵌套一辈子解决即每个HTTP请求中创建和处置范围。

Registrations that are marked with InstancePerHttpRequest are expected to be resolved from a particular nested lifetime scope that is created and disposed during each HTTP request.

如果您添加 IWorkContext 作为构造函数的参数,以您的控制器之一,你会发现一个实例注入。在您的code你正试图从根寿命范围内解决您的服务,而不是嵌套每个请求终生范围。

If you add IWorkContext as a constructor parameter to one of your controllers you will find that an instance is injected. In your code you are attempting to resolve your service from the root lifetime scope and not the nested "per request" lifetime scope.

如果你想测试不运行你的应用程序,您需要创建一个一生范围具有相同标记作为HTTP请求中创建的解析服务。在MVC 3集成的生命周期范围标为HTT prequest。

If you want to test resolving the service without running up your application you will need to create a lifetime scope with the same tag as the one created during the HTTP request. In the MVC 3 integration the lifetime scope is tagged "httpRequest".

using (var httpRequestScope = container.BeginLifetimeScope("httpRequest"))
{
    Assert.That(httpRequestScope.Resolve<IWorkContext>(), Is.Not.Null);
}

我想我会更新MVC整合公开通过API揭露HTT prequest的标记名称,这样的字符串值不必硬codeD。也可以通过自己的 ILifetimeScopeProvider 执行到 AutofacDependencyResolver ,这样你可以在外面控制的寿命范围的创建的ASP.NET运行时。这是在单元测试中非常有用当没有HTTP请求提供。

I think I will update the MVC integration to expose the "httpRequest" tag name publicly through the API so that string values don't need to be hard coded. It is also possible to pass your own ILifetimeScopeProvider implementation to the AutofacDependencyResolver so that you can control the creation of lifetime scopes outside of the ASP.NET runtime. This is useful in unit tests when there is no HTTP request available.

这篇关于如何解决Autofac InstancePerHtt prequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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