Oct 06 2006

Interesting WCF Behavior

Category: .microsoftAmit Bahree @ 10:20 pm

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?

Share
Similar posts to check out:
  • January 10, 2012 -- 15 Year of Microsoft at CES (0)
    ...
  • November 1, 2011 -- Productivity Future Vision (0)
    Microsoft's productivity future - how cool is this? ...
  • June 17, 2011 -- Kinect SDK (0)
    Microsoft recently release the Kinect SDK which allows you to implement a Natural User Interface and program against it! There is a lot of interest  around including claims on how Robotics will change to how you can integrate a light sensor. You can use Visual Studio (C++, C# and VB.NET supported) and get quite interesting results. Here are a series of links below which will help you get started. Download and install the Kinect SDK Download and install Quickstart Samples and Slide...

Tags:

Leave a Reply

*

Get Adobe Flash playerPlugin by wpburn.com wordpress themes