web services throwing errors intermittently - c#

I have a web service running on server but once in a while it throws an error. This is happening when calling one particular method and all other method works fine but on calling this method I receive following error:-
An error occurred while receiving the HTTP response to
https://xxx/xxx/xxx.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.
Currently it happened after running fine for 2 weeks. Strangely the method started working fine again after few hours. I am wondering where should I start looking to troubleshoot this issue? Any pointers to troubleshoot shall be helpful.
Just to add to this today I received below error:-
The underlying connection was closed: A connection that was expected
to be kept alive was closed by the server
Can both of the error be related to each other?
Thanks for the help!

I've also encountered that error.
I encountered that error when almost thousands of user simultaneously accessing the web service.
I put this piece of code..
ServiceClient = new ServiceClient ();
var customBinding = new CustomBinding(ServiceClient.Endpoint.Binding);
var transportElement = customBinding.Elements.Find<HttpTransportBindingElement>();
transportElement.KeepAliveEnabled = false;
ServiceClient.Endpoint.Binding = customBinding;
ServiceClient.Open();

Related

Error in Calling some service in WCF Client

When I call this service method:
StaffServiceClient _Service = new StaffServiceClient();
Table_StaffInfo item = _Service.GetStaffById(10);
I get an error:
An error occurred while receiving the HTTP response to http://localhost:8008/StaffService. 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.
but when i call another service method:
StaffServiceClient _Service = new StaffServiceClient();
List<spStaffList_Result> list = _Service.GetStaffList().ToList();
the program works.
Your method GetStaffById failed internally. Maybe it has problems with receiving data (unlikely if just an int) maybe it has problems sending data (does your type Table_StaffInfo feature DataContract / DataMember attributes?) or it simply threw an exception.
You will need to debug your service. Put a breakpoint in the method and see where it takes you.
For starters, ignore the int parameter and just put a return null; in the method. If that works, it has nothing to do with your connection or server setup and you need to debug your code.

Avoiding the timeout exception while connecting to a https url to send request to a web-service

In a dotNet mvc application, while trying to send requests to a http url, my application is working as expected. However, when I try to send requests to a https url, I get the following error ( upon publishing the app in IIS and running it in a web browser) :
Server Error in '/dotnetstage' Application.
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 10.63.64.146:443
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: 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 10.63.64.146:
I understand that it has got something to do with the SSL certificates, so I found that there's a way to ignore certificate validation:
WebRequest webRequest = WebRequest.Create(reqURL);
ServicePointManager.ServerCertificateValidationCallback += delegate { return true; }; //this line works for the certificate validation ignoring
Even then, I am getting the same error. I have two queries:
Am I adding the ServicePointManager line at the proper place? If not
what is the correct location where you add this in your code?
Is there anything else I need to take care of ?
Any help or at least a pointer towards the right approach will be higly appreciated. Stack Overflow helped me a lot with learning to code in c# mvc apps and build complex caller applications in a matter of 4 weeks and I look forward to getting a quick solution to this as well. Thanks. :)
The ServerCertificateValidationCallback should be defined first before making the web request. But in your case the issue is totally different it could be that the web server side code crashed before it could send a response back.

Errors from wcf service method after few hours of work

I have a problem with wcf servcices.
Wcf services method is inovked by an application. This app calls service method very often (dozens of times per minute). The service method is called properly (with Close() at the end, or Abort() after exception). The most strange thing for me is that after few hours my app is getting errors from services:
An error occurred while receiving the HTTP response to http://domain.xx/MyService.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. The underlying connection was closed: An unexpected error occurred on a receive. Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
or this one:
*The request channel timed out while waiting for a reply after 00:15: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 'http://domain.xx/MyService.svc' has exceeded the allotted timeout of 00:15:00. The time allotted to this operation may have been a portion of a longer timeout. The operation has timed out *
What can couse such errors? Why services are working properly within few first hours?
I would check your application log. From my experience, those errors tend to me more server related than code related. IIS may be having problems.
I know you mentioned it but it looks your are not closing your channels right. Also, make sure you do NOT use the same client for many server calls. Just create one, use it for a single call, and dispose it.
Here's a good read about closing WCF channels.

suppressing WCF error

I have written a WCF service. The client is built up in Silverlight.
It works fine, but as soon as the internet connection is lost even for a second, my application throws an error in a message box "the remote server returned an error not found"
Also, it sometimes thorws WCF Request Timed Out exception. All these exceptions are displayed in a message box. I wanted to know if there is a way to suppress these exceptions, as-in I don't want a message box to be popped up everytime with these messages.
Please give me some leads.
Thanks
Not sure about your first issue.
The second issue can be solved by two ways: 1.set up client side binding timeout to longer time in web.config 2.write code to set up binding timeout. The following example sets up BasicHttpBinding at code behind.
BasicHttpBinding binding = new BasicHttpBinding();
binding.ReceiveTimeout = System.TimeSpan.Parse("00:10:00");
binding.SendTimeout = System.TimeSpan.Parse("00:10:00");
you have to catch the Exception in the Catch block and there you can keep the process for the ideal state for some time.
After some time again try to access the remote services. This will how you can handle this.

WCF REST Client Exception

I have the following WCF client code:
string add = String.Format("http://localhost:{0}/ServiceRequestRest",accessPort);
var cf = new ChannelFactory<IServiceRequestRest>(new WebHttpBinding(), add);
cf.Endpoint.Behaviors.Add(new WebHttpBehavior());
busService = cf.CreateChannel();
busService.DoMyStuff("hello",null);
When I have "first chance exception break" activated, that last line of code throws a series of exceptions. The first exception is this:
System.InvalidOperationException occurred
Message="Envelope Version 'EnvelopeNone (http://schemas.microsoft.com/ws/2005/05/envelope/none)' does not support adding Message Headers."
Source="System.ServiceModel"
StackTrace:
at System.ServiceModel.Channels.MessageHeaders.ValidateHeaderKind(HeaderKind headerKind)
I am not aware that I am trying to "add Message Headers", but I choose to continue execution and immediately get the next exception:
System.Net.WebException occurred
Message="The underlying connection was closed: The connection was closed unexpectedly."
Source="System"
StackTrace:
at System.Net.HttpWebRequest.GetResponse()
With all that said, I'm having difficulty understanding what the problem is. Any clues?
Update: The return type of my REST call could not be serialized, as I learned from another site. That alone could cause this problem, but after I fixed it I still had the same problem. When I use a regular catch block for the exception, all I get is "the connection was closed unexpectedly." What else could I be missing?
Update2: I think I found the final problem. I was getting interference from Skype. The code is working now. :)
The main problem that I see is that you are trying to use a WCF Channel to communicate using WebHttpBinding.
IMHO the entire purpose of WebHttpBinding was so that the client did not have to use the WCF stack to communicate with it. You simply use any HTTP stack to make the requests.

Categories