.net Core

时间:2018-10-23 13:35:59

标签: c# asp.net-core

我有exceptionMiddleware。并将所有错误记录到数据库中。但是,当第一次使用时,它工作正常,但是第二次尝试,在将SaveChangesAsync到数据库期间给了我错误。该错误的原因可能是什么。

无法访问已处置的对象。导致此错误的常见原因是处理从依赖项解析的上下文  注入,然后尝试在应用程序的其他位置使用相同的上下文实例。如果您可能会发生这种情况  在上下文上调用Dispose()或将上下文包装在using语句中。如果您使用依赖项注入,  您应该让依赖项注入容器负责处理上下文实例。 对象名称:“ MyDbContext”。

“该连接不支持MultipleActiveResultSet。”

MyDbContext.cs

public class MyDbContext : DbContext
{
    private readonly IAuditHelper auditHelper;

    public MyDbContext(DbContextOptions<MyDbContext> options, IAuditHelper auditHelper)
        : base(GetOptions())
    {
        this.auditHelper = auditHelper;
    }

    private static DbContextOptions GetOptions()
    {
        return SqlServerDbContextOptionsExtensions.UseSqlServer(new DbContextOptionsBuilder(), "server=asdf; database=asdf; user id=asdf; password=asdf").Options;
    }

    public async Task<int> SaveChangesWithoutAuditAsync(CancellationToken cancellationToken = default(CancellationToken))
    {
        return (await base.SaveChangesAsync(true, cancellationToken));
    }
}

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddHttpContextAccessor();
    services.AddScoped<IUnitOfWork, UnitOfWork>();
    services.AddScoped<IJwtHelper, JwtHelper>();
    services.AddScoped<IAuditHelper, AuditHelper>();
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

    services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
    services.AddDbContext<MyDbContext>();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, IHttpContextAccessor httpContextAccessor)
{
    app.ConfigureCustomExceptionMiddleware();

    app.UseHttpsRedirection();
    app.UseMvc();
}

ExceptionMiddleware.cs

public class ExceptionMiddleware
{
    private readonly RequestDelegate _next;

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

    public async Task InvokeAsync(HttpContext httpContext, IUnitOfWork unitOfWork, IJwtHelper jwtHelper)
    {
        try
        {
            await _next(httpContext);
        }
        catch (Exception ex)
        {
            await HandleExceptionAsync(httpContext, ex, unitOfWork, jwtHelper);
        }
    }

    private Task HandleExceptionAsync(HttpContext context, Exception exception, IUnitOfWork unitOfWork, IJwtHelper jwtHelper)
    {
        Log log = new Log();
        log.UserIp = jwtHelper.GetValueFromToken("UserIp");

        unitOfWork.LogRepo.AddOrUpdate(log);
        unitOfWork.CompleteAsync();

        return context.Response.WriteAsync(exception.Message);
    }
}

1 个答案:

答案 0 :(得分:0)

我发现了问题。问题是HandleExceptionAsync方法。它必须添加aync并等待,例如:

  private async Task HandleExceptionAsync(HttpContext context, Exception exception, IUnitOfWork unitOfWork, IJwtHelper jwtHelper)
    {
        Log log = new Log();
        log.UserIp = jwtHelper.GetValueFromToken("UserIp");

        unitOfWork.LogRepo.AddOrUpdate(log);
        await unitOfWork.CompleteAsync();
        await context.Response.WriteAsync(exception.Message);

        return ;
    }