Silverlight client consuming WCF service

Thursday, July 3, 2014

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://"));

// Open the channel.
ICalcService channel = cf.CreateChannel();

// Invoke the method asynchronously.
channel.BeginAdd(9, 5, AddCallback, channel);

private void AddCallback(IAsyncResult asyncResult)
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:// 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?