I don’t know if I am doing something wrong, or if this is a bug (it has been a long day for me and I am at wit’s end so I cannot be certain one way or the other). If you have a WCF class (for example as shown in the code snipped below), where I have a Windows Form that happens to implement both the Service and Client side of a WCF application.

1
2
3
4
5
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public partial class Main : Form, ISomeInterface
{
  //rest of the code commented out 
}

Now this is pretty straightforward - all I was trying to do was start the ServiceHost as shown in the code snippet below. To begin with, if you instantiate the host by passing in a type (as shown by the code in Line 4), the solution will compile and run without any issues - or does it? Well, it does not as the behavior is not what you expect; while the service side of things seems to wire up correctly, the client side of things don’t. I can see the event firing and everything looks OK, and no exceptions are thrown, but it does not work. I cannot get a complete end-to-end “conversation” going.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
private void StartService()
{
  //Instantiate new ServiceHost
  host = new ServiceHost(typeof(Main));
  
  //this does not work
  host = new ServiceHost(this);
  
  //but this does
  //Open ServiceHost
  host.Open(); 
  
  channelFactory = new ChannelFactory<ISomeInterface\>("SomeEndpoint");
  channel = channelFactory.CreateChannel(); 
  
  //rest of code delete for clarity
}

It took me a little time to narrow it down to this, but if you change the line 4 above to passing in the object directly as shown in Line 5 (remember both the client and service is in the same class - see the signature in the first code snippet at the top of this post), then everything works like a charm. I am running Vista RC1 Build 5728, which essentially is the Sept. CTP of WCF.

To give you the complete context the ServiceHost class has two constructors shown below. Not sure if it is important or not, but the bindings were setup as netPeerTcpBindings.

1
2
public ServiceHost(object singletonInstance, params Uri[] baseAddresses);
public ServiceHost(Type serviceType, params Uri[] baseAddresses);

Before anyone starts sending me emails on this, it goes without saying, in the real world, you will of course not have both lines 4 and 5 - they are only shown here to point out the issue.

Not sure if anyone else has seen something similar or is it just me?