-
Notifications
You must be signed in to change notification settings - Fork 10.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Read additional KestrelServerOptions from config #4765
Comments
If anyone wants to set limits from config today, it can be done with a few extra lines of code:
|
|
You're right. I'm not using it because it's too much magic for my taste but other peoples mileage may vary. |
Any progress on issue? Will you plan to add this to |
Is there any specific fields you're interested in? We haven't seen much demand. |
Currently we tweaked two properties: MinRequestBodyDataRate
MaxRequestBodySize It is reasonable to allow configure all listed settings. Who knows where it applicable? For example, if some microservice is published, there is practically no options for maintainers of running service to change those options, if circumstances require it. |
BTW, I could not find generic method UseKestrel((builderContext, options) =>
{
var kestrelSection = builderContext.Configuration.GetSection(KestrelSectionName);
options.Configure(kestrelSection);
var kestrelOptions = kestrelSection.Get<KestrelServerOptions>();
if (kestrelOptions != null)
{
options.AddServerHeader = kestrelOptions.AddServerHeader;
options.AllowSynchronousIO = kestrelOptions.AllowSynchronousIO;
options.ApplicationSchedulingMode = kestrelOptions.ApplicationSchedulingMode;
foreach (var property in typeof(KestrelServerLimits).GetProperties())
{
var value = property.GetValue(kestrelOptions.Limits);
property.SetValue(options.Limits, value);
}
}
}); |
I could really use this. Currently on Azure App Service with ASP .NET Core 2.2, you will get a Quite critical in my opinion. |
@ffMathy do you have more information on that? It's not clear why setting AddServerHeader would be a problem or how this feature would address that. |
+1. |
The Kestrel documentation suggests that we can now set options on |
As an aside, are there any plans to add some validation attributes to the |
Those kestrel docs show how to read most options using the standard Options binder. We know that works for primitives like bool and int, but we should check how well it handles enums, MinDataRate, etc. before closing this. |
Since MinDataRate has no settable properties - they will not be filled from configuration, because
At lease properties for MinDataRate must have Also I didn't find any notable Even @RehanSaeed mentioned that we can now set options on |
Kestrel has a custom config binder that's primarily used for endpoints. Additional complex settings like MinDataRate should be added there, not in CreateDefaultBuilder. |
What is the status of
It looks like in the 5.0 docs this has been removed again and we're not able to use appsettings-configuration anymore and we need to use the workaround again. Why is that the case? Is there a solution (except the Reflection-based-workaround posted above)? Best regards, |
The 5.0 docs were reorganized. Check here for a example showing KestrelServerOptions from config: |
Could you also elaborate a tiny bit on why this is not done automatically by the default webhost builder? Why would I not want to have these options configurable? |
This issue tracks just that, reading more from config by default. My explanation above was the historical reasoning for why we enabled endpoint config first.
I don't quite follow, most Options types are not bound to config by default, you do that binding in Startup.ConfigureServices or similar. Logging is the main exception I can think of where the binding is always done for you. |
Are we able to set Also, can |
Yes in 5.0. The docs for that are still in progress. See #26246.
Not today, and I don't know of any issue tracking it. Is this one that you commonly enable in production? We've mainly seen it used in development environments. The trick with UseConnectionLogging is that there are multiple places in the pipeline it could be put and you'd get different results. E.g. it could be put before or after HTTPS decryption. Both are valid for investigating different scenarios. |
Bumping into .NET 7 to reconsider |
Thanks for contacting us. We're moving this issue to the |
In
In
Feel free to add more custom values and handle them using a switch case in your logic. Another improvement I will be doing as the second iteration is to create an extention function for webBuilder and move all the logiv away from I will post the logic I create in the extention here, @microsoft can feel free to finetune it and ship it as an extention in .net8 |
Can someone from Microsoft please, please, please explain to me why the MSDN docs say:
yet this literally does not happen and we still, in 2023, have to use the reflection code posted here for Kestrel options to be read from configuration? |
Would be nice if the official docs remarked more accurately on the current state of this. |
A simplified version without needs of doing reflection by yourself: builder.WebHost.ConfigureKestrel(options =>
{
var kestrelSection = builder.Configuration.GetSection("Kestrel");
options.Configure(kestrelSection);
kestrelSection.Bind(options);
}); |
These docs include this example:
{
"Kestrel": {
"Limits": {
"MaxConcurrentConnections": 100,
"MaxConcurrentUpgradedConnections": 100
},
"DisableStringReuse": true
}
} Yet none of these settings actually bind by default. I just wasted a bunch of time trying to set Max Request Body/Line size settings from configuration which of course wasn't working. I tried the example above exactly and verified that even those don't load. I chased down the code in this repo and it looks like most configuration loading is still w/r/t ListenOptions and endpoints |
Presently, they can't conveniently be configured from appsettings.json. There are [workarounds](dotnet/aspnetcore#4765), but none that seem worth recommending. Fixes #31108
I'd love to see this too. Today a customer ran into a maximum file upload size issue. If this had been working, I could have seamlessly fixed this in a minute. Now I have to deploy a new production version of the API. UPDATE: The code by @hez2010 works very nicely. Now we don't need to a code change if we need to alter these parameters again. I made one change, I used the overload of Host
.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults((webBuilder) => webBuilder
.UseStartup<Foo>()
.ConfigureKestrel((context, options) =>
{
var kestrelSection = context.Configuration.GetSection("Kestrel");
options.Configure(kestrelSection);
kestrelSection.Bind(options);
})
)
.Build()
.Run(); |
This is an important scenario for us when deploying apps in customers environment. Specifically, enabling connection logging/tracing in a running app. Could we see a change of direction here for NET 9 where appsettings is a first class citizen? |
As for file uploading (through multipart-form), it is limited by another options called |
Good to know. We didn't run into this in our case, since it's a ReST-ish POST to our web API where the file upload data is the request body itself and the necessary metadata is part of the URI. |
The new configuration APIs will read endpoints with their certificates and the default certificate. aspnet/KestrelHttpServer#2186
Here are other KestrelServerOptions to consider reading from config (Unsorted):
HttpProtocols Protocolsf38f60fToday these must all be set in code in parallel to the configuration.
The text was updated successfully, but these errors were encountered: