Taritsyn / WebMarkupMin

The Web Markup Minifier (abbreviated WebMarkupMin) - a .NET library that contains a set of markup minifiers. The objective of this project is to improve the performance of web applications by reducing the size of HTML, XHTML and XML code.
Apache License 2.0
440 stars 48 forks source link

ArgumentNullException: Value cannot be null. (Parameter 'destination') #151

Closed toolgood closed 1 year ago

toolgood commented 1 year ago

error message

ArgumentNullException: Value cannot be null. (Parameter 'destination')
System.ArgumentNullException.Throw(string paramName)
System.IO.Stream.ValidateCopyToArguments(Stream destination, int bufferSize)
System.IO.MemoryStream.CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
System.IO.Stream.CopyToAsync(Stream destination)
WebMarkupMin.AspNetCore6.BodyWrapperStreamBase.InternalFinishAsync()
WebMarkupMin.AspNetCore6.BodyWrapperStreamWithResponseBodyFeature.FinishAsync()
WebMarkupMin.AspNetCore6.WebMarkupMinMiddleware.InvokeCore(HttpContext context, bool useMinification, bool useCompression)
WebMarkupMin.AspNetCore6.WebMarkupMinMiddleware.InvokeCore(HttpContext context, bool useMinification, bool useCompression)
Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.ResponseCaching.ResponseCachingMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.ResponseCompression.ResponseCompressionMiddleware.InvokeCore(HttpContext context)
Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

WebMarkupMin code

            builder.Services.AddWebMarkupMin(options => {
                options.AllowMinificationInDevelopmentEnvironment = true;
                options.AllowCompressionInDevelopmentEnvironment = true;
            }).AddHtmlMinification(options => {
                HtmlMinificationSettings settings = options.MinificationSettings;
                settings.RemoveRedundantAttributes = true;
                settings.RemoveHttpProtocolFromAttributes = true;
                settings.RemoveHttpsProtocolFromAttributes = true;

                options.CssMinifierFactory = new NUglifyCssMinifierFactory();
                options.JsMinifierFactory = new NUglifyJsMinifierFactory();
            }).AddHttpCompression(options => {
                options.CompressorFactories = new List<ICompressorFactory> {
                    new BuiltInBrotliCompressorFactory( new BuiltInBrotliCompressionSettings{ Level= CompressionLevel.Fastest}),
                    new DeflateCompressorFactory(new DeflateCompressionSettings { Level = CompressionLevel.Fastest }),
                    new GZipCompressorFactory(new GZipCompressionSettings { Level = CompressionLevel.Fastest })
                };
            });

app.UseWebMarkupMin();
        public IButtonPass ButtonPass { get; private set; }
        public int ProjectId { get; private set; }
        public int AppId { get; private set; }
        public async Task<IActionResult> OnGetAsync(int pid = 0, int id = 0)
        {
            if (pid <= 0 || id <= 0) { return Redirect(UrlSetting.MemberNotFoundUrl); }
            if (MemberDto.AllowProject(pid) == false) { return Redirect(UrlSetting.MemberNotFoundUrl); }

            ProjectId = pid;
            AppId = id;
            ButtonPass = await _application.GetButtonPassByMemberId(MemberDto.Id, ViewData["MenuCode"].ToString());

            return Page();
        }

WebMarkupMin. AspNetCore6 and WebMarkupMin AspNetCore7 is the same error

brunobritodev commented 1 year ago

We are facing the same problem.

Taritsyn commented 1 year ago

Hello!

Thanks for information! Try updating to version 2.13.3.

brunobritodev commented 1 year ago

Hi @Taritsyn thx for your efforts and support.

I've updated to latest version and the error changed. Now we are getting some blank screens. I've no clue which info to send you. Some Views work fine and others don't.

I see some changes in code to prevent the _compressionStream from null at BodyWrapperStreamBase, This was the problematic line.

This problem started when we upgraded from net6.0 to net7.0.

But there is an workaround:

Our code

builder.Services
            .AddWebMarkupMin(options =>
            {
                options.AllowMinificationInDevelopmentEnvironment = true;
                options.AllowCompressionInDevelopmentEnvironment = true;  
            })
            .AddHtmlMinification()
            .AddXmlMinification()
            .AddHttpCompression(options => { options.ExcludedPages = new List<IUrlMatcher> { new WildcardUrlMatcher("/health*") }; });

If I change to this:

builder.Services
            .AddWebMarkupMin(options =>
            {
                options.AllowMinificationInDevelopmentEnvironment = true;
                options.AllowCompressionInDevelopmentEnvironment = true;  
            })
            .AddHtmlMinification(options =>
            {
                HtmlMinificationSettings settings = options.MinificationSettings;
                settings.RemoveRedundantAttributes = true;
                settings.RemoveHttpProtocolFromAttributes = true;
                settings.RemoveHttpsProtocolFromAttributes = true;

                options.CssMinifierFactory = new NUglifyCssMinifierFactory();
                options.JsMinifierFactory = new NUglifyJsMinifierFactory();
            })
            .AddXmlMinification()
            .AddHttpCompression(options => { options.ExcludedPages = new List<IUrlMatcher> { new WildcardUrlMatcher("/health*") }; });

It start to work again.

Taritsyn commented 1 year ago

Hello, Bruno!

Today I fixed another small defect and released version 2.13.4.

… Now we are getting some blank screens. … … This problem started when we upgraded from net6.0 to net7.0.

Prior to version 2.13.0, as a rule, such errors occurred for 2 reasons: due to outdated implementation of the BodyWrapperStreamBase class and peculiarities of disposing an empty instance of the BrotliStream class. Now, in most cases, such errors no longer occur (“[WebMarkupMin.AspNetCore5] Sometimes a blank page is returned as the request has no response data available” and “Blank page response”). It is quite possible that you are using an extension focused on old version of ASP.NET Core and you should replace it by the WebMarkupMin.AspNetCore7 package.

… Some Views work fine and others don't.

It is possible that some Views contain HTML code with syntax errors. Therefore, I recommend you to write your own logger or use the built-in logger (ThrowExceptionLogger class). In your case, the logger for WebMarkupMin needs to be registered in the Program.cs file as follows:

…
using IWmmLogger = WebMarkupMin.Core.Loggers.ILogger;
using WmmThrowExceptionLogger = WebMarkupMin.Core.Loggers.ThrowExceptionLogger;
…

var builder = WebApplication.CreateBuilder(args);
…

// Override the default logger for WebMarkupMin.
builder.Services.AddSingleton<IWmmLogger, WmmThrowExceptionLogger>();
…