[Updated for Win8/Dev11 Beta]
Hi all, With Windows 8 metro styled applications getting a great buzz - I thought this was a perfect time to introduce the WCF support for Metro applications.
We provide support for WCF client inside a Metro application with a subset of features compared to classic desktop applications. See here - http://blogs.msdn.com/b/piyushjo/archive/2011/10/19/wcf-for-metro-apps-supported-functionality.aspx for the supported feature list. This enables you to consume all those awesome web services out there, in your metro app very easily and as we will see – very similar to what you are already used to.
We will start with a simple Metro application in C# and consume an ASMX web service (one of the first weather services I found in my search results). The application in its current form will be a simple weather reporting client taking in a US zip code and displaying the weather by calling an external web service hosted here - http://wsf.cdyne.com/WeatherWS/Weather.asmx. The point of the demo is to clarify the client experience for Win8 Metro app. In future blog posts, we will extend the application to include more of supported WCF features and will consume WCF services which is likely going to be the norm.
1. Open Visual Studio 11 and create a new Windows Metro style Blank application.
2. In your application - right click on References and add the service reference to http://wsf.cdyne.com/WeatherWS/Weather.asmx service just as you do for classic desktop apps. This will generate a service reference to this weather service.
3. So far nothing out of the ordinary - everything is exactly the same as what we are used to while adding a service reference to a web service.
The fun starts now: a) First note that there is no configuration file generated. Metro apps do not support the traditional xml styled application so we have modified our Add Service Reference (ASR) experience from within a metro app to generate code based configuration. Where will you find this generated code? Go to the Reference.cs file generated under the service reference folder. You will have to click "Show All Files" in Solution explorer. You will see code like the following for the generated binding and endpoint address:
Now this also means that if you need to change any binding properties on the client side - you need to do this in code. You can use the newly generated partial method ‘ConfigureEndpoint’ to implement any changes outside the generated proxy class so that you can update the generated proxy (including Reference.cs) without any fear of your changes being overwritten.
b) Next - you will also notice a difference in the style of operations generated for the service. Note that you only have task based async patterned methods generated. This ensures that there are no blocking calls from the metro styled UI apps, even unknowingly. If you will generate a similar service reference from a classic desktop app in Dev11 - you will see both synchronous and task based async methods. Task based async pattern is the recommended pattern for doing async programming going forward therefore in Add Service Reference - Advanced dialog, we select it for you by default:
4) That’s it. Thereafter you can call the service asyn using the following Task based async model:
Attached is a sample app which contains two projects - one is a metro styled app and another a regular console app making the same service call. You can compare the generated service references to get more insight.
Sample code attached.