Skip to content

Commit a7b4a42

Browse files
committed
Support configuring the MaxRequestBodySize for the upload endpoint
1 parent b3e9397 commit a7b4a42

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

service/Core/Configuration/ServiceConfig.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ public class ServiceConfig
1212
/// </summary>
1313
public bool RunWebService { get; set; } = true;
1414

15+
/// <summary>
16+
/// The maximum allowed size in bytes for a request body posted to the upload endpoint.
17+
/// If not set, the default ASP.NET Core limit of 30 MB (~28.6 MiB) is applied.
18+
/// </summary>
19+
public long? MaxUploadRequestBodySize { get; set; } = null;
20+
1521
/// <summary>
1622
/// Whether to run the asynchronous pipeline handlers
1723
/// Use these booleans to deploy the web service and the handlers on same/different VMs

service/Service.AspNetCore/WebAPIEndpoints.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
using System.Threading.Tasks;
88
using Microsoft.AspNetCore.Builder;
99
using Microsoft.AspNetCore.Http;
10+
using Microsoft.AspNetCore.Http.Features;
1011
using Microsoft.AspNetCore.Http.HttpResults;
1112
using Microsoft.AspNetCore.Mvc;
1213
using Microsoft.AspNetCore.Routing;
1314
using Microsoft.Extensions.Logging;
15+
using Microsoft.KernelMemory.Configuration;
1416
using Microsoft.KernelMemory.Context;
1517
using Microsoft.KernelMemory.DocumentStorage;
1618
using Microsoft.KernelMemory.Service.AspNetCore.Models;
@@ -21,10 +23,11 @@ public static class WebAPIEndpoints
2123
{
2224
public static IEndpointRouteBuilder AddKernelMemoryEndpoints(
2325
this IEndpointRouteBuilder builder,
26+
ServiceConfig serviceConfig,
2427
string apiPrefix = "/",
2528
IEndpointFilter? authFilter = null)
2629
{
27-
builder.AddPostUploadEndpoint(apiPrefix, authFilter);
30+
builder.AddPostUploadEndpoint(apiPrefix, authFilter, serviceConfig.MaxUploadRequestBodySize);
2831
builder.AddGetIndexesEndpoint(apiPrefix, authFilter);
2932
builder.AddDeleteIndexesEndpoint(apiPrefix, authFilter);
3033
builder.AddDeleteDocumentsEndpoint(apiPrefix, authFilter);
@@ -37,7 +40,7 @@ public static IEndpointRouteBuilder AddKernelMemoryEndpoints(
3740
}
3841

3942
public static void AddPostUploadEndpoint(
40-
this IEndpointRouteBuilder builder, string apiPrefix = "/", IEndpointFilter? authFilter = null)
43+
this IEndpointRouteBuilder builder, string apiPrefix = "/", IEndpointFilter? authFilter = null, long? maxRequestBodySize = null)
4144
{
4245
RouteGroupBuilder group = builder.MapGroup(apiPrefix);
4346

@@ -49,6 +52,13 @@ public static void AddPostUploadEndpoint(
4952
IContextProvider contextProvider,
5053
CancellationToken cancellationToken) =>
5154
{
55+
if (maxRequestBodySize is not null
56+
&& request.HttpContext.Features.Get<IHttpMaxRequestBodySizeFeature>() is { } feature)
57+
{
58+
log.LogTrace("Max upload request body size set to {0} bytes", maxRequestBodySize);
59+
feature.MaxRequestBodySize = maxRequestBodySize;
60+
}
61+
5262
log.LogTrace("New upload HTTP request, content length {0}", request.ContentLength);
5363

5464
// Note: .NET doesn't yet support binding multipart forms including data and files

service/Service/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public static void Main(string[] args)
138138
.Produces<ProblemDetails>(StatusCodes.Status403Forbidden);
139139

140140
// Add HTTP endpoints using minimal API (https://learn.microsoft.com/aspnet/core/fundamentals/minimal-apis)
141-
app.AddKernelMemoryEndpoints("/", authFilter);
141+
app.AddKernelMemoryEndpoints(config.Service, "/", authFilter);
142142

143143
// Health probe
144144
app.MapGet("/health", () => Results.Ok("Service is running."))

service/Service/appsettings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
// Whether to run the web service that allows to upload files and search memory
4343
// Use these booleans to deploy the web service and the handlers on same/different VMs
4444
"RunWebService": true,
45+
// The maximum allowed size in bytes for a request body posted to the upload endpoint
46+
// If not set, the default ASP.NET Core limit of 30 MB (~28.6 MiB) is applied
47+
"MaxUploadRequestBodySize": null,
4548
// Whether to expose OpenAPI swagger UI at http://127.0.0.1:9001/swagger/index.html
4649
"OpenApiEnabled": false,
4750
// Whether to run the asynchronous pipeline handlers

0 commit comments

Comments
 (0)