Freigeben über


Spent a lot of time trying to talk to a "Muted" WCF ServiceHost

Today i spent 1h trying to figure why i couldn't talk to a WCF Endpoint hosted on a Windows Service.

I did some work on my Vista laptop yesterday where everything was working fine and then copied the Service library (that contains the service i wanted to host) to my main dev box (Win7), added it to my Windows Service host and hacked some quick code/config to have it created by the service and running.

The service started fine and the logs were saying that the service host for the WCF service i just added was all good, but from my clients I kept getting:

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

Hmm, must be some Win7 security goo. So i spent the next *hour* trying a gazzilion of things - security? turned it off and no go, firewall? some weird policy? configured it and created a console app that opened the port and it was working fine. Need to do netsh http iplisten? that's for http, but who knows.... nothing still. Ok, let's try another binding instead, still same issue... . Ok, let's trace the client and server setting up WCF diagnostic traces... nothing relevant on the server and i already know what the client is going through. Ok, run under the debugger and see if there's anything weird... still nothing. Just looks like the wcf service is "muted" but it does not complain when i start it, and I know i'm creating the host and starting one by one.... I cannot be the only one having this problem so let's see what live.com finds... many different issues but i've already covered most of the tips or they don't apply.
At this point I'm starting to lower the level of investigation to figure what's going on with the ports, so i just try to use telnet

>telnet localhost 8733
Connecting To localhost...Could not open connection to the host, on port 8733: Connect failed

ok, let me use netstat to confirm that i've used the right port and protocol (which is probably what i should have done to begin with):

> netstat -a -o | findstr 8733
>

nothing! i'm absolutelly sure that i'm only using netTcpBinding and i review my code and config and yes, that's the port!
I use netstat again to verify what other ports might be opened for my process (pid 5868 - not to confuse with port nb)

> netstat -a -o | findstr 5868
>

nothing again!
At this point i'm really confused, but the only logical explanation is that my hosting code is busted, so i review it again and compare it to the last version... and finally understand what happened.

I added some tracing to know in detail when each ServiceHost was created and in doing so, i removed a "kind of important" line by accident

host.Open();

I "uncomment" it, and now everything's good again.

>netstat -a -o | findstr 2864
TCP 0.0.0.0:8733 MACHINE1:0 LISTENING 2864
TCP [::]:8733 MACHINE1:0 LISTENING 2864
UDP 127.0.0.1:63316 *:* 2864

So, moral of the story: cover you basics first.

Make sure the server is actually "listening" in order to reply and not "muted" and double check your code changes before trying heavy weight techniques and tools :)

This time i'm also documenting it so that if i run into this again (to be honest i'ts not the first time :)) so that i don't forget again!