Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to configure my .net core API in order to limit the requests.

To achieve this i modify the program.cs class like

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();            
    }


    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.ConfigureKestrel(serverOptions =>
                {
                    serverOptions.Limits.MaxConcurrentConnections = 2;
                })
                //.UseContentRoot(Directory.GetCurrentDirectory())
                //.UseIISIntegration()
                .UseStartup<Startup>();
            });
}

but the problem is when I call my API with a console app using more threads than 2 I get the responses from all threads-calls. The API I have deployed-published it in my local IIS pc.

I consider that I must get only 2 responses and then for the other calls I will get 503 services unavailable.

what is wrong with my code?

EDIT I have read this article How to configure concurrency in .NET Core Web API?, the problem is when I add the web.config

 <configuration>
    <system.web>
        <applicationPool
            maxConcurrentRequestsPerCPU="5000"
            maxConcurrentThreadsPerCPU="0"
            requestQueueLimit="5000" />
    </system.web>
</configuration>

i have the warning

the element system. web has invalid child element applicationpool

and i cannot publish the api on iis or run it in iis express


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
535 views
Welcome To Ask or Share your Answers For Others

1 Answer

Since the API will host in the IIS, so, the configuration for the Kestrel will not be used.

To set the max concurrency connections in IIS, you could Open IIS manager window. Select the site from the server node. Then, select Advance setting from the action pane. You can see the Limits under the Behavior section. Set Maximum Concurrent Connection value based on your requirement. Like this:

enter image description here

[Note] This setting is for all Sites.

Besides, you could also check this sample and create a custom middleware to limit the request.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...