Timeout exception calling UserPrincipal.GetGroups from a Windows service - wcf

When I run simple console app that calls UserPrincipal.GetGroups, it enumerates the users groups with no problems. However when I run the same code as the same user on the same server but from a windows service hosting WCF, I get the following chain of errors:
Message : The socket transfer timed out after 00:00:10. You have exceeded the timeout set on your binding. The time allotted to this operation may have been a portion of a longer timeout.
Inner Exception
---------------
Message : The read operation failed, see inner exception.
Inner Exception
---------------
Message : The socket transfer timed out after 00:00:10. You have exceeded the timeout set on your binding. The time allotted to this operation may have been a portion of a longer timeout.
Inner Exception
---------------
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
Could this have something to do with the WCF thread impersonation? WindowsIdentity.GetCurrent().Name returns the same user, however Thread.CurrentPrincipal.Identity.Name is different - empty string in the case of the console app, but the impersonated WCF user in the case of the Windoes Service.

Related

Diagnosing timeout, connection pool or socket issue

I have a .Net 6 webapplication, that is hosted in IIS on Windows server 2019.
Sometimes after deployment, it will start having issues connection to SQL server and WCF services.
The thing that strikes me is that both types of errors start appearing at the same time. The SQL server, net.tcp WCF services and HTTP WCF services all start throwing these errors at the same time.
This leads me to think it could be some kind of socket exhaustion or similar?
How would I diagnose this kind of broad connection issue?
Note: The application has been ported from .Net Framework to .Net 6, and the old version does not exhibit this behavior, so I suspect that the .Net 6 code has some sort of leak, but I need to find out what is causing it.
Examples of the exceptions:
SQL error after the default 15s timeout:
Begin failed with SQL exception Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.
Another sql error
could not execute batch command.[SQL: SQL not available] Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. The wait operation timed out.
WCF net.tcp service
The socket was aborted because an asynchronous receive from the socket did not complete within the allotted timeout of 10675199.02:48:05.4775807. The time allotted to this operation may have been a portion of a longer timeout. The socket connection has been disposed.
Object name: 'System.ServiceModel.Channels.SocketConnection'.
HTTP HTTP service
The request channel timed out attempting to send after 00:01:00. Increase the timeout value passed to the call to Request or increase the SendTimeout value on the Binding. The time allotted to this operation may have been a portion of a longer timeout. The HTTP request to 'https://www.example.com/service' has exceeded the allotted timeout of 00:01:00. The time allotted to this operation may have been a portion of a longer timeout.

multiple user connecting to WCF at same time gives socket exception

when multiple users are connecting to WCF Service at the same time, we are getting socket Exception.
Error Message:
{"The socket transfer timed out after 00:00:09.9844000. You have exceeded the timeout set on your binding. The time allotted to this operation may have been a portion of a longer timeout."}
Error code:10060

Self hosted WCF service not responding after several hours of being idle

I have hosted WCF service in a console application which is expected to run 24/7 and respond to requests from several applications. It works great if it gets requests continuously or even after 4 hrs of idle time. The code written to host the service is given below,
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(abcdService)))
{
host.Open();
Console.ReadLine();
host.Close();
}
}
In the WCF service, I was writing the output to console. When it throws the timeout error, I could see that the method in WCF service never received a request.
Is there any settings I need to do to make the self hosted WCF service respond to simple calls even after several hours of idle time?
The error I received is given below,
The request channel timed out while waiting for a reply after 00:00:59.9989999. Increase the timeout value passed to the call to Request or increase the SendTimeout value on the Binding. The time allotted to this operation may have been a portion of a longer timeout. ---> System.TimeoutException: The HTTP request to '' has exceeded the allotted timeout of 00:01:00. The time allotted to this operation may have been a portion of a longer timeout. ---> System.Net.WebException: The operation has timed out. Aborting test execution.
Note: When I restart the console application hosting WCF service, it starts to work fine again.
Your requests might be queuing up on server before they are handled and then times out.
1) Try throttling in your WCF services.
2) Try using PerCall Use the following to remove the Session in your interface
[ServiceContract(Namespace="namespace", SessionMode=SessionMode.NotAllowed)]
Contract class implementing interface
ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
You should be able to enable tracing to see exactly what is occurring.

How do I troubleshoot a WCF service with failing load tests?

I have a WCF 4 service running on IIS 7 (Windows Server 2008), deployed to a development server. The service works when I run a small website that consumes the service (click a button, the page calls the service, data is returned. It's very basic).
I also created some unit tests that call the service and placed them into a load test. In a nutshell, the "unit tests" call the service and write the byte[] streamed back from the service to a file. The service performs as expected when running the load test.
The problem is the load test fails after a very small number of iterations. Sometimes foour calls are successful, other times after five calls. Again, individually, the tests run fine but when I put them together to simulate a live environment (multiple users, multiple test iterations) this problem appears.
The exception thrown is:
Test method TestProject1.UnitTest1.RunReport threw exception:
System.ServiceModel.CommunicationException:
An error occurred while receiving the HTTP response to http://xx.xx.xx.xx:yy/Service.svc. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details. ---> System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
Update #1:
I just set the test iteration to 10 (run the test 10x) and the constant user load to 1. The test was able to complete succesfully. This was using the most basic of the unit tests. The second, more complicated test, was omitted for this run.
Look in your application event log (eventvwr.exe). Assuming you're operating in a mostly-normal environment, there will be an entry for the server-side error.

WCF call fails because underlying connection was closed

I'm making a call to a WCF service but I get a CommunicationException on the client while receiving the response from the service.
System.ServiceModel.CommunicationException: An error occurred while receiving the HTTP response to http://localhost:8080/Service. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details. ---> System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
The client that makes the call is a WCF proxy client.
The service method executes without
any exceptions.
The WCF call works fine in those cases where it does not
take a long time for the serivce
method to finish.
The WCF call fails with the above exception message when the service method is taking long time to finish.
The sendTimeout property of the client's binding has been increased to 30 minutes to accommodate the time it can take for the service method to finish.
Try to set the receiveTimeout equal or greater than the time it takes for the service method to complete. The default value for the receiveTimeout property is 10 minutes. So if the service method takes longer time to complete the connection will be closed (if no other activity takes place before the receiveTimeoutoccurs). The receiveTimeout property is described here.
A very long operation like this should most likely be called asynchronously - in other words, the client asks the server to prepare the data, then gets on with something else while the server does the work. When the server's finished it calls the client back.
Asynchronous WCF operations are discussed here.