Xabaril / AspNetCore.Diagnostics.HealthChecks

Enterprise HealthChecks for ASP.NET Core Diagnostics Package
Apache License 2.0
4.06k stars 793 forks source link

Unexpected character encountered while parsing value: H. Path '', line 0, position 0. #440

Closed SyntaxWarrior closed 4 years ago

SyntaxWarrior commented 4 years ago

I just created a new .net core project and added the health check UI to it in order to monitor a few services. I get the below error every time the background worker in the UI tries to collect data.

fail: HealthChecks.UI.Core.HostedService.HealthCheckReportCollector[0]
      GetHealthReport threw an exception when trying to get report from http://localhost:55867/health configured with name security.
Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: H. Path '', line 0, position 0.
   at Newtonsoft.Json.JsonTextReader.ParseValue()
   at Newtonsoft.Json.JsonReader.ReadAndMoveToContent()
   at Newtonsoft.Json.JsonReader.ReadForType(JsonContract contract, Boolean hasConverter)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
   at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType)
   at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
   at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
   at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value)
   at System.Net.Http.HttpResponseMessageExtensions.As[TContent](HttpResponseMessage response)
   at HealthChecks.UI.Core.HostedService.HealthCheckReportCollector.GetHealthReport(HealthCheckConfiguration configuration)

On the app I want to monitor I have

// ConfigureServices
services.AddHealthChecks().AddSqlServer(securitylConnectionString, "SELECT TOP 1 * FROM [security].[Roles]");
// Configure
app.UseEndpoints(endpoints =>
 {
   endpoints.MapControllers();
   endpoints.MapHealthChecks("/health");
});

and in the UI I have (other than this its a default core 3.1 project)

// ConfigureServices
services.AddHealthChecksUI("healthCheckDb", s =>
{
    s.AddHealthCheckEndpoint("security", "http://localhost:55867/health"); // i can surf to this address and see "Healthy"
    s.SetEvaluationTimeInSeconds(10);
});
// Configure
app.UseEndpoints(endpoints =>
{
    endpoints.MapControllers();
    endpoints.MapHealthChecksUI(setup =>
    {
        setup.UIPath = "/healthcheck";
    });
});

The service I want to monitor is returning OK 200, and the word Healthy.

Any suggestions as to what I have done wrong?

P.S. I'm running everything on my localhost computer, and can surf to the urls in the browser.

SyntaxWarrior commented 4 years ago

bild This is what I see in the interface.

CarlosLanderas commented 4 years ago

You are not using the HealthChecks UI response writer. Please take a look to the samples:

https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks/blob/master/samples/HealthChecks.UIAndApi/Startup.cs#L82

This repository works with Microsoft packages and you need to define the writer in the MaphealthChecks settings

SyntaxWarrior commented 4 years ago

Thank you. That was the solution.

For anyone else, on the service I want to monitor I added a reference to:

AspNetCore.HealthChecks.UI.Client

And updated my code to:

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                endpoints.MapHealthChecks("/health", new HealthCheckOptions()
                {
                    Predicate = _ => true,
                    ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
                });
            });

As a new user (since about 2 hours ago) I really like this project, and it was not to hard to get it working (well, besides this issue). One critique I might add is that I think its difficult to tell what code I need to add to which side. The service being monitored or the UI project. I mean it's clear now, but this was basically the only thing I had trouble extracting from the text.

CarlosLanderas commented 4 years ago

Thanks @SyntaxWarrior. I might propose adding a new change to simplify and unify our endpoints with Microsoft's