1

I need to upload large files using a ServiceStack Service, hosted on an AspNetCore 5.0 application. Attempting to usethe AspNetMvc attribute doesn't work.

[Route("/api/tehformz", "POST")]
public class BigFileUploadRequest {
  public string Name { get; set; }
  public string Description { get; set; }
  public string DeviceTypeId { get; set; }
}

[RequestSizeLimit(300 * 1024 * 1024)]

public ResponseStatus Post(BigFileUploadRequest request) {
...
}

I've tried adding a MiddleWare handler like below. It is executed, but not honored.

private async Task FallbackMiddlewareHandler(HttpContext httpContext, Func<Task> next)
{
            httpContext.Features.Get<IHttpMaxRequestBodySizeFeature>().MaxRequestBodySize = null;
            await next();
}

It might be worth noting I am posting the file as form data, and it works fine for smaller uploads.

How do I update my middle ware or decorate my service call so I can upload files larger than AspNetCore's ~30MiB limit?

enter image description here

jklemmack
  • 3,518
  • 3
  • 30
  • 56
  • can you paste definition for `BigFileUploadRequest`? is it streaming? – Lei Yang Mar 30 '22 at 15:03
  • This is not a "streaming" request. `BigFileUploadRequest` is an arbitrary POCO used to get the form data. It is irrelevant, – jklemmack Mar 30 '22 at 17:13
  • while setting max messages size possible, it is always suggested to streaming upload large files(objects, bytes) and consider the possibility of continue after networking interrupting. – Lei Yang Mar 30 '22 at 23:46
  • @LeiYang Without going on to a diatribe, you're saying "you're doing it wrong". You answer doesn't take in to account the context of it being an internal, local management API; nor the the use of middleware (ServiceStack); nor the fact that I have post data. You're saying I need to use my limited time to rewrite the application if I want to upload a ... say... 35MiB file. – jklemmack Mar 31 '22 at 15:04
  • Does this answer your question? [Increase upload file size in Asp.Net core](https://stackoverflow.com/questions/38698350/increase-upload-file-size-in-asp-net-core) – Lei Yang Mar 31 '22 at 15:11
  • i cannot imagin a so user with so many scores cannot use search, there are many similiar questions and answers out there. i've seen at least 3 kind of solutions, but you say you only tried one or two. – Lei Yang Mar 31 '22 at 15:15
  • @LeiYang Perhaps this is better in discussion, but I *did* in fact try 2 of the 3 solutions in the solution you linked. In fact, in the question itself I showed attempts to use both `RequestSizeLimit` (I'm not using AspNetMVC) and `IHttpMaxSizeRequestBodySizeFeature` based options (which I stated mysteriously didn't work). The last required hosting-specific solutions (Kestrel or HttpSys) which I don't feel comfortable using. – jklemmack Mar 31 '22 at 20:17

1 Answers1

2

Adding this to the ConfigureServices method of my StartUp seems to work. Ideally I'd have a per-endpoint setting, but this is sufficient for now.

// Set to allow 300MiB files for device software uploads
services.Configure<FormOptions>(options =>
{
    options.MultipartBodyLengthLimit = 300 * 1024 * 1024;
});
Nino van der Mark
  • 622
  • 1
  • 9
  • 19
jklemmack
  • 3,518
  • 3
  • 30
  • 56