Query a Web Service hosted outside IIS with it's DNS name

Yesterday someone asked the following:

I am developing a Web Service inside a Windows Service via the soap.tcp protocol. This all works, and I have created the webservice at soap.tcp://localhost:9090/BookService.

However, when I set the Url to soap.tcp://example.com:9090/BookService on my local machine, I get an exception that the computer actively refused the connection.

My first attempt was to simulate the problem. I added an entry in my hosts file so that example.com resolves to 192.168.10.1 (My machine’s IP address) and wrote the following code:

EndpointReference epr = new EndpointReference(new Uri("soap.tcp://example.com:9090/BookService"));
SoapReceivers.Add(epr, typeof(BookService));

When i tried to run this code i received an ArgumentException: “WSE813: The following transport address could not be mapped to a local network interface: soap.tcp://example.com:9090/BookService”. My intuition said that i should help the infrastructure a little:

EndpointReference epr = new EndpointReference(new Uri("soap.tcp://192.168.10.1:9090/BookService"));

The application started fine this time, but when i used wsewsdl3.exe to generate the proxy i received the following error: Destination Unreachable. Anyway, after a lot of experimenting i found that the following method allows me to generate a proxy using the DNS name:

static EndpointReference GetEndpointReference(string host, int port, string path)
{
 // we happy:
 // for the Address part we use the DNS name
 //
 // infrastructure happy:
 // for the Via par we use the first IP that maps to the provided DNS name

 Uri address = new Uri(string.Format("soap.tcp://{0}:{1}/{2}", host, port, path));
 Uri via = new Uri(string.Format("soap.tcp://{0}:{1}/{2}", Dns.GetHostEntry(host).AddressList[0], port, path));
 return new EndpointReference(address, via);
}

static void Main(string[] args)
{
 EndpointReference epr = GetEndpointReference("example.com", 9090, "BookService");
 SoapReceivers.Add(epr, typeof(BookService));

 Console.Write("{0}Press any key to continue...", Environment.NewLine);
 Console.ReadKey();

 SoapReceivers.Remove(epr);
}

As always, you can download a sample solution with a windows service that hosts the webservice, and a console application that consumes the webservice: MsdnSoapExample.zip

8 Comments.

  1. It’s gr8 article to host webservice outside IIS… how do invoke the webservice method using soap.tcp?

  2. Well, i’ve decided to add a demo solution: http://www.timvw.be/wp-content/code/csharp/MsdnSoapExample.zip which contains a console application that consumes the service (the proxy was generated with WseWsdl3.exe)

  3. I’m getting the following exception
    #1: “WSE101: An asynchronous operation raised an exception.”
    #2: Internal Exception:
    “No connection could be made because the target machine actively refused it”

    FYI
    I’m running the windows service under “local system account” credentails.

  4. - Make sure that example.com resolves to an ip on your machine
    - Verify that there is not a firewall that blocks incoming traffic

  5. I de-installed and re-installed WSE 3.0 and is working fine :)

    Thanks for your help.

  6. Earlier I tried to access soap.tcp://localhost/MyService and it was working fine. but when I tried to access soap.tcp://example.com:9090/BookService I’m still getting the error.

    I have added ip address and example.com to hosts file.

    What else I have to do to access the service installed outside my system?

  7. Good day.

    Thank you very much for this article. It helped me a lot in setting up a web service that doesn’t run in iis.

    I however am still strugling with discovering the webservice. Is there a way of making these type of web services discoverable?

    http://www.timvw.be/host-a-web-service-outside-iis/

  8. Hi great example. I tried using it and it works very well on my own pc. But when I tried to invoke the web service on ther pc over lan I always get Message Expired Error. Can you help me resolved this issue? Thanks