Inter-application communication with WCF webservices

Unfortunately for me (but not unfortunately at all I guess), not all software developed right now is .NET based. That’s fine because there are a lot of other really good alternatives out there which do the trick and in some cases even do the same trick even better. And even when a .NET based application seems to be a better choice, there are a lot reasons why you’ll still want to develop a non-.NET app. For instance: let’s say we’ve got an big application developed in Java, Oracle or Progress. It makes sense that new requirements made by your customer will end up being developed in the same language/environment used for building the main app, right?

But how about giving the new part of your application a modern “Office style” look? Using .NET with some 3rd party components that’s a piece of cake, but not all development environments offer the possibility of developing nice looking applications. And I’m not even talking about all really nice framework parts which are available in .NET development.

A possibility would be to create the new ‘module’ of your application using .NET, but that would (in most cases) require some-kind of communication mechanism between the ‘main app’ and your module. Now with .NET 3.0 that’s possible and made easy for you using webservices!

For creating a .NET based application module which is connect-able from any otherapp, you’ll need the new ServiceHost class which is available in .NET 3.0. This host allows you to create a web service host which doesn’t need IIS installed on the ‘server’ machine. Because a web service is a platform independent way of communicating, it allows you to connect separate application layers to each-other, not caring what is beneath the surface of the web service. But we’ll take it one step further and not connect application layers, but entire applications with each other.

I’ve made a little sample application available for download here(Visual Studio 2008 project). Run the bat file in the root dir to run both apps and test the sample, view the code along with reading the rest of this post 🙂

Setting this up is quite easy to do, I’ll take you through all of the steps.

  1. First create a new Winforms project. On your newly created form, add a label and set the text to ‘Nothing received yet’ or something like that.
  2. Now, using the Add Item option in the Solution explorer, add a new WCF Service class to your project. This will add two class files; one for the interface defining your contract, one for the class implementation. It will also set some configuration settings in your app.config file, make sure you check what’s going on in there.
  3. To be able to alter your form from within the service class file, you’ll need to pass along the forms instance to the class constructor. Just create a private property to store the instance once received.
  4. Now create a function ‘SetLabelText’ in the forms code file to alter the label text, nothing exciting.
  5. Change the default ‘DoWork’ function of the WCF service to ‘SetText’ which accepts a string parameter. Off course, this function calls the forms SetLabelText function. Keep in mind you’ll have to change the function in two places: the service interface and the service class file!
  6. Now you’ve got the service all set up, you just need to host it. That’s done using the ServiceHost class from System.ServiceModel. Set up a little helper class called ‘WCFServiceHost’ which has a private ServiceHost property to keep track of your host. The class will have two functions; StartService and StopService. In the StartService function, create a new instance of the ServiceHost. You’ll need to create a singleton instance of your service (which you can store in your helper class to prevent garbage collection) and pass along an empty Uri[] array item because you already set the baseAddress in your app.config. In order to run the service from a singleton instance, you need to label the WCFService class with the following attribute:
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] 
  7. Now call the StartService function from your forms constructor and you’re done, all should run well without any errors!

Now the second part is setting up the client app; this is even easier!

  1. Create another winforms application and call it ‘WCFClientApp’ or something like that. In order to add the service reference, the ‘server’ app needs to be running. You can run it without debugging in Visual Studio by pressing ctrl+F5. While running, add the service reference and paste in the URI you can copy/paste from your app.config. If your service is any good, it will be discovered and all of the functions in it will be listed.
  2. Add a text box and a button to the form of your client app. Double-click the button to generate the click event handler. Create a new instance of the WCFServiceClient class which is generated for you, or create one in the constructor. Call the SetText function and pass along the text property of the text-box you put on the form.

Run the both together and voila: interaction between two applications! Now off course this example is made with .NET technology only, but the beauty of using the webservice is the ability to call from anything which supports calling webservices! In order to have the best compatibility, you should set the WCF service to use the basicHttpBinding instead of the default wsHttpBinding. More information about the different binding types is found here.

Related posts

Latest posts

Leave a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *