.NET Core 标识和简单注入器交叉线不工作

问题描述

我在一个使用 asp.net 核心身份的项目上使用简单的注入器 (4.8.1)。我试图横向用户管理器,但我收到一条错误消息 No service for type 'Microsoft.AspNetCore.Identity.UserManager`1[Tenancy.Core.Domain.AppUser]' has been registered." SimpleInjectorConfig 类是:

public static class SimpleInjectorConfig
{
    private static Container _container;

    public static void ConfigureServices(IServiceCollection services,IConfiguration config,IWebHostEnvironment env)
    {
        _container = new Container();

        _container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();

        _container.ConfigureCore(config,env);

        _container.RegisterInitializer<BaseApiController>(controller =>
        {
            controller.Mediator = _container.GetInstance<IMediator>();
        });

        services.AddSingleton(_container);
        services.AddSingleton<IHttpContextAccessor,HttpContextAccessor>();

        services.AddControllers();

        services.AddLogging();
        // Sets up the basic configuration that for integrating Simple Injector with
        // ASP.NET Core by setting the DefaultScopedLifestyle,and setting up auto
        // cross wiring.
        services.AddSimpleInjector(_container,options =>
        {
            // AddAspNetCore() wraps web requests in a Simple Injector scope and
            // allows request-scoped framework services to be resolved.
            options
                .AddAspNetCore()
                .AddControllerActivation();
            options.AddLogging();
            options.CrossWire<UserManager<AppUser>>();
        });
    }

    public static void Configure(IApplicationBuilder app,IWebHostEnvironment env)
    {
        app.UseSimpleInjector(_container);
        app.UseRouting();
        app.UseAuthorization();
        app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            IdentityModelEventSource.ShowPII = true;
        }
        //app.UseHttpsRedirection();

        _container.Verify();
    }
}

我在 Startup.cs 文件调用了它

 public class Startup
{
    public IConfiguration _configuration { get; }
    public IWebHostEnvironment _env { get; }

    public Startup(IConfiguration configuration,IWebHostEnvironment env)
    {
        _configuration = configuration;
        _env = env;
    }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        SimpleInjectorConfig.ConfigureServices(services,_configuration,_env);
        CorsConfig.ConfigureServices(services);
        DatabaseConfig.ConfigureServices(services,_configuration);
        MvcConfig.ConfigureServices(services);
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app,IWebHostEnvironment env)
    {
        SimpleInjectorConfig.Configure(app,env);
        CorsConfig.Configure(app,_configuration);
        MvcConfig.Configure(app);
    }
}

配置有自己的类,并在 Startup 类中调用的 Startup 中调用只是为了保持清洁,SimpleInjectorConfig 是简单注入器的配置类。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...