.NET Core 2.0 Autofac注册类InstancePerRequest不起作用 [英] .NET Core 2.0 Autofac Register Class InstancePerRequest doesn't work

查看:469
本文介绍了.NET Core 2.0 Autofac注册类InstancePerRequest不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的界面

public interface IMatchServices
{
    string MatchList();
}

在这里上课

public class MatchServices : IMatchServices
{
    public string MatchList() {

        return "test";
    }
}

控制器

public class SearchController : Controller
{
    private readonly IMatchServices matchservices;

    public SearchController(IMatchServices matchservices)
    {
        this.matchservices = matchservices;
    }

    [Route("matchlist")]
    public string MatchList() {

        return matchservices.MatchList();
    }
}

ConfigureService

public IServiceProvider ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
    ContainerBuilder builder = new ContainerBuilder();
    builder.RegisterType<MatchServices>().As<IMatchServices>().SingleInstance();
    builder.Populate(services);
    var container = builder.Build();
    return container.Resolve<IServiceProvider>();
}

最后是错误(当我向InstancePerRequest注册时):

And Finally The Error (When I register with InstancePerRequest):

Autofac.Core.DependencyResolutionException:无法解析类型 'xxx.Services.MatchServices',因为它所属的生存期范围 找不到.此服务提供以下服务 登记: -xxx.Interfaces.IMatchServices

Autofac.Core.DependencyResolutionException: Unable to resolve the type 'xxx.Services.MatchServices' because the lifetime scope it belongs in can't be located. The following services are exposed by this registration: - xxx.Interfaces.IMatchServices

详细信息--->没有与"AutofacWebRequest"匹配的标签的作用域 在请求实例的范围内可见.

Details ---> No scope with a tag matching 'AutofacWebRequest' is visible from the scope in which the instance was requested.

如果在执行Web应用程序时看到此消息,则通常是 表示正在注册为按HTTP请求的组件 由SingleInstance()组件(或类似情况)请求. 在网络集成下,请始终向 依赖解析器或请求生存期范围,切勿来自 容器本身. (有关详细信息,请参见内部异常.)---> Autofac.Core.DependencyResolutionException:没有带有标签的范围 匹配的"AutofacWebRequest"在以下范围内可见: 实例被请求.

If you see this during execution of a web application, it generally indicates that a component registered as per-HTTP request is being requested by a SingleInstance() component (or a similar scenario). Under the web integration always request dependencies from the dependency resolver or the request lifetime scope, never from the container itself. (See inner exception for details.) ---> Autofac.Core.DependencyResolutionException: No scope with a tag matching 'AutofacWebRequest' is visible from the scope in which the instance was requested.

当我注册MatchServices类SingleInstance时,它正在工作,但InstancePerRequest不起作用.为什么?我什至没有在MatchServices中做任何DI.

When I register MatchServices class SingleInstance it is working but InstancePerRequest didn't work. Why? I didn't even do any DI in MatchServices.

推荐答案

按照

使用InstancePerLifetimeScope代替InstancePerRequest .在以前的ASP.NET集成中,您可以将依赖项注册为InstancePerRequest,这将确保每个HTTP请求仅创建一个依赖项实例.之所以可行,是因为Autofac负责设置每个请求的生命周期范围.随着Microsoft.Extensions.DependencyInjection的引入,按框架创建的每个请求和其他子生存期范围现在已成为框架提供的容器的一部分,因此所有子生存期范围都得到同等对待-不再有特殊的请求级别范围".代替注册您的依赖项InstancePerRequest,使用InstancePerLifetimeScope,您应该得到相同的行为.请注意,如果要在Web请求期间创建自己的生存期作用域,则将在这些子作用域中获得一个新实例.

Use InstancePerLifetimeScope instead of InstancePerRequest. In previous ASP.NET integration you could register a dependency as InstancePerRequest which would ensure only one instance of the dependency would be created per HTTP request. This worked because Autofac was in charge of setting up the per-request lifetime scope. With the introduction of Microsoft.Extensions.DependencyInjection, the creation of per-request and other child lifetime scopes is now part of the conforming container provided by the framework, so all child lifetime scopes are treated equally - there’s no special "request level scope" anymore. Instead of registering your dependencies InstancePerRequest, use InstancePerLifetimeScope and you should get the same behavior. Note if you are creating your own lifetime scopes during web requests, you will get a new instance in these child scopes.

此外,您还有另一个问题:

请注意,Populate基本上是添加内容的foreach 放入集合中的Autofac中.如果您注册 Populate之前Autofac中的内容,然后 ServiceCollection可以覆盖那些东西;如果您注册 Populate之后,这些注册可以覆盖所有内容 在ServiceCollection中.根据需要进行混合和匹配.

Note that Populate is basically a foreach to add things into Autofac that are in the collection. If you register things in Autofac BEFORE Populate then the stuff in the ServiceCollection can override those things; if you register AFTER Populate those registrations can override things in the ServiceCollection. Mix and match as needed.

换句话说,这些行的顺序也不正确:

In other words, you also have these lines in the wrong order:

builder.Populate(services);
builder.RegisterType<MatchServices>().As<IMatchServices>().InstancePerLifetimeScope();

这篇关于.NET Core 2.0 Autofac注册类InstancePerRequest不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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