[英]“Cannot resolve scoped service from root provider” with custom EF Core SeriLog Sink
[英]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)
{
....
}
您的middleware
和service
必須相互兼容,以便通過middleware
的constructor
注入service
。 在這里,您的middleware
已創建為基於convention-based middleware
,這意味着它充當singleton service
並且您已將服務創建為scoped-service
。 因此,您不能將scoped-service
注入到singleton-service
scoped-service
的構造函數中,因為它強制scoped-service
充當singleton
scoped-service
。 但是,這里有您的選擇。
InvokeAsync
方法。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.