使用IApplicationBuilder的Singleton服务返回UserManager作用域服务错误

时间:2018-03-27 02:33:11

标签: c# asp.net-core dependency-injection

我有点困惑,我在我的.NET Core应用程序中注册了RabbitMQ监听器并返回了一个范围错误:

  

调用方法时遇到错误' BuildWebHost'课程'课程'。继续没有应用程序服务提供商。错误:无法使用作用域服务' Microsoft.AspNetCore.Identity.UserManager`1 [AuthService.Models.DbEntities.User]'来自singleton' AuthService.Services.RabbitMqListener'。

但是,我实际上并未在我的服务中使用UserManager。 consturctor仅使用其他单例服务:

public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton<RabbitMqListener>();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseRabbitListener();
}

public static class ApplicationBuilderExtentions
{
    public static RabbitMqListener Listener { get; set; }

    public static IApplicationBuilder UseRabbitListener(this IApplicationBuilder app)
    {
        Listener = app.ApplicationServices.GetService<RabbitMqListener>();

        var life = app.ApplicationServices.GetService<IApplicationLifetime>();

        life.ApplicationStarted.Register(OnStarted);

        life.ApplicationStopping.Register(OnStopping);

        return app;
    }

    private static void OnStarted()
    {
        Listener.Register();
    }

    private static void OnStopping()
    {
        Listener.Deregister();
    }
}

我的RabbitMQ服务的构造函数:

public class RabbitMqListener
{
    private readonly IJWTFactory _jwtFactory;
    private readonly IConnection _mqConnection;
    private readonly IModel _channel;
    private readonly ILogger<RabbitMqListener> _logger;

    public RabbitMqListener(
            IJWTFactory jwtFactory,
            ILogger<RabbitMqListener> logger
        )
    {
        _jwtFactory = jwtFactory;
        _mqConnection = new ConnectionFactory() { HostName = "localhost" }.CreateConnection();
        _channel = _mqConnection.CreateModel();
        _logger = logger;
    }
}

任何人都可以解释为什么UserManager服务被用于我的服务并没有使用它?

0 个答案:

没有答案