web-dev-qa-db-ja.com

ルートプロバイダー.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();
    }
}

これが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 = "[email protected]";
    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();
}

何が起こっているのか正確にはわかりませんが、今はうまくいくようです。

43
geoff swartz

IEmailRepositoryクラスで、Startupをスコープ付きサービスとして登録しました。つまり、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);
    }
}
91
user1336

スコープ依存関係のインスタンスを取得する別の方法は、サービスプロバイダー(IServiceProvider)をミドルウェアコンストラクターに注入し、scopeメソッドでInvokeを作成してから、スコープから必要なサービスを取得することです。

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

    //do your stuff....
}

詳細については、asp.netコア依存性注入のベストプラクティスのヒントトリック のメソッド本体でのサービスの解決をご覧ください。

29
Riddik

ミドルウェアは常にシングルトンであるため、ミドルウェアのコンストラクターでコンストラクターの依存関係としてスコープ依存関係を持つことはできません。

ミドルウェアはInvokeメソッドでのメソッドインジェクションをサポートしているため、IEmailRepository emailRepositoryをそのメソッドのパラメーターとして追加するだけで、そこにインジェクトされ、スコープどおりに問題なく動作します。

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

    ....
}
21
Joe Audette

middlewareserviceを介してserviceを注入するには、constructormiddlewareが相互に互換性がある必要があります。ここで、middlewareconvention-based middlewareとして作成されています。つまり、singleton serviceとして機能し、scoped-serviceとしてサービスを作成しています。したがって、scoped-servicesingleton-serviceのコンストラクターに挿入することはできません。これは、scoped-servicesingletonとして強制するためです。ただし、オプションは次のとおりです。

  1. InvokeAsyncメソッドへのパラメーターとしてサービスを注入します。
  2. 可能であれば、サービスをシングルトンにします。
  3. middlewarefactory-basedに変換します。

Factory-based middlewarescoped-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
{
}
1