无法从根提供程序解析作用域服务

当我尝试运行我的应用程序,我得到了错误

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

奇怪的是,这个 EmailRepository 和接口的设置与我所知道的所有其他存储库完全一样,但是没有为它们抛出错误。只有当我尝试使用该应用程序时才会出现错误。异常处理() ; 行。这是我的 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();
}
}

这里是 EmailRepository

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();
}

我不知道到底发生了什么,但现在看起来起作用了。

149378 次浏览

中间件始终是一个单例,因此您不能在中间件的构造函数中将作用域依赖项作为构造函数依赖项。

中间件支持 Invoke 方法上的方法注入,所以您可以将 IEmailRepository 作为一个参数添加到该方法中,它将在那里被注入,并且作用域很好。

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


....
}

您在 Startup类中将 IEmailRepository注册为作用域服务。 这意味着在 Middleware中不能将其作为构造函数参数注入,因为在 Middleware中只能通过构造函数注入解析 Singleton服务。您应该像下面这样将依赖关系移动到 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 核心依赖注入的最佳实践技巧中的 在方法体中解析服务

为了通过 middlewareconstructor注入 service,你的 middlewareservice必须相互兼容。在这里,您的 middleware被创建为一个 convention-based middleware,这意味着它作为一个 singleton service,您已经创建了您的 scoped-service服务。因此,您不能将 scoped-service注入到 service0的构造函数中,因为它强制 scoped-service充当 service2的构造函数。然而,这是你的选择。

  1. 将您的服务作为参数注入到 InvokeAsync方法中。
  2. 如果可能的话,使您的服务成为单例服务。
  3. 把你的 middleware变成 factory-based

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
{
}