AnderssonPeter / CompressedStaticFiles

asp.net core middleware to send compressed static files to the browser without having to compress on demand, also has support for sending more advanced image formats when the browser indicates that i has support for it.
Apache License 2.0
88 stars 18 forks source link

System.NullReferenceException: 'Object reference not set to an instance of an object.' When using Compressed Static File Middleware? #35

Closed aloksharma1 closed 2 years ago

aloksharma1 commented 2 years ago

Hello, when i am using the compressed middleware i am getting this issue on trying to access embedded files related to #34

info: CompressedStaticFiles.CompressedAlternativeFileProvider[1]
      Sending file. Request file: '/embedbackend/plugins/global/plugins.bundle.css'. Served file: '/embedbackend/plugins/global/plugins.bundle.css.gz'. Original file size: 567386. Served file size: 72449
fail: Microsoft.AspNetCore.Server.Kestrel[13]
      Connection id "0HMHK8J2D283R", Request id "0HMHK8J2D283R:00000009": An unhandled exception was thrown by the application.
      System.NullReferenceException: Object reference not set to an instance of an object.
         at CompressedStaticFiles.CompressedAlternativeFile.Prepare(IContentTypeProvider contentTypeProvider, StaticFileResponseContext staticFileResponseContext)
         at CompressedStaticFiles.CompressedStaticFileMiddleware.<>c__DisplayClass6_0.<InitializeStaticFileOptions>b__0(StaticFileResponseContext context)
         at Microsoft.AspNetCore.StaticFiles.StaticFileContext.ApplyResponseHeaders(Int32 statusCode)
         at Microsoft.AspNetCore.StaticFiles.StaticFileContext.SendAsync()
         at Microsoft.AspNetCore.StaticFiles.StaticFileContext.ServeStaticFile(HttpContext context, RequestDelegate next)
         at Ultra.ImageTools.Services.Middleware.ImageResizerMiddleware.InvokeAsync(HttpContext context) in D:\Projects\Web\dotnet\UltraWeb\Net5\Ultra.WebHost\Ultra.ImageTools\Services\Middleware\ImageResizerMiddleware.cs:line 106
         at Microsoft.AspNetCore.Builder.Extensions.MapMiddleware.Invoke(HttpContext context)
         at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)
[23:57:59 ERR] Connection id "0HMHK8J2D283R", Request id "0HMHK8J2D283R:00000009": An unhandled exception was thrown by the application.
System.NullReferenceException: Object reference not set to an instance of an object.
   at CompressedStaticFiles.CompressedAlternativeFile.Prepare(IContentTypeProvider contentTypeProvider, StaticFileResponseContext staticFileResponseContext)
   at CompressedStaticFiles.CompressedStaticFileMiddleware.<>c__DisplayClass6_0.<InitializeStaticFileOptions>b__0(StaticFileResponseContext context)
   at Microsoft.AspNetCore.StaticFiles.StaticFileContext.ApplyResponseHeaders(Int32 statusCode)
   at Microsoft.AspNetCore.StaticFiles.StaticFileContext.SendAsync()
   at Microsoft.AspNetCore.StaticFiles.StaticFileContext.ServeStaticFile(HttpContext context, RequestDelegate next)   

as i checked the middleware doesn't handle the embedded file headers correctly, hence pass it on to next one so is the problem with this issue. Is there any way to provide it

AnderssonPeter commented 2 years ago

I have no idea what a embedded file header is, but if you make a pull request with a unit test and a fix i would be happy to merge it and release a new version!

aloksharma1 commented 2 years ago

here embedded file is same as just your other files on system, its just embedded in assembly that's it. I am checking it further, if I get a resolution i will post it here, since you are targeting multiple framework its a bit hard to add these embedded project and test it (i will need to put code instructions for both frameworks to make it work) so it will take some time.

aloksharma1 commented 2 years ago

i found the issue, i will make a PR by tomorrow. i dont think this need a unit test as this only need an additional option to supply virtual paths in that case program will simply switch this line

if (contentTypeProvider.TryGetContentType(staticFileResponseContext.File.PhysicalPath.Remove(
                        staticFileResponseContext.File.PhysicalPath.Length - fileExtension.Length, fileExtension.Length), out var contentType))

this is where null error is happening.

aloksharma1 commented 2 years ago

ok, i have made the PR let me know when its nuget ready :)