'nullrefferenceException in method app.UseExceptionHandler()

After update solution to .Net6.0(from netcorre3.1) i have problem with method UseExceptionHandler always is nullrefferenceException.

public void Configure(IApplicationBuilder app)
    {
        app.Requires(nameof(app)).IsNotNull();

        if (env.EnvironmentName == "Development")
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseHsts();
        }

        app.UseExceptionHandler(appError =>//this line causes error
        {
            appError.Run(async context =>
            {
                context.Response.Headers.Add("Access-Control-Allow-Origin", "*");
                context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                context.Response.ContentType = "application/json";

                var contextFeature = context.Features.Get<IExceptionHandlerFeature>();
                if (contextFeature != null)
                {
                    var message = $"Internal Server Error - {Guid.NewGuid()}";
                    errorLogger.LogCritical(contextFeature.Error, message);

                    await context.Response.WriteAsync(JsonConvert.SerializeObject(new { StatusCode = context.Response.StatusCode, Message = message }));
                }
            });
        });
    }

Error occurs even if i use app.UseExceptionHandler() //without arguments. the details of the exception say little:

System.NullReferenceException
  HResult = 0x80004003
  Message = Object reference not set to an instance of an object.
  Source = <Unable to evaluate source of the exception>
  Stack trace:
<Unable to evaluate trace data>

  This exception was originally thrown on this call stack:
    [External code]
    XXXX.YYYYYY.Infrastructure.Common.Startup.ExceptionHandling.CustomExceptionHandlingStartup.Configure (Microsoft.AspNetCore.Builder.IApplicationBuilder) in CustomExceptionHandlingStartup.cs


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source