簡體   English   中英

無法從根提供程序 .Net Core 2 解析范圍服務

[英]Cannot resolve scoped service from root provider .Net Core 2

當我嘗試運行我的應用程序時出現錯誤

InvalidOperationException: Cannot resolve 'API.Domain.Data.Repositories.IEmailRepository' from root provider because it requires scoped service 'API.Domain.Data.EmailRouterContext'.

奇怪的是,就我所知,這個 EmailRepository 和界面的設置與我所有其他存儲庫完全相同,但沒有為它們拋出任何錯誤。 該錯誤僅在我嘗試使用 app.UseEmailingExceptionHandling(); 時發生; 線。 這是我的一些 Startup.cs 文件。

public class Startup
{
    public IConfiguration Configuration { get; protected set; }
    private APIEnvironment _environment { get; set; }

    public Startup(IConfiguration configuration, IHostingEnvironment env)
    {
        Configuration = configuration;

        _environment = APIEnvironment.Development;
        if (env.IsProduction()) _environment = APIEnvironment.Production;
        if (env.IsStaging()) _environment = APIEnvironment.Staging;
    }

    public void ConfigureServices(IServiceCollection services)
    {
        var dataConnect = new DataConnect(_environment);

        services.AddDbContext<GeneralInfoContext>(opt => opt.UseSqlServer(dataConnect.GetConnectString(Database.GeneralInfo)));
        services.AddDbContext<EmailRouterContext>(opt => opt.UseSqlServer(dataConnect.GetConnectString(Database.EmailRouter)));

        services.AddWebEncoders();
        services.AddMvc();

        services.AddScoped<IGenInfoNoteRepository, GenInfoNoteRepository>();
        services.AddScoped<IEventLogRepository, EventLogRepository>();
        services.AddScoped<IStateRepository, StateRepository>();
        services.AddScoped<IEmailRepository, EmailRepository>();
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole();

        app.UseAuthentication();

        app.UseStatusCodePages();
        app.UseEmailingExceptionHandling();

        app.UseMvcWithDefaultRoute();
    }
}

這是電子郵件存儲庫

public interface IEmailRepository
{
    void SendEmail(Email email);
}

public class EmailRepository : IEmailRepository, IDisposable
{
    private bool disposed;
    private readonly EmailRouterContext edc;

    public EmailRepository(EmailRouterContext emailRouterContext)
    {
        edc = emailRouterContext;
    }

    public void SendEmail(Email email)
    {
        edc.EmailMessages.Add(new EmailMessages
        {
            DateAdded = DateTime.Now,
            FromAddress = email.FromAddress,
            MailFormat = email.Format,
            MessageBody = email.Body,
            SubjectLine = email.Subject,
            ToAddress = email.ToAddress
        });
        edc.SaveChanges();
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    private void Dispose(bool disposing)
    {
        if (!disposed)
        {
            if (disposing)
                edc.Dispose();
            disposed = true;
        }
    }
}

最后是異常處理中間件

public class ExceptionHandlingMiddleware
{
    private const string ErrorEmailAddress = "errors@ourdomain.com";
    private readonly IEmailRepository _emailRepository;

    private readonly RequestDelegate _next;

    public ExceptionHandlingMiddleware(RequestDelegate next, IEmailRepository emailRepository)
    {
        _next = next;
        _emailRepository = emailRepository;
    }

    public async Task Invoke(HttpContext context)
    {
        try
        {
            await _next.Invoke(context);
        }
        catch (Exception ex)
        {
            await HandleExceptionAsync(context, ex, _emailRepository);
        }
    }

