Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
548 views
in Technique[技术] by (71.8m points)

c# - 504 Gateway Timeout in dotnet core 3.1

I have created a React Project using dotnetcore 3.1 Version. This is hosted on an IIS Server on AWS Lightsail. We use AWS Lightsail Loadbalancers. The Web Service communicates to an Microsoft SQL Server Express (64-bit) Database, version 14.0.3281.6 using Entity Framework Core.

The problem we are facing is:

We make a call to the webservice via a POST request. This runs a query on the database. This query fetches data from many related tables using Include()

For large data we have noticed that the web service return a 504 Gateway Timeout.

We have tried setting the CommandTimeout to 900 seconds as below

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                IConfigurationRoot configuration = new ConfigurationBuilder()
                   .SetBasePath(Directory.GetCurrentDirectory())
                   .AddJsonFile("appsettings.json")
                   .Build();
                var connectionString = configuration.GetConnectionString("DefaultConnection");
                optionsBuilder.UseSqlServer(connectionString, sqlServerOptions => sqlServerOptions.CommandTimeout(900));
            }
        }

Our Connection string

"DefaultConnection": "Data Source=server_name_here,port_number_here;Initial Catalog=db_name_here;Integrated Security=False;Persist Security Info=False;User ID=user_name_here;Password=password_here

Other things we have tried: Setting the requestTimeout="00:20:00" in the web.config

Application Pool settings screenshot

enter image description here

Are we missing something?


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

1 Reply

0 votes
by (71.8m points)

I suspect that this has something to do with the AWS loadbalancer configuration. The 504 Gateway timeout probably comes from the loadbalancer because it expects your application to answer within a certain amount of time (which it doesn't for large data).

You could try to (temporarily) disable the loadbalancer and see if you still hit the 504 Gateway Timeout error.

You could also add a new controller to test this issue:

[ApiController]
[Route("[controller]")]
public class TimeoutController : ControllerBase
{
    [HttpGet]
    public IActionResult Get(int timeoutInSeconds)
    {
        Thread.Sleep(TimeSpan.FromSeconds(timeoutInSeconds));
        return Ok($"Waited for {timeoutInSeconds} seconds.");
    }
}

Then you can call it with your browser on http://[server:port]/timeout?timeoutInSeconds=10

enter image description here

You can then gradually increase the timeout. This helps you to identify the threshold and to prove that it does not have something to do with your application code (because the controller does not do anything but wait).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...