I have following code in which a service in the controller calls a service in another controller :
[Route("Employees")]
[HttpGet]
public async Task<HttpResponseMessage> TCPEmployeeAsync()
{
StringBuilder uri = new StringBuilder();
uri.Append(ApiHelper.GetHost(this.Request));
uri.Append("/HumanResources/Employees");
uri.Append("?fields=id,fname,lname,email");
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//This is where it gives error when uri contains https//
HttpResponseMessage response = client.GetAsync(uri.ToString()).Result;
return response;
}
This was just deployed in a DMZ server, and when we use http://localhost.... it works just fine but when we use https://example.domain.com/... it gives the following exception
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 c
at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure,
Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState
state, IAsyncResult asyncResult, Exception& exception)
What might be the problem? All my certificates are in place and up to date and also tried disabling TLS/SSL in the server, but that didn't work. Any help will be highly appreciated. Thanks.
Related
I'm making an Android app (using Unity and C#) which needs to connect over TCP to a different app running on another device (using the .NET System.Net.Sockets.TcpClient and TcpListener classes). The way this works is the host app broadcasts a UDP packet giving info on its local IP (192.168.x.x) and its TCP listening port. Upon receiving the UDP packet, the Android app attempts to connect to the TCP endpoint given. Most of the time (~80%), this works perfectly and the two devices establish a valid TCP connection. Sometimes though, the Android app receives the UDP packet, tries to connect over TCP but a "No Route To Host" socket error shows up instead; even trying again upon receiving the next UDP packet fails.
I'm suspecting that this is to do with the router creating different subnets. I'm not very familiar with networking code, so I'm not sure how to forward the TCP request over to a different subnet of the local network. What's weird is that the UDP packet is always received no matter what; and most of the time, the TCP request will fail for 10 minutes straight then start working again like nothing happened.
public async Task<bool> ConnectToHost(IPEndPoint endpoint) {
try {
TcpClient client = new TcpClient();
client.NoDelay = true;
IPAddress ipv4 = endpoint.Address;
Debug.Log("IPv4: " + ipv4);
await client.ConnectAsync(ipv4, endpoint.Port); // <--- this call throws the SocketException
Debug.Log("Connected.");
// ...
return true;
}catch(SocketException se) {
Debug.LogError("[TCPClient] Socket Exception (" + se.ErrorCode + "), cannot connect to host: " + se.ToString(), this);
}catch(Exception e) {
Debug.LogError("[TCPClient] Error, cannot connect to host: " + e.ToString(), this);
}
return false;//Could not connect
}
The ConnectAsync() call fails on the client side, giving the following SocketException (error code 10065, WSAEHOSTUNREACH); on server side, no trace of the message is ever seen.
05-29 13:31:54.591: I/Unity(24587): IPv4: 192.168.1.21
05-29 13:31:54.591: I/Unity(24587):
05-29 13:31:54.591: I/Unity(24587): (Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 48)
05-29 13:31:58.103: E/Unity(24587): [TCPClient] Socket Exception (10065), cannot connect to host: System.Net.Sockets.SocketException (0x80004005): No route to host
05-29 13:31:58.103: E/Unity(24587): at System.Net.Sockets.SocketAsyncResult.CheckIfThrowDelayedException () [0x00014] in <9eab73f5583e4ab3921ff80e74ccdb29>:0
05-29 13:31:58.103: E/Unity(24587): at System.Net.Sockets.Socket.EndConnect (System.IAsyncResult asyncResult) [0x0002c] in <9eab73f5583e4ab3921ff80e74ccdb29>:0
05-29 13:31:58.103: E/Unity(24587): at System.Net.Sockets.TcpClient.EndConnect (System.IAsyncResult asyncResult) [0x0000c] in <9eab73f5583e4ab3921ff80e74ccdb29>:0
05-29 13:31:58.103: E/Unity(24587): at System.Threading.Tasks.TaskFactory`1[TResult].FromAsyncCoreLogic (System.IAsyncResult iar, System.Func`2[T,TResult] endFunction, System.Action`1[T] endAction, System.Threading.Tasks.Task`1[TResult] promise, System.Boolean requiresSynchronization) [0x00019] in <a6266edc72ee4a578659208aefcdd5e1>:0
I have an Asp.Net application running from AWS, and it has some process that require it to send e-mails automatically (the usual welcome, confirm email, etc...).
I was able to configure it and publish it. It works fine. But as the website enters "Production", I need to run a second application for testing purposes. I'm able to create it, and differentiate which one is being requested by the bindings in IIS.
The issue when both are up and running is that when I try to send an e-mail from the "Production" one, it works fine. But from the "Test" one, I get the following Exception:
[0:] {"$id":"1","Message":"Bad Request:System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: Unable to connect to the remote server ---> 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 177.185.201.253:587\r
at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)\r
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)\r
--- End of inner exception stack trace ---\r
at System.Net.Mail.SmtpConnection.ConnectAndHandshakeAsyncResult.End(IAsyncResult result)\r
at System.Net.Mail.SmtpTransport.EndGetConnection(IAsyncResult result)\r
at System.Net.Mail.SmtpClient.ConnectCallback(IAsyncResult result)\r
--- End of inner exception stack trace ---\r
at Shappa.BackEnd.Helpers.EmailSender.<NewPhotoRequired>d__2.MoveNext() in C:\\Andre\\Shappa\\Shappa.BackEnd-Dev\\Shappa.BackEnd\\Helpers\\EmailSender.cs:line 112\r
--- End of stack trace from previous location where exception was thrown ---\r
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r
at Shappa.BackEnd.Controllers.AdminController.<PostPhotoReproved>d__2.MoveNext() in C:\\Andre\\Shappa\\Shappa.BackEnd-Dev\\Shappa.BackEnd\\Controllers\\AdminController.cs:line 78"}
My Code to send email is pretty simple:
public async Task<bool> SendEmail(MailMessage message)
{
try
{
using (var smtp = new SmtpClient())
{
var credential = Config.SMTPCredential;
smtp.Credentials = credential;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
//smtp.Host = "smtp.gmail.com";
//smtp.EnableSsl = true;
#if DEBUG
smtp.Host = "smtp.kinghost.net";
#else
smtp.Host = "smtpi.kinghost.net";
#endif
smtp.EnableSsl = true;
smtp.Port = 587;
await smtp.SendMailAsync(message).ConfigureAwait(false);
}
}
catch (Exception ex)
{
throw ex;
}
return true;
}
Any ideas?
I found it. Thanks dlatikay for your comment. It helped me find my stupid mistake.
177.185.201.253:587 this is an IP in Brazil, which meant I was deploying the application in DEBUG mode. Checking the options for publish, I was able to change it to Release. Now it works perfectly from both applications.
I've got a web app, that gets data from external services. The request itself happens like the code below - quite straightforward as far as I can see. Create a request, fire it away asynchronously and let the callback handle the response. Works fine on my dev environment.
public static void MakeRequest(Uri uri, Action<Stream> responseCallback)
{
WebRequest request = WebRequest.Create(uri);
request.Proxy = null;
request.Timeout = 8000;
try
{
Task.Factory.FromAsync<WebResponse>(request.BeginGetResponse, request.EndGetResponse, null)
.ContinueWith(task =>
{
WebResponse response = task.Result;
Stream responseStream = response.GetResponseStream();
responseCallback(response.GetResponseStream());
responseStream.Close();
response.Close();
});
} catch (Exception ex)
{
_log.Error("MakeRequest to " + uri + " went wrong.", ex);
}
}
However external test environments and the production environment could, for reasons beyond me, not reach the target URL. Fine, I thought - a request timeout won't really hurt anyone. However, it seemed that every time this request timed out, ASP.NET crashed and IIS was restarted. The event log shows me, among other things, this stacktrace:
StackTrace: at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task`1.get_Result()
at MyApp.AsyncWebClient.<>c__DisplayClass2.<MakeRequest>b__0(Task`1 task)
at System.Threading.Tasks.Task`1.<>c__DisplayClass17.<ContinueWith>b__16(Object obj)
at System.Threading.Tasks.Task.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
InnerException: System.Net.WebException
Message: Unable to connect to the remote server
StackTrace: at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endMethod, TaskCompletionSource`1 tcs)
InnerException: System.Net.Sockets.SocketException
Message: 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
StackTrace: at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception)
..so it all boils down to a SocketException, it seems to me. And right afterwards (within the same second) another event (which I'm guessing is relevant) is logged:
Exception Info: System.AggregateException
Stack:
at System.Threading.Tasks.TaskExceptionHolder.Finalize()
This is sort of beyond me as I'm no master of async code and threading, but that a timeout from a web requests causes IIS to crash seems very weird. I reimplemented MakeRequest to perform the requests synchronously, which works out great. The request still times out in those environments, but no damage is done and the app continues to run happily forever after.
So I've sortof solved my problem, but why does this happen? Can anyone enlighten me? :-)
Your continuation needs to handle the fact that .Result might reflect an exception. Otherwise you have an unhandled exception. Unhandled exceptions kill processes.
.ContinueWith(task =>
{
try {
WebResponse response = task.Result;
Stream responseStream = response.GetResponseStream();
responseCallback(responseStream);
responseStream.Close();
response.Close();
} catch(Exception ex) {
// TODO: log ex, etc
}
});
your old exception handler only covers the creation of the task - not the callback.
I have an program where I need to create some large number of folders to an external sharepoint site (external meaning I can't use the sharepoint object model). Web requests work well for this, but simply doing them one at a time (send request, wait for response, repeat) is rather slow. I decided to multithread the requests, to try and speed it up. The program has sped up considerably, but after some amount of time (between 1-2 minutes or so), concurrency exceptions start getting thrown.
Code is below, is this the best way to go about this?
Semaphore Lock = new Semaphore(10, 10);
List<string> folderPathList = new List<string>();
//folderPathList populated
foreach (string folderPath in folderPathList)
{
Lock.WaitOne();
new Thread(delegate()
{
WebRequest request = WebRequest.Create(folderPath);
request.Credentials = DefaultCredentials;
request.Method = "MKCOL";
WebResponse response = request.GetResponse();
response.Close();
Lock.Release();
}).Start();
}
for(int i = 1;i <= 10;i++)
{
Lock.WaitOne();
}
The exception is something along the lines of
Unhandled Exception: System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: Only one useage of each socket address is normally permitted 192.0.0.1:81
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddre
ss socketAddress)
at System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state,
IAsyncResult asyncResult, Int32 timeout, Exception& exception)
You might create too many connections, thus using up all the local ports you can use.
There's a timeout period for when a port can be reused after you close it.
WebRequest hides all the low level socket handling for you, but I am guessing it eventually runs out of ports, or tries to (re)bind to a socket already in a TIME_WAIT state.
You should make sure you read the response stream, even if you don't care about the response. This should help not producing too many lingering connections.
WebResponse response = request.GetResponse();
new StreamReader(response.GetResponseStream()).ReadToEnd();
I'll paste some relevant info from here:
When a connection is closed, on the side that is closing the connection the 5 tuple
{ Protocol, Local IP, Local Port, Remote IP, Remote Port} goes into a TIME_WAIT state for 240 seconds by default.
In this case, the protocol is fixed - TCP
the local IP, remote IP and remote PORT are also typically fixed. So the variable is the local port.
What happens is that when you don't bind, a port in the range 1024-5000 is used.
So roughly you have 4000 ports. If you use all of them in 4 minutes - meaning roughly you
make 16 web service calls per second for 4 minutes you will exhaust all the ports. That is the cause of this exception.
OK now how can this be fixed?
One of the ways is to increase the dynamic port range. The max by default is 5000. You can set this up to 65534.
HKLM\System\CurrentControlSet\Services\Tcpip\Parameters\MaxUserPort is the key to use.
The second thing you can do is once the connection does get into an TIME_WAIT state you can reduce the time it is
in that state, Default is 4 minutes, but you can set this to 30 seconds
HKLM\System\CurrentControlSet\Services\Tcpip\Parameters\TCPTimedWaitDelay is the key to use.
Set this to 30 seconds
You're not closing the webrequest which might cause the connection to be open unecessarily long. This sounds like a perfect job for Parallel.Net's Parallel.Foreach, just be sure to indicate how many threads you want it running on
ParallelOptions parallelOptions = new ParallelOptions();
parallelOptions.MaxDegreeOfParallelism = 10;
Parallel.ForEach(folderPathList, parallelOptions, folderPathList =>
{
using(WebRequest request = WebRequest.Create(folderPath))
{
request.Credentials = DefaultCredentials;
request.Method = "MKCOL";
GetResponse request = WebRequest.Create(folderPath);
request.Credentials = DefaultCredentials;
request.Method = "MKCOL";
using (WebResponse response = request.GetResponse());
}
});
Another thing to keep in mind is maxConnections, be sure to set it in your app.config:
<configuration>
<system.net>
<connectionManagement>
<add address = "*" maxconnection = "100" />
</connectionManagement>
</system.net>
</configuration>
Of couse in a real-world scenario you would have to add try-catch to and retrying connections that might time out leading to more complicated code
For this kind of IO intensive tasks, asynchronous programming model is very useful. However, it is a little hard to use in C#.C# also has language level support for async now, you can try the CTP release.
try this
folderPathList.ToList().ForEach(p =>
{
ThreadPool.QueueUserWorkItem((o) =>
{
WebRequest request = WebRequest.Create(p);
request.Credentials = DefaultCredentials;
request.Method = "MKCOL";
WebResponse response = request.GetResponse();
response.Close();
});
EDIT - different webrequest approach
folderPathList.ToList().ForEach(p =>
{
ThreadPool.QueueUserWorkItem((o) =>
{
using (WebClient client = new WebClient())
{
client.Credentials = DefaultCredentials;
client.UploadString(p, "MKCOL", "");
}
});
});
I have an program where I need to create some large number of folders to an external sharepoint site (external meaning I can't use the sharepoint object model). Web requests work well for this, but simply doing them one at a time (send request, wait for response, repeat) is rather slow. I decided to multithread the requests, to try and speed it up. The program has sped up considerably, but after some amount of time (between 1-2 minutes or so), concurrency exceptions start getting thrown.
Code is below, is this the best way to go about this?
Semaphore Lock = new Semaphore(10, 10);
List<string> folderPathList = new List<string>();
//folderPathList populated
foreach (string folderPath in folderPathList)
{
Lock.WaitOne();
new Thread(delegate()
{
WebRequest request = WebRequest.Create(folderPath);
request.Credentials = DefaultCredentials;
request.Method = "MKCOL";
WebResponse response = request.GetResponse();
response.Close();
Lock.Release();
}).Start();
}
for(int i = 1;i <= 10;i++)
{
Lock.WaitOne();
}
The exception is something along the lines of
Unhandled Exception: System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: Only one useage of each socket address is normally permitted 192.0.0.1:81
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddre
ss socketAddress)
at System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state,
IAsyncResult asyncResult, Int32 timeout, Exception& exception)
You might create too many connections, thus using up all the local ports you can use.
There's a timeout period for when a port can be reused after you close it.
WebRequest hides all the low level socket handling for you, but I am guessing it eventually runs out of ports, or tries to (re)bind to a socket already in a TIME_WAIT state.
You should make sure you read the response stream, even if you don't care about the response. This should help not producing too many lingering connections.
WebResponse response = request.GetResponse();
new StreamReader(response.GetResponseStream()).ReadToEnd();
I'll paste some relevant info from here:
When a connection is closed, on the side that is closing the connection the 5 tuple
{ Protocol, Local IP, Local Port, Remote IP, Remote Port} goes into a TIME_WAIT state for 240 seconds by default.
In this case, the protocol is fixed - TCP
the local IP, remote IP and remote PORT are also typically fixed. So the variable is the local port.
What happens is that when you don't bind, a port in the range 1024-5000 is used.
So roughly you have 4000 ports. If you use all of them in 4 minutes - meaning roughly you
make 16 web service calls per second for 4 minutes you will exhaust all the ports. That is the cause of this exception.
OK now how can this be fixed?
One of the ways is to increase the dynamic port range. The max by default is 5000. You can set this up to 65534.
HKLM\System\CurrentControlSet\Services\Tcpip\Parameters\MaxUserPort is the key to use.
The second thing you can do is once the connection does get into an TIME_WAIT state you can reduce the time it is
in that state, Default is 4 minutes, but you can set this to 30 seconds
HKLM\System\CurrentControlSet\Services\Tcpip\Parameters\TCPTimedWaitDelay is the key to use.
Set this to 30 seconds
You're not closing the webrequest which might cause the connection to be open unecessarily long. This sounds like a perfect job for Parallel.Net's Parallel.Foreach, just be sure to indicate how many threads you want it running on
ParallelOptions parallelOptions = new ParallelOptions();
parallelOptions.MaxDegreeOfParallelism = 10;
Parallel.ForEach(folderPathList, parallelOptions, folderPathList =>
{
using(WebRequest request = WebRequest.Create(folderPath))
{
request.Credentials = DefaultCredentials;
request.Method = "MKCOL";
GetResponse request = WebRequest.Create(folderPath);
request.Credentials = DefaultCredentials;
request.Method = "MKCOL";
using (WebResponse response = request.GetResponse());
}
});
Another thing to keep in mind is maxConnections, be sure to set it in your app.config:
<configuration>
<system.net>
<connectionManagement>
<add address = "*" maxconnection = "100" />
</connectionManagement>
</system.net>
</configuration>
Of couse in a real-world scenario you would have to add try-catch to and retrying connections that might time out leading to more complicated code
For this kind of IO intensive tasks, asynchronous programming model is very useful. However, it is a little hard to use in C#.C# also has language level support for async now, you can try the CTP release.
try this
folderPathList.ToList().ForEach(p =>
{
ThreadPool.QueueUserWorkItem((o) =>
{
WebRequest request = WebRequest.Create(p);
request.Credentials = DefaultCredentials;
request.Method = "MKCOL";
WebResponse response = request.GetResponse();
response.Close();
});
EDIT - different webrequest approach
folderPathList.ToList().ForEach(p =>
{
ThreadPool.QueueUserWorkItem((o) =>
{
using (WebClient client = new WebClient())
{
client.Credentials = DefaultCredentials;
client.UploadString(p, "MKCOL", "");
}
});
});