Dependency injection asp.net core 3.1中带有Autofac的ServiceLocator

Dependency injection asp.net core 3.1中带有Autofac的ServiceLocator,dependency-injection,autofac,asp.net-core-3.1,service-locator,Dependency Injection,Autofac,Asp.net Core 3.1,Service Locator,我正在开发一个asp.net core 3.1 webapi应用程序,并使用Autofac作为DI容器。 对于一种特殊情况,我不能使用构造函数注入、属性注入或方法注入。我唯一的方法是在Autofac的支持下实现一种ServiceLocator模式 *我知道服务定位器是一种反模式,但只有当它是唯一的机会时,我才会使用它* 我创建了一个小静态类: public static class ServiceLocator { private static XXXX Resolver;

我正在开发一个asp.net core 3.1 webapi应用程序,并使用Autofac作为DI容器。 对于一种特殊情况,我不能使用构造函数注入、属性注入或方法注入。我唯一的方法是在Autofac的支持下实现一种ServiceLocator模式

*我知道服务定位器是一种反模式,但只有当它是唯一的机会时,我才会使用它*

我创建了一个小静态类:

public static class ServiceLocator
{

    private static XXXX Resolver;

    public static T Resolve<T>()
    {
        return Resolver.Resolve<T>();
    }

    public static void SetCurrentResolver(XXXX resolver)
    {
        Resolver = resolver;
    }
}
我试图传递ILifetimeScope实例,但当我稍后在服务定位器中使用它时,它将被释放,然后无法工作。我想传递IContainer对象,但无法在Startup.cs中检索实例(无论是在Configure方法中还是在ConfigureContainer中)

我报告ConfigureContainer方法以完成

    public void ConfigureContainer(ContainerBuilder builder)
    {
        //first register the dependency for WebApi Project

        builder.RegisterType<HttpContextUserService>().As<IUserService>().SingleInstance();

        //and then register the dependency for all other project

        var appConfiguration = new AppConfiguration();
        Configuration.GetSection("Application").Bind(appConfiguration);
        builder.RegisterInstance(appConfiguration).SingleInstance();

        builder.RegisterModule(new DependencyInjectionBootstrapper(appConfiguration)); 
        
    }
public void配置容器(ContainerBuilder生成器)
{
//首先注册WebApi项目的依赖项
builder.RegisterType().As().SingleInstance();
//然后为所有其他项目注册依赖项
var appConfiguration=新的appConfiguration();
Configuration.GetSection(“应用程序”).Bind(appConfiguration);
RegisterInstance(appConfiguration).SingleInstance();
RegisterModule(新的DependencyInjectionBootstrapper(appConfiguration));
}
有人能帮我解决这个问题吗

谢谢

如果你看一下这个例子,事实上就是这样

以下是相关的部分

公共类启动
{
公共启动(IHostingEnvironment环境)
{
//为简洁起见,省略了正文。
}
公共ILifetimeScope自动传真容器{get;private set;}
public void配置服务(IServiceCollection服务)
{
//为简洁起见,省略了正文。
}
公共void配置容器(ContainerBuilder生成器)
{
//为简洁起见,省略了正文。
}
公共void配置(IApplicationBuilder应用程序)
{
//如果出于某种原因,需要对生成的容器进行引用,则
//可以使用方便的扩展方法GetAutofacRoot。
//这是您设置服务定位器的位置。
this.AutofacContainer=app.ApplicationServices.GetAutofacRoot();
}
}

非常感谢。我试图查看doc()的这一页,但似乎无法使用asp.net core 3.1。你的解决方案满足了我的需要。谢谢
    public void ConfigureContainer(ContainerBuilder builder)
    {
        //first register the dependency for WebApi Project

        builder.RegisterType<HttpContextUserService>().As<IUserService>().SingleInstance();

        //and then register the dependency for all other project

        var appConfiguration = new AppConfiguration();
        Configuration.GetSection("Application").Bind(appConfiguration);
        builder.RegisterInstance(appConfiguration).SingleInstance();

        builder.RegisterModule(new DependencyInjectionBootstrapper(appConfiguration)); 
        
    }