UploadOperation timeout restrictions - c#

UploadOperation has next time restrictions:
1.) When establishing a new connection for an upload over TCP/SSL, the connection attempt is aborted if not established within five minutes.
2.) After the connection has been established, an HTTP request message that has not received a response within two minutes is aborted.
Is it normal for POST and PUT requests to limit them in time(if I want to upload big file)?
Is there a way to overwrite that "two minutes" value to allow longer requests?

The 0x80072EE2 exception is ERROR_INTERNET_TIMEOUT. That happens because the server has not read data from the request in 2 minutes, or because the server has not sent response data in 2 minutes.
If the server keeps reading or writing data constantly, the connection does not timeout.

Related

C# HttpClient Multiple Get Requests at the same time giving System.Net.Sockets.SocketException (10060) Exception

As Suggested by #John Wu's Answer on Stackoverflow. I am using the following code to make concurrent HTTP Get Requests on the same host for sending SMS
public async Task SendBulk(List<string> recipentURLs)
{
using (var client = new HttpClient())
{
//Start requests for all of them
var requests = recipentURLs.Select(url => client.GetAsync(url)).ToList();
//Wait for all the requests to finish
await Task.WhenAll(requests);
}
}
Now I am having the exception saying
System.Net.Http.HttpRequestException: An error occurred while sending the request.
---> System.IO.IOException: Unable to read data from the transport connection: 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..
---> 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.
The API I am sending requests to doesn't require a response (as I am only using it to send sms).
I am using this in a backgroud job by using HangFire. So I am okay if it takes a long time.
What I am currently looking is an efficient way to send multiple HTTP Get Requests at the Same host.
How can I safely handle all these concurrent requests and retry if any of it fails?
Also I am currently researching that due HTTP Throttling I cannot use 2 connection to the same host?
Closed HttpClient instances leave sockets open in the TIME_WAIT state for a short period of time. If a code path that creates and disposes of HttpClient objects is frequently used, the app may exhaust available sockets. You should use HttpClientFactory to avoid that.
See the post Make HTTP requests using IHttpClientFactory in ASP.NET Core.

Disable Kestrel (dotnet asp.net core server) request queuing

Kestrel (dotnet asp.net core server) is queuing requests if too many requests are hit at one time. I want it to throw a 503 than queue instead to avoid timeout. We have
.UseKestrel(options => { options.Limits.MaxConcurrentConnections = 100; })
But if more than 100 requests it would still queue up, and some requests just timeout.
MaxConcurrentConnections property specifies the number of connections the Kester Server can accept before it starts rejecting the connections.
So, in other words, MaxConcurrentConnections specifies the queue length. In the above example, it will start to drop if it accepted 100 requests and processing them.
https://github.com/aspnet/AspNetCore/blob/b31bdd43738a55e10bb38336406ee0db56c66b44/src/Servers/Kestrel/Core/src/Middleware/ConnectionLimitMiddleware.cs#L32-L39
If your site receives less than 10 requests per second and you are processing the requests within 5 seconds, you will be good.
Also, there is no option to specify a custom HTTP error code. The TCP connection will be terminated abrubtly by the server. Youe client should detect and handle the Network Error.
Also refer this open issue: https://github.com/aspnet/AspNetCore/issues/4777

How to prevent connection time out when trying to select big data through looping in C#?

Recently,I have to develop a system which will fetch huge data from SQL Server 2008 R2. And then need to create HL7 messages using these data and send these HL7 messages to another application. I need to fetch data and create HL7 messages within looping also. The problem I am facing is that connection time out when 1 or 2 minutes after running the application.How can I prevent this problem? What should I do?
Modifying the connection string in the web.config , you could append ;Connection Timeout=30. The timeout value set in the Connection Timeout property is a time expressed in seconds.
Please Refer Connection Time out
setting the timeout value to 0, you are specifying that your attempt to connect waits an infinite time. As described in the documentation, this is something that you shouldn't set in your connection string.
A value of 0 indicates no limit, and should be avoided in a ConnectionString because an attempt to connect waits indefinitely.

HttpListener setting a total connection timeout

I am trying to control the maximum total duration of a single connection in HttpListener. I am aware of the TimeoutManager property and the 5 or so different timeout values that it contains but it is unclear whether or not setting each of those values will add up to the total places where delay may occur in a connection.
I am looking for something more along the lines of: "If we have a connection that lasts more than x s from the moment of opening the connection until now, abort it without sending anything else or waiting for anything else."
EDIT
To clarify, the scenario that I was experimenting with involves the server trying to send the response and the client not receiving. This causes HttpListenerResponse.OutputStream.Write() to hang indefinitely. I was trying to find a method that I can call from another thread to hard-abort the connection. I tried using OutputStream.Close() and got Cannot Close Stream until all bytes are written. I also tried HttpListenerResponse.Abort() which produced no visible effect.
None of those properties will do what you want. HttpListener is intended to control the request flow, incomming and outgoing data, so it doesn't handle the time between when the response has been fully received and when you send a response, it's your responsability to take care of it.
You should create your own mechanism to abort the request if the total time is higer than the desired one, just a timer can be enough, when a new connection is created enqueue a timer with the total timeout as expiring time, if the request ends before the timer expires cancel the timer, else the timer aborts the request.

Fail over subscribe with SipSorcery

How is it possible to create a new subscription using SipSorcery, if your TCP connection died within the expiry timer of your initial subscription?
Write now I'm having a expiry timer running, and when it elapse, I'm checking if the connection is established like this:
while (!tcpChannel.IsConnectionEstablished(myRemoteEndpoint))
{
//... using same from tag, but creating new call id saved as SIPRequest _request...
System.Threading.Thread.Sleep(1000 * 60);
tcpChannel.Send(myRemoteEndpoint, Encoding.UTF8.GetBytes(_request.ToString());
}
The idea was to wait 60 seconds, then try to send a new SUBSCRIBE to the server, check if the connection is established, if not run again after 60 senconds, until the connection is established.
But the .IsConnectionEstablished seems a little unreliable to this purpose... Its like the while loop blocking for something. I can see that my SUBSCRIBE request has been sended, but I ain't receiving any response on that request.
Any ideas are appriciated.
You shouldn't need to do the IsConnectionEstablished check. When you call tcpChannel.Send it will take care of establishing a new TCP connection to the required end point if one is not available.
As to why you are not receiving a response to your subsequent SUBSCRIBE requests if you are re-sending the same request repeatedly without updating the required headers such as CSeq, Via branchid, Call-ID & from tag then it's probably getting flagged as a duplicate request.
Also you may want to have a look SIPSorcery.SIP.App.SIPNotifierClient as it is designed to maintain a subscription with a SIP server.

Categories

Resources