Interesting WCF Behavior

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 [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] 2 public partial class Main : Form, ISomeInterface 3 { 4 //rest of the code commented out 5 }

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

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 public ServiceHost(object singletonInstance, params Uri[] baseAddresses); 2 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?

Published by

Amit Bahree

This blog is my personal blog and while it does reflect my experiences in my professional life, this is just my thoughts. Most of the entries are technical though sometimes they can vary from the wacky to even political – however that is quite rare. Quite often, I have been asked what’s up with the “gibberish” and the funny title of the blog? Some people even going the extra step to say that, this is a virus that infected their system (ahem) well. [:D] It actually is quite simple, and if you have still not figured out then check out this link – whats in a name?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.