RickStrahl / Westwind.AspnetCore.LiveReload

ASP.NET Core Live Reload Middleware that monitors file changes in your project and automatically reloads the browser's active page
Other
469 stars 42 forks source link

null reference exception in: ResponseStreamWrapper.IsHtmlResponse #27

Closed harishrathi closed 4 years ago

harishrathi commented 4 years ago

Getting null reference exception

System.NullReferenceException: Object reference not set to an instance of an object. at Westwind.AspnetCore.LiveReload.ResponseStreamWrapper.IsHtmlResponse(Boolean forceReCheck) at Westwind.AspnetCore.LiveReload.ResponseStreamWrapper.FlushAsync(CancellationToken cancellationToken) at Microsoft.AspNetCore.Http.StreamResponseBodyFeature.StartAsync(CancellationToken cancellationToken) at Microsoft.AspNetCore.Http.DefaultHttpResponse.StartAsync(CancellationToken cancellationToken) at Microsoft.AspNetCore.Http.HttpResponseWritingExtensions.WriteAsync(HttpResponse response, String text, Encoding encoding, CancellationToken cancellationToken) at Microsoft.AspNetCore.Http.HttpResponseWritingExtensions.WriteAsync(HttpResponse response, String text, CancellationToken cancellationToken) at ICollect.Public.WebApi.AppFilters.CustomExceptionMiddleware.HandleExceptionAsync(HttpContext context, Exception exception, HostingSetting setting) in ....\AppFilters\CustomExceptionMiddleware.cs:line 46

The custom exception handler is

        private Task HandleExceptionAsync(HttpContext context, Exception exception, HostingSetting setting)
        {
            context.Response.ContentType = "application/json";
            context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
            var error = new
            {
                exception.Message,
                Stacktrace = setting.EnableExceptionDetails ? exception : null
            };
            return context.Response.WriteAsync(JsonConvert.SerializeObject(error));
        }

My .Net core 3.0 project is only for API and does not deal with MVC as such.

RickStrahl commented 4 years ago

Hmmm... So it looks like hte ResponseStream is not set when the error occurs which means either the error occurs before the Response has been initialized (ie. pre-controller from other middleware?) or the exception is wiping out the original response.

Do you know where the actual error that is going into the CustomExceptionMiddleware is triggered?

I'll double check and see if there's some way to effectively check to make sure the ResponseStream is actually available.

RickStrahl commented 4 years ago

I've added additional null checks into the check routine that writes to the response stream, so that might help although I have a feeling this will push the issue up higher.

Give the 0.2.1 release a try and see if that fixes it. If not if you could create a small test project so I can see the exact issue you're describing that'd be helpful.

Also that said for API projects I'm not sure that LiveReload provides any benefit. Unless you have some sort of HTML content that needs to be redisplayed this Live Reload functionality isn't really useful. For server code reloads dotnet watch run is all that should be needed.

harishrathi commented 4 years ago

Sorry, for raising the issue without full investigation. You are right, Live Reload is not really required for API project. I though the .net core dotnet watch run does not watch for dependent project changes, hence I had added this package. But it seems that the build in dotnet watch run does watch for changes in dependent projects.

Sorry once again.

RickStrahl commented 4 years ago

Would be nice if you could still try this and see if the fix addressed the issue - other than updting the package nothing else should have to change in that same scenario.

harishrathi commented 4 years ago

Issue is resolved. Upgraded from 0.1.18 to 0.2.1. Thanks a lot for quick turnaround. I will try to submit a PR for next time.