Getting started with WCF |
Written by Ian Elliot | |||
Sunday, 16 January 2011 | |||
Page 2 of 2
Trying it outSo now we have a basic service and all that remains is to try it out – but, to do so we need to consider how to host the service and we have to write and run a client to call the method. However, we can avoid both of these steps if we simply allow Visual Studio to take care of both problems. If you now run the application you should see, after a build and time for the WCF Host to start, the WCF Test Client. This is the client that Visual Studio creates for you. Within its window you should also be able to see the details of the service you have just created. In screen dump below you will see it’s name, Service1, and details of the interface it implements. In particular you should be able to see the GetData() method.
To try your new service out double click the GetData method and you will see a new tab open. Enter any numeric value you like into the Value field and click the Invoke button. This will call the service using that method and display the result in the Response pane.
This might look all very trivial but what has happened is far from trivial. The method call was performed over the network to the WCF Service host. In principle the test client could have been located anywhere – you have a genuine service and you haven’t written a single line of code. You should at this point be thinking that WCF is easy and be wondering what all the fuss was about. However notice that all of the code you have just used was generated by Visual Studio and if you wanted to use say Visual C# Express then you would have had to write all the code yourself as it doesn't have a WCF template. Customise the serviceTo make the service our own let’s add some code to return the usual Hello World type message. Adding a method to the service is a two-step process – first expand the interface definition and then implement the method. This is always that way that it works. Go to the IService.cs file and add: [OperationContract] Now go to the Service.cs file and add to the Service1 class:
public string HelloService() Now if you run the project the WCF Service host will be started and you will see the new method within the WCF Test Client. Invoking it will produce the familiar response.
So WCF is easy after all – well yes that’s the point. Client server development is more difficult than desktop applications but WCF tries to package the whole thing to make it more usable. This does mean, however, that there is a lot going on behind the scenes. For example there is an additional file in the project that we haven’t examined, even though it does a lot of work – App.config. This is an XML configuration file that determines how the service is hosted and other details. Then there is the fact that we have avoided the problem of creating a client – and this is the next logical step in getting to grips with WCF. If you would like to be informed about new articles on I Programmer you can either follow us on Twitter, on Facebook, Digg or you can subscribe to our weekly newsletter. |
|||
Last Updated ( Monday, 17 January 2011 ) |