Open Closed

Abp Studio Middleware Problem When try to flush outputstream (Response.Body.OutputStream) #8570


User avatar
0
cangunaydin created
  • ABP Framework version: v9.0.2
  • UI Type: Angular
  • Database System: EF Core (PostgreSQL)
  • Tiered (for MVC) or Auth Server Separated (for Angular): yes

Hello, When i try to serve a file with chunks, i am getting an error from abp studio middleware. here is my code.

    [HttpGet]
    [Route("download")]
    public async Task DownloadAsync([FromQuery(Name = "id")]  Guid? id,[FromQuery(Name = "tenantId")]  Guid? tenantId)
    {
        Check.NotNull(id, nameof(id));
        Check.NotNull(tenantId, nameof(tenantId));
        var file = await _fileAppService.GetAsync(id.Value, tenantId.Value);
        HttpContext.Response.ContentType = file.MimeType;
        await HandleContentDownload(file, 0, file.Size);
    }

    private async Task HandleContentDownload(FileDto file, long startOffset, long length)
    {
        using (CurrentTenant.Change(file.TenantId))
        {
            var tempOffset = (int)startOffset;
            var tempLength = length;
            var chunkLength = 1024 * 1024; //1mb

            var outputStream = HttpContext.Response.Body;
            while (tempOffset < tempLength)
            {
                var remainingLength = tempLength - tempOffset;
                if (remainingLength < chunkLength)
                {
                    chunkLength = (int)(tempLength - tempOffset);
                }

                await using (var stream = await _fileAppService.DownloadAsync(file.Id, tempOffset, chunkLength))
                {
                    await outputStream.WriteAsync((stream as MemoryStream)?.ToArray());
                }

                tempOffset += chunkLength;
            }

            await outputStream.FlushAsync();
        }
    }

i am getting this error.

System.InvalidOperationException: Writing to the response body is invalid for responses with status code 204.
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.FirstWriteAsyncInternal(ReadOnlyMemory`1 data, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.WritePipeAsync(ReadOnlyMemory`1 data, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpResponseStream.WriteAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)
   at System.IO.MemoryStream.CopyToAsync(Stream destination, Int32 bufferSize, CancellationToken cancellationToken)
   at System.IO.Stream.CopyToAsync(Stream destination)
   at Volo.Abp.Studio.Client.AspNetCore.AbpStudioMiddleware.InvokeAsync(HttpContext context, RequestDelegate next)
   at Volo.Abp.Studio.Client.AspNetCore.AbpStudioMiddleware.InvokeAsync(HttpContext context, RequestDelegate next)
   at Volo.Abp.Studio.Client.AspNetCore.AbpStudioMiddleware.InvokeAsync(HttpContext context, RequestDelegate next)
   at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.InterfaceMiddlewareBinder.<>c__DisplayClass2_0.<<CreateMiddleware>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at Volo.Abp.AspNetCore.Mvc.Libs.AbpMvcLibsService.<CheckLibs>b__1_0(HttpContext httpContext, RequestDelegate next)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)

what can be done to fix the problem? any idea? somehow can i leave this method out of AbpStudioMiddleware?


3 Answer(s)
  • User Avatar
    1
    maliming created
    Support Team Fullstack Developer

    hi

    Can you try this? I will fix this in Framework. Your ticket has been refunded.

    context.Services.PostConfigure<MvcOptions>(mvcOptions =>
    {
        mvcOptions.Filters.RemoveAll(x => x is ServiceFilterAttribute serviceFilterAttribute && serviceFilterAttribute.ServiceType == typeof(AbpNoContentActionFilter));
    });
    
  • User Avatar
    0
    cangunaydin created

    Hello again, i have tried your suggestion it works but it throws error whenever i try to write to output stream.

    System.ObjectDisposedException: Cannot access a closed Stream.
       at System.IO.MemoryStream.CopyToAsync(Stream destination, Int32 bufferSize, CancellationToken cancellationToken)
       at System.IO.AbpStreamExtensions.CreateMemoryStreamAsync(Stream stream, CancellationToken cancellationToken)
       at System.IO.AbpStreamExtensions.GetAllBytesAsync(Stream stream, CancellationToken cancellationToken)
       at Volo.Abp.Studio.Client.AspNetCore.AbpStudioMiddleware.InvokeAsync(HttpContext context, RequestDelegate next)
       at Volo.Abp.Studio.Client.AspNetCore.AbpStudioMiddleware.InvokeAsync(HttpContext context, RequestDelegate next)
       at Volo.Abp.Studio.Client.AspNetCore.AbpStudioMiddleware.InvokeAsync(HttpContext context, RequestDelegate next)
       at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.InterfaceMiddlewareBinder.<>c__DisplayClass2_0.<<CreateMiddleware>b__0>d.MoveNext()
    --- End of stack trace from previous location ---
       at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)
    

    so it throws many of these whenever i tried to serve a file with big size as you can imagine. I want to ask when the new version is going to be released without the error. And what is this middleware used for? is it for telemetry? if i do not use the middleware what am i missing?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    System.ObjectDisposedException: Cannot access a closed Stream.

    Can you share a project to reproduce this?

    liming.ma@volosoft.com


    if i do not use the middleware what am i missing?

    This middleware will provide information to the Studio IDE. If you have a problem, you can remove it until we fix it.

    Thanks.

Made with ❤️ on ABP v9.1.0-preview. Updated on January 02, 2025, 07:06