Timeout when reaching to server after certain time of inactivity

Hi, we are currently developing a Web Application using C# and Blazor (Dot net 8) with Mudblazor (6.11.1). We have a page which requests data from a MongoDb Server (Mongo Atlas ReplicaSet.) and displays it in our application. For this we are using the MongoDb Driver 2.22.0. The application consists of the following:

The application is hosted in Azure in the App Services Resource. The error prompts when the application tries to request the data after certain time of inactivity and in the console shows the following error:

MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server.
 ---> System.Net.Sockets.SocketException (10060): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)
   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token)
   at System.Threading.Tasks.ValueTask.ValueTaskSourceAsTask.<>c.<.cctor>b__4_0(Object state)
--- End of stack trace from previous location ---
   at MongoDB.Driver.Core.Connections.TcpStreamFactory.ConnectAsync(Socket socket, EndPoint endPoint, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Connections.TcpStreamFactory.CreateStreamAsync(EndPoint endPoint, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Connections.SslStreamFactory.CreateStreamAsync(EndPoint endPoint, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken cancellationToken)
   at MongoDB.Driver.Core.ConnectionPools.ExclusiveConnectionPool.PooledConnection.OpenAsync(CancellationToken cancellationToken)
   at MongoDB.Driver.Core.ConnectionPools.ExclusiveConnectionPool.ConnectionCreator.CreateOpenedInternalAsync(CancellationToken cancellationToken)
   at MongoDB.Driver.Core.ConnectionPools.ExclusiveConnectionPool.ConnectionCreator.CreateOpenedOrReuseAsync(CancellationToken cancellationToken)
   at MongoDB.Driver.Core.ConnectionPools.ExclusiveConnectionPool.AcquireConnectionHelper.AcquireConnectionAsync(CancellationToken cancellationToken)
   at MongoDB.Driver.Core.ConnectionPools.ExclusiveConnectionPool.AcquireConnectionAsync(CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Servers.Server.GetChannelAsync(CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.RetryableReadContext.InitializeAsync(CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.RetryableReadContext.CreateAsync(IReadBinding binding, Boolean retryRequested, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.AggregateOperation`1.ExecuteAsync(IReadBinding binding, CancellationToken cancellationToken)
   at MongoDB.Driver.OperationExecutor.ExecuteReadOperationAsync[TResult](IReadBinding binding, IReadOperation`1 operation, CancellationToken cancellationToken)
   at MongoDB.Driver.MongoCollectionImpl`1.ExecuteReadOperationAsync[TResult](IClientSessionHandle session, IReadOperation`1 operation, ReadPreference readPreference, CancellationToken cancellationToken)
   at MongoDB.Driver.MongoCollectionImpl`1.AggregateAsync[TResult](IClientSessionHandle session, PipelineDefinition`2 pipeline, AggregateOptions options, CancellationToken cancellationToken)
   at MongoDB.Driver.MongoCollectionImpl`1.UsingImplicitSessionAsync[TResult](Func`2 funcAsync, CancellationToken cancellationToken)
   at USS.CustomerPortal.PersistenceSteelCommand.Repositories.SaleOrderRepository.GetbyId(String customerNumber, String id) in D:\a\UssCustomerPortal\UssCustomerPortal\USS.CustomerPortal.PersistenceSteelCommand\Repositories\SaleOrderRepository.cs:line 119
   at USS.CustomerPortal.Application.Features.SaleOrders.Handlers.Queries.GetSaleOrderIdRequestHandler.Handle(GetSaleOrderIdRequest request, CancellationToken cancellationToken) in D:\a\UssCustomerPortal\UssCustomerPortal\USS.CustomerPortal.Application\Features\SaleOrders\Handlers\Queries\GetSaleOrderIdRequestHandler.cs:line 24
   at USS.CustomerPortal.Server.Pages.CustomerUser.SaleOrder.SaleOrdersList.Test_Function() in D:\a\UssCustomerPortal\UssCustomerPortal\USS.CustomerPortal.Server\Pages\CustomerUser\SaleOrder\SaleOrdersList.razor:line 253
   at USS.CustomerPortal.Server.Pages.CustomerUser.SaleOrder.SaleOrdersList.OnInitializedAsync() in D:\a\UssCustomerPortal\UssCustomerPortal\USS.CustomerPortal.Server\Pages\CustomerUser\SaleOrder\SaleOrdersList.razor:line 144
   at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState)

It takes a while until the error is prompted in the web application and after the reloading it works fine (fast). It seems that when the server does not have activity and a request is made it gives the error but after that ping to the server it kind of becomes active and the requests made work fine.

We tried to replicate the error localy ( we are connected through a vpn) and it works fine. But when it is deployed in App Services, it is working intermittently alway prompting the error in the first attemp after inactivity.

The requests are made through a pipeline using the MongoDb Driver. The connection string is the following:

mongodb+srv://xxxxxxxxx:yyyyyyyyy@zzzzzzzz.mongodb.net/?retryWrites=true&w=majority&serverSelectionTimeoutMS=3000&directConnection=true

Any suggestions or another option to diagnose this?

1 Like

I’m having a similar problem. Does anyone have any idea what’s going on here?

You are using a mongodb+srv connection string with the directConnection=true option, which is invalid. You should receive a MongoConfigurationException with the message “Direct connect cannot be used with SRV.” when parsing the connection string. (Direct connection means that you want to connect to a particular node in a replica set ignoring SDAM and server selection. This should only be used for administrative operations that require a connection to a specific node.) Please try removing directConnection=true and see if that resolves the issue.

Sincerely,
James

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.