aspnet / AspNetKatana

Microsoft's OWIN implementation, the Katana project
Apache License 2.0
967 stars 333 forks source link

NullReferenceException calling IOwinContext.Request.RemoteIpAddress #455

Closed abatishchev closed 2 years ago

abatishchev commented 2 years ago

Under certain (unknown) circumstances the following code fails with NullReferenceException:

public override async Task Invoke(IOwinContext context)
{
    var sw = Stopwatch.StartNew();
    try
    {
      await this.Next.Invoke(context).ConfigureAwait(false);
    }
    catch (Exception ex)
    {
      LogException(ex);
    }
    finally
    {
      sw.Stop();
      LogHttpRequest(context, operation, tenant, x509Cert);
    }
}

private void LogHttpRequest(IOwinContext context)
{
    // here
    var remoteIpAddress = context.Request?.RemoteIpAddress; 

    // or here
    var hostName = context.Request?.Host.Value;
}

Environment:

.NET Framework 4.6.2
Azure Service Fabric

Packages:

<PackageReference Include="Microsoft.Owin" Version="4.2.0" />
<PackageReference Include="Microsoft.Owin.FileSystems" Version="4.2.0" />
<PackageReference Include="Microsoft.Owin.Host.HttpListener" Version="4.2.0" />
<PackageReference Include="Microsoft.Owin.Hosting" Version="4.2.0" />
<PackageReference Include="Microsoft.Owin.Security" Version="4.2.0" />
<PackageReference Include="Microsoft.Owin.Security.ActiveDirectory" Version="4.2.0" />
<PackageReference Include="Microsoft.Owin.StaticFiles" Version="4.2.0" />
abatishchev commented 2 years ago

It turned out the method in question was wrapped with a try/catch that was swallowing the exception. I've removed it and will report back if the exception comes back, or it's gone.