IISHttpServer OnDisconnect NullReferenceException - PullRequest
2 голосов
/ 30 апреля 2020

Во время работы базового веб-приложения. NET, которое в основном основано на выполнении конечных точек, я часто получаю NullReferenceException на вызове AbortIO после вызова на context.Response.Redirect(url, false) в моем коде.

У кого-нибудь есть идеи?

Например, следующий код:

app.UseEndpoints(endpoints =>
{
    endpoints.MapGet("/someurl", async context => {
        context.Response.Redirect("/someotherurl", false);
    });
});

Часто здесь ошибки, потому что context равен нулю:

private static void OnDisconnect(IntPtr pvManagedHttpContext)
{
    IISHttpContext context = null;
    try
    {
        context = (IISHttpContext)GCHandle.FromIntPtr(pvManagedHttpContext).Target;
        context.AbortIO(clientDisconnect: true);
    }
    catch (Exception ex)
    {
        context?.Server._logger.LogError(0, ex, $"Unexpected exception in {nameof(IISHttpServer)}.{nameof(OnDisconnect)}.");
    }
}

Источник: Microsoft.AspNetCore.Server.IIS.Core.IISHttpServer , строка 173

...