aspnet / Diagnostics

[Archived] Diagnostics middleware for reporting info and handling exceptions and errors in ASP.NET Core, and diagnosing Entity Framework Core migrations errors. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
213 stars 111 forks source link

ArgumentNullException when not setting Name in IHealthCheck #519

Closed andrecarlucci closed 5 years ago

andrecarlucci commented 5 years ago

Hi guys,

You will get an ArgumentNullException when creating your own HealthCheck handler without setting something to its Name property:

Microsoft.AspNetCore.Server.Kestrel:Error: Connection id "0HLHTL2OE18LU", Request id "0HLHTL2OE18LU:00000002": An unhandled exception was thrown by the application.

System.ArgumentNullException: Value cannot be null.
Parameter name: key
   at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
   at System.Collections.Generic.Dictionary`2.set_Item(TKey key, TValue value)
   at Microsoft.Extensions.Diagnostics.HealthChecks.HealthCheckService.CheckHealthAsync(Func`2 predicate, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Diagnostics.HealthChecks.HealthCheckMiddleware.InvokeAsync(HttpContext httpContext)
   at Microsoft.AspNetCore.Builder.Extensions.MapMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Server.IISIntegration.IISMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)

As the exception is not helpful at all, it took me a little while to figure it out (I had to look at the source code).

I believe the solution for this would be:

Thanks!

mkArtakMSFT commented 5 years ago

Thanks for contacting us, @andrecarlucci. @rynowak, seems like something we can do in 2.2 timeframe.

rynowak commented 5 years ago

Hi, @andrecarlucci - as of 2.2.0-preview3 the health check doesn't define its own name, it's passed in (and null-checked) when the check is registered.