System.IO.IOException: Unable to read data from the transport connection - rest

Using IIS, ASP.NET Core 2.2 and RestSharp V106.6.10 via Nuget on multiple servers getting exceptions after a period of time. My API calls with RestSharp work for a while then all of a sudden they start failing with common exception messages such as...
"ErrorException:System.Net.WebException: An error occurred while sending the request. 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.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: 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"
This occurs on multiple servers and I have yet to determine the source of the issue.
I have tried to create RestClient per call and also as a singleton with the same exception in both cases. I have also tried using syncrhonously as well as async all the way from the controller to the server call. In all cases I continue to get the above failure messages after a period of time.
Restarting the apppool does not resolve the issue. The only thing that will resolve this is a server reboot. I believe it has something to do with http client connections and reaching some limit but have yet to determine why i continue to have this issue.
Any help would be greatly appreciated. (Reference also https://github.com/restsharp/RestSharp/issues/1322)
Code currently looks like...
var defaultTimeout = 5 * 60 * 1000;
_restClientTimeout = defaultTimeout;
_restClientReadWriteTimeout = defaultTimeout;
_requestTimeout = defaultTimeout;
_client = new RestClient(_fastFieldBaseUrl);
_client.Timeout = _restClientTimeout;
_client.ReadWriteTimeout = _restClientReadWriteTimeout;
var request = new RestRequest(url, Method.POST);
request.Timeout = _requestTimeout;
request.ReadWriteTimeout = _requestTimeout;
request.AddHeader("Content-Type", "application/json");
request.AddParameter("body", json, ParameterType.RequestBody);
try
{
var response = await _client.ExecuteTaskAsync(request);
if (response.StatusCode == HttpStatusCode.Created)
{
return response.RawBytes;
}
else
{
var message= $"ExecuteTaskAsync raw bytes is null GetResponseMessage(response)}";
Log(message, traceIdentifier, null, Microsoft.Extensions.Logging.LogLevel.Error);
return null;
}
}
catch (Exception e)
{
var message = $"Error: {e.ToString()}";
Log(message, traceIdentifier, e, Microsoft.Extensions.Logging.LogLevel.Error);
}

Related

RestTemplate exchange method error: ResourceAccessException: I/O error on POST request for : Connection timed out

I have a problem consuming an REST API with RestTemplate exchange method:
I tried some other public API and it worked with the same code (just changing the URL), so I think it is not a probleme with my code but maybe with the network proxy or something else.
The error text:
org.springframework.web.client.ResourceAccessException: I/O error on POST request for : Connection timed out: connect; nested exception is java.net.ConnectException: Connection timed out: connect
RestTemplate exchange call:
ResponseEntity<FooDto> reponseEntity = restTemplate.exchange(restUrl, HttpMethod.POST, httpEntity, FooDto.class);
The way you define the rest template is crucial here. you need to set the connection time out and read time out.
It is important to optimize them to the level where it doesn't slow down your application responsiveness.
SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
// Connect timeout: time is in milliseconds
clientHttpRequestFactory.setConnectTimeout(3000);
// Read timeout: time is in milliseconds
clientHttpRequestFactory.setReadTimeout(3000);
RestTemplate template = new RestTemplate(clientHttpRequestFactory);

Getting transport error while watching the document in stitch

When I watch the certain ids in the collection using MongoDB watch I am getting error
ERROR Error: Uncaught (in promise): e: (TRANSPORT_ERROR): the request transport encountered an error communicating with Stitch: event source failed to open and will not reconnect; check network log for more details
e: (TRANSPORT_ERROR): the request transport encountered an error communicating with Stitch: event source failed to open and will not reconnect; check network log for more details
It was working till yesterday it started from today
const db = mongoClient.db(environment.databaseName);
const comments = db.collection('collectionName');
this.changeStream = await comments.watch(ids);
this.changeStream.onNext((event) => {
console.log('Watched document changed:', event);
this.fetchData();
});
I expect that whenever the document is changed in the list of ids. Change event should trigger.
This indicates that your app is unable to communicate with the stitch backend, probably because of not internet or poor network

QuickFixN has session disconnection event that doesn't call OnLogout

In the QF event log there are session layer events:
20180418-13:30:51.268 : Connection failed: 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 #.#.#.#:#
20180418-13:31:00.288 : Connecting to #.#.#.# on port #
20180418-13:31:21.293 : Connection failed: 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 #.#.#.#:#
20180418-13:31:00.288 : Connecting to #.#.#.# on port #
What's the event handler to use to report / react to these events? The OnLogout handler is not being called.
I don't want to use FromEarlyIntercept that I guess would catch the event?
This behavior is by design - there was no connection established, no logon done, so no OnLogout event after network failure when connecting.
You can see the source code for that part - FromEarlyIntercept is also not triggered in this case. QuickFix/n just logs the error and will try to reconnect after ReconnectInterval seconds.
try
{
t.Connect();
t.Initiator.SetConnected(t.Session.SessionID);
t.Session.Log.OnEvent("Connection succeeded");
t.Session.Next();
while (t.Read())
{ }
if (t.Initiator.IsStopped)
t.Initiator.RemoveThread(t);
t.Initiator.SetDisconnected(t.Session.SessionID);
}
catch (IOException ex) // Can be exception when connecting, during ssl authentication or when reading
{
t.Session.Log.OnEvent("Connection failed: " + ex.Message);
}
catch (SocketException e)
{
t.Session.Log.OnEvent("Connection failed: " + e.Message);
}

How to handle the error "UNet Client Disconnect Error: Timeout"?

How to handle the error "UNet Client Disconnect Error: Timeout"? How not reboot the application, when it occurs?
I now resolve this issue.
I think this can help you too.
myClient.RegisterHandler(MsgType.Error, OnError);
void OnError(NetworkMessage msg){
//Error handler code
}
This Error client receive with "UNET Client Disconnect Error: Timeout".
My solution: If i not have server. I set current client as Server.

EF4 EntityException - The underlying provider failed on Open

Okay, this is a new one. I'm trying to debug my project, which I've done many times in the past, and I'm now getting this exception in one of my repositories. I haven't seen it before now. I haven't touched my repos in days, and my connection string is the same as its always been. The inner exception states:
{"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)"}
And the code it's choking on is:
public class HGArticleRepository : IArticleRepository
{
private HGEntities _siteDB = new HGEntities();
public List<Article> Articles
{
get { return _siteDB.Articles.ToList(); } // <-- this is the line
}
// more repo code
}
Again, like I said, I've never encountered this exception before, and I haven't touched my domain code in days.
This error usually means:
Connection String points to nonexistent SQL Server.
Connection String points to SQL Server that was shut down. Or not started.
Named pipes transport was disabled in SQL Server settings.
Check them carefully one by one. In your case I guess it is 2.
A second option of solution:
Review that IIS is running.
In my case it was stopped, so I got the same error.