Unable to access over a network using WCF

5.8k views Asked by At

I have a WCF self-hosted service in a Windows Forms application. I am going to be using it so a hand-held mobile device can interact with the application at a basic level.

I have enabled basicHttp (http://localhost:8080/tagservice/basic), NetTCP (net.tcp://localhost:8888/tagservice), and basicHttpMex as endpoints.

For testing purposes, I have setup a virtual machine on the network (and repeated these on a physical machine as well).

These endpoints all work on the local machine where the service is hosted.

However when running the WCFTestClient on remote machines I get mixed results.

  1. Using the above endpoints, I can connect to the service and see the service contracts, proving the mex (IMetadataExhange) works. But I cannot use either http or net.tcp. I get the error message

Could not connect to http://localhost:8080/tagservice/basic. TCP error code 10061: No connection could be made because the target machine actively refused it 127.0.0.1:8080.

Could not connect to net.tcp://localhost:8888/tagservice. The connection attempt lasted for a time span of 00:00:01.0014400. TCP error code 10061: No connection could be made because the target machine actively refused it 127.0.0.1:8888.

  1. When changing localhost to the real IP address in the app.config on the host, in this case 192.168.0.61. Basic HTTP works. However net.icp fails with

    "The server has rejected the client credentials."

My two questions are:

  1. Why is the net.tcp failing? I am not given a choice to enter credentials as far as I can tell.
  2. Given the IP address can change, how can I get the app.config to use the IP address it is assigned by the DHCP server? As once this software moves from development to production environment the IP address cannot be hardcoded as it will change.
2

There are 2 answers

2
Taylor Bird On BEST ANSWER

Build and configure the client binding at runtime, so you can programmatically set the URL based on the machine information

Something like this should work

string hostName = System.Net.Dns.GetHostName();
int port = 8080;
Uri serviceUri = new Uri(string.Format("http://{0}:{1}", hostName, port.ToString()));


EndpointAddress endpoint = new EndpointAddress(serviceUri);

Then you just attach that endpoint to your client and it should all hook up.

3
Sergey Vedernikov On

You can replace localhost to your hostname (if IP address is dynamic) or IP address(if static) and recompile your client.