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:
  • May 19, 2012 -- Metro Apps in C++ anyone? (0)
    In Visual Studio “11” when I try and create a new C++ Metro app using the built-in template, I get the following error: “Can’t find localized resources”. I wonder if anyone else has managed to get around this? I am running the Consumer Preview Build of Win 8 (Build 8250). ...
  • April 25, 2012 -- SkyDrive, Windows 8 and Domain Account (0)
    I am running Windows 8 on my primary work machine now, which is domain joined. When I try and use the SkyDrive metro app (which ships with Windows 8), it does not like that fact I am domain joined and wants me to switch accounts, which is something I don’t want to do. This of course works great for those who are not domain joined and essentially are personal machines. For many of us who will be using this on ‘work’ machines, this seems like we will be ignored. Of course I can install the deskt...
  • March 6, 2012 -- Enabling Win 8 Metro on a Netbook (0)
    When you install Win 8 on a Netbook the screen resolution would be too low for Metro apps to run which is a bummer. One way to get around this and “fix” this is to update the registry (and you thought that was so XP!) :). Run Regedit and search for “display1_downscalingsupported” (without quotes). Find all occurrences of this entry and change its value from 0 to 1. Reboot when finished and you should have more options on your Screen Resolution choosing which will allow you to run Metro. :) ...

Tags:

Leave a Reply

*

Get Adobe Flash player