Wcf – Problem with accessing WCF service hosted on IIS from a Windows application

wcf

I made a WCF Service with wsHttpBinding that uses asp.net membership provider for authentication. I can able to host the service on Server successfully. But my problem is wiered. (I can able see the .svc and wsdl files in browser irrespective of the domain) I can able to consume the service only from my machine or any other machine in the same domain. If I try to access the service from some other domain through the application(windows appl.). It is giving me the error message :-

"The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state"

Stack Trace:
Server stack trace:
at System.ServiceModel.Channels.CommunicationObject.Close(TimeSpan timeout)

Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
at System.ServiceModel.ICommunicationObject.Close(TimeSpan timeout)
at System.ServiceModel.ClientBase1.System.ServiceModel.ICommunicationObject.Close(TimeSpan timeout)
at System.ServiceModel.ClientBase
1.Close()
at System.ServiceModel.ClientBase`1.System.IDisposable.Dispose()
at AOLICWindows.Forms.SynchronizeTest.btnRegistration_Click(Object sender, EventArgs e)

Best Answer

wsHttpBinding defaults to using Windows credentials for authentication, which is fine as long as you are on the same domain, or on domain with full trust relationships.

The error message seems to point to a timeout - maybe you need to tweak those. Once an exception happens on your server which is not handled properly and turned into a SOAP fault, then the channel (the connection between client and server) is "faulted", e.g. it goes into a state of error, and cannot be used anymore. All you can do is abort the channel (you can't even close it anymore at that point), and re-create it from scratch.

Or maybe this timeout happens because you have wrapped your client proxy's usage into a using(......) {......} block? That's usually a great idea - but not in the case of a WCF client proxy.

The problem occurs because of the fact that once a channel is faulted, you can't even close it anymore. If you wrap your client proxy usage into a using() statement, when something goes bad on the server and isn't handled properly, the channel will fault, and at the end of the using() block, the .NET runtime tries to close it, which then throw another exception since the channel is faulted....

So for WCF clients, the recommended best practice is something like this:

YourClientProxy proxy = new YourClientProxy();

try
{
   ... use it

   proxy.Close();
}
catch(TimeoutException exception)
{
   proxy.Abort();
}
catch(CommunicationException exception)
{
   proxy.Abort();
}

Marc

Related Topic