asp.net-coreposttelerikblazor-server-sideminimal-apis

Blazor server connection disconnected error when sending Excel file as streamcontent to ASP.NET Core Minimal API endpoint


I am using Telerik Blazor TelerikFileSelect component to select an excel file and send it as a StreamContent to ASP.NET Core Minimal API endpoint. I do not see any issues when I send a 13 KB Excel file to the API.

I get this error in Blazor when I send a 377 kb Excel file:

Connection disconnected with error. Server returned an error on close: Connection closed with an error.

Here is the code that takes the file and sends it to API:

async Task OnFileSelectHandler(FileSelectEventArgs args)
{
    var uploadedFile = args.Files[0];
    var content = new MultipartFormDataContent();
    content.Add(new StreamContent(uploadedFile.Stream),"file", uploadedFile.Name);
    var response = await httpClient.PostAsync("processFile/", content);
}

The API endpoint signature looks like this, but it never gets hit for 377 KB Excel file:

endpoints.MapPost("processFile", async (RboContext dbContext, LoggerService loggerService,IFormFile file ) =>
{
}

This code is also added in Blazor side's program.cs but it's not helping:

builder.Services.AddServerSideBlazor()
    .AddHubOptions(options =>
    {
        options.MaximumReceiveMessageSize = 100 * 1024 * 1024;
        options.ClientTimeoutInterval = TimeSpan.FromMinutes(2);
        options.KeepAliveInterval = TimeSpan.FromMinutes(2);
        options.EnableDetailedErrors = true;
    });
builder.Services.Configure<FormOptions>(options =>
{
    options.MultipartBodyLengthLimit = 100 * 1024 * 1024;
});

builder.WebHost.ConfigureKestrel(options =>
{
    options.Limits.MaxRequestBodySize = 100 * 1024 * 1024;
});

I am unable to figure out what is causing the connection to disconnect?


Solution

  • Adding below code in Blazor server apps Program.cs helped solve the issue:

    builder.Services.Configure<HubOptions>(options =>
    {
        options.MaximumReceiveMessageSize = 10 * 1024 * 1024; // 10MB or use null
    });
    

    More information can be found on:

    https://www.telerik.com/blazor-ui/documentation/knowledge-base/common-increase-signalr-max-message-size

    Also the code below was already added and it didn't help:

    builder.Services.AddServerSideBlazor()
        .AddHubOptions(options =>
        {
            options.MaximumReceiveMessageSize = 100 * 1024 * 1024;
        });