Search code examples
c#asp.net-core-webapi.net-8.0

How can you programmatically insert "requestTimeout="00:30:00" via program.cs, rather than web.config


I have an ASP.NET Core 8 Web API that gets published to IIS on multiple dedicated customer web servers.

Is there a way to insert the:

<aspNetCore requestTimeout="00:30:00".../>

in program.cs?

I tried doing a timeout middleware but it appeared to have ignored the value of the timeout set on the controller, but when I added the

requestTimeout="00:30:00"

in the web.config, that one worked. I want to know if I can do it in program.cs as I do not want to have to have the customer inserting it manually into web.config.

Thanks


Solution

  • You can do this via filter attribute decorated over your API,or you can configure you middle ware with request timeout policy

    Decorate over API endpoint [RequestTimeout(milliseconds: 2000)]

    Through the middleware

    `builder.Services.AddRequestTimeouts(options => {
        options.DefaultPolicy =
            new RequestTimeoutPolicy { Timeout = TimeSpan.FromMilliseconds(1500) };
        options.AddPolicy("MyPolicy", TimeSpan.FromSeconds(2));
    });`
    

    Please refer the link enter link description here