Error in Calling some service in WCF Client - c#

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.

Related

web services throwing errors intermittently

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();

FaultException vs Exception in WCF

I'm learning about FaultException and CLR Exceptions in the context of WCF services but something is not clear.
In the book "Learning WCF" it says that when a service throws a normal CLR Exception it's caught by the service model, the service model constructs a SOAP message from that, sends it back to the client, and the exception is thrown at the client again. There the channel is faulted.
So I've built a service which throws a NullReferenceException.
A client calls the service, catches the exception and print the CommunicationState.
try
{
x = Proxy.Call(); // throws NullReferenceException at the service
}
catch (Exception ex) // or CommunicationException is the same
{
MessageBox.Show("" + Proxy.InnerChannel.State);
}
But the State member stays on Opened and I can call the service forever....
What is correct here? Should a client go into faulted state when a service throws a CLR exception and stays in Opened state when a service throws a FaultException? Or does it always stay open?
I can not find official confirmation, but here is what going on in my opinion:
You using BasicHttpBinding which does not keep connection open, but creates new one on every request. If you switch to NetTcpBinding, you will see expected behavior(just tested locally)
When error occures session's state gets in fault state and since binding such as basicHttpBinding does not support session at all you cannot distincly see that connection is in faulted state. Try to make use of binding which supports session such as netTcpBinding and you should discover that session, after exception being thrown, is not accessible.

Silverlight client consuming WCF service

Please bear with me as I am new to WCF services/ Windows services. I've created a WCF service hosted in a Windows service. I want to consume that WCF service in a Silverlight in-browser application over TCP. Below is the code fragment in Silverlight to access WCF service:
var messageEncoding = new BinaryMessageEncodingBindingElement();
var tcpTransport = new TcpTransportBindingElement();
var binding = new CustomBinding(messageEncoding, tcpTransport);
// Create a channel factory for the service endpoint configured with the custom binding.
var cf = new ChannelFactory<ICalcService>(binding, new EndpointAddress("net.tcp://192.168.2.104:4508"));
// Open the channel.
ICalcService channel = cf.CreateChannel();
// Invoke the method asynchronously.
channel.BeginAdd(9, 5, AddCallback, channel);
private void AddCallback(IAsyncResult asyncResult)
{
try
{
double endAdd = ((ICalcService) asyncResult.AsyncState).EndAdd(asyncResult);
}
catch (Exception exception)
{
throw exception;
}
}
The code works fine sometimes but often it throws an infamous System.ServiceModel.CommunicationException with the following message for some reasons:
Could not connect to net.tcp://192.168.2.104:4508/. The connection attempt lasted for a time span of 00:00:00.1010058. TCP error code 10013: An attempt was made to access a socket in a way forbidden by its access permissions.. This could be due to attempting to access a service in a cross-domain way while the service is not configured for cross-domain access. You may need to contact the owner of the service to expose a sockets cross-domain policy over HTTP and host the service in the allowed sockets port range 4502-4534.
The innerException of type System.Net.Sockets.SocketException says
An attempt was made to access a socket in a way forbidden by its access permissions.
What are the possible reasons behind this exceptions? Based on what I investigated so far, I could find only one reason: Improper ClientAccessPolicy.xml. What may be the other reasons? If you have any useful resources, please provide me the same. One more question, if I want to make Windows service hosted WCF service to get consumed by other machines on LAN, what settings do I have to make? E.g. firewall settings? My code cannot access WCF service on other machine. It throws the same exception I mentioned above. Any ideas about how to get rid of this exception?
Problem sorted..!! I had to do following things:
1) Specified SecurityMode.None while creating NetTcpBinding in Windows service.
2) Created an Inbound Rule in Windows Firewall With Advanced Security to allow TCP traffic on the port I specified in the end point address.

Error 502 proxy error (network name no longer available)

I have an application that connects to a external WCF Service behind a proxy.
When i try to call a method of the server I keep receiving the following error:
(502) Proxy Error ( The specified network name is no longer available. ).
With an exception of type ProtocolException.
I have tried to search for the WSDL using a browser (using the same proxy as the application) and I am able to reach it without problems.
The proxy is specified in the app.config under system.net/defaultProxy.
Thanks in advance.
Mattias

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.

Categories