    private static Task HandleExceptionAsync(HttpContext context, Exception exception,
        IEmailRepository emailRepository)
    {
        var code = HttpStatusCode.InternalServerError; // 500 if unexpected

        var email = new Email
        {
            Body = exception.Message,
            FromAddress = ErrorEmailAddress,
            Subject = "API Error",
            ToAddress = ErrorEmailAddress
        };

        emailRepository.SendEmail(email);

        context.Response.ContentType = "application/json";
        context.Response.StatusCode = (int) code;
        return context.Response.WriteAsync("An error occured.");
    }
}

public static class AppErrorHandlingExtensions
{
    public static IApplicationBuilder UseEmailingExceptionHandling(this IApplicationBuilder app)
    {
        if (app == null)
            throw new ArgumentNullException(nameof(app));
        return app.UseMiddleware<ExceptionHandlingMiddleware>();
    }
}

更新:我發現這個鏈接https://github.com/aspnet/DependencyInjection/issues/578這導致我改變了我的 Program.cs 文件的 BuildWebHost 方法從此

public static IWebHost BuildWebHost(string[] args)
{
    return WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>()
        .Build();
}

對此

public static IWebHost BuildWebHost(string[] args)
{
    return WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>()
        .UseDefaultServiceProvider(options =>
            options.ValidateScopes = false)
        .Build();
}

我不知道到底發生了什么,但現在似乎可以工作了。

您在Startup類中將IEmailRepository注冊為范圍服務。 這意味着您不能將其作為構造函數參數注入Middleware因為只有Singleton服務可以通過Middleware的構造函數注入來解析。 您應該將依賴項移動到Invoke方法,如下所示:

public ExceptionHandlingMiddleware(RequestDelegate next)
{
    _next = next;
}

public async Task Invoke(HttpContext context, IEmailRepository emailRepository)
{
    try
    {
        await _next.Invoke(context);
    }
    catch (Exception ex)
    {
        await HandleExceptionAsync(context, ex, emailRepository);
    }
}

另一種獲取作用域依賴實例的方法是將服務提供者( IServiceProvider )注入到中間件構造函數中,在Invoke方法中創建scope ,然后從作用域中獲取所需的服務:

using (var scope = _serviceProvider.CreateScope()) {
    var _emailRepository = scope.ServiceProvider.GetRequiredService<IEmailRepository>();

    //do your stuff....
}

查看在asp.net 核心依賴項注入最佳實踐提示技巧中的方法體解析服務以獲取更多詳細信息。

中間件始終是單例,因此您不能將作用域依賴項作為中間件構造函數中的構造函數依賴項。

中間件支持 Invoke 方法的方法注入,因此您只需將 IEmailRepository emailRepository 作為參數添加到該方法,它將被注入到那里,並且在作用域范圍內都可以。

public async Task Invoke(HttpContext context, IEmailRepository emailRepository)
{

    ....
}

您的middlewareservice必須相互兼容,以便通過middlewareconstructor注入service 在這里,您的middleware已創建為基於convention-based middleware ,這意味着它充當singleton service並且您已將服務創建為scoped-service 因此,您不能將scoped-service注入到singleton-service scoped-service的構造函數中,因為它強制scoped-service充當singleton scoped-service 但是,這里有您的選擇。

  1. 將您的服務作為參數注入InvokeAsync方法。
  2. 如果可能,讓您的服務成為單例服務。
  3. 將您的middleware轉換factory-based middleware

Factory-based middleware能夠充當scoped-service 因此,您可以通過該中間件的構造函數注入另一個scoped-service 下面,我向您展示了如何創建基於factory-based中間件。

這僅用於演示。 所以,我已經刪除了所有其他代碼。

public class Startup
{
    public Startup()
    {
    }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddScoped<TestMiddleware>();
        services.AddScoped<TestService>();
    }

    public void Configure(IApplicationBuilder app)
    {
        app.UseMiddleware<TestMiddleware>();
    }
}

測試TestMiddleware

public class TestMiddleware : IMiddleware
{
    public TestMiddleware(TestService testService)
    {
    }

    public Task InvokeAsync(HttpContext context, RequestDelegate next)
    {
        return next.Invoke(context);
    }
}

TestService

public class TestService
{
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM