Using the Service Model Metadata tool

If you have created a WCF/web service to consume from your mobile application, you can easily connect to it by adding a web reference to it in your mobile application project in Visual Studio (as you've done in Chapter 10, Building the Dashboard). There is, however, another way to do this. The ServiceModel Metadata tool allows you to quickly generate the classes that are required to connect to a WCF/web service. You can even specify the language of the generated source code.

To test this tool, you can use the DashboardService web service you've created in Chapter 10. Make sure that the web service is properly set up and hosted in IIS. Open a command prompt window and navigate to the following folder:

\Program Files\Microsoft.NET\SDK\CompactFramework\v3.5\bin

You can run the ServiceModel Metadata tool using the following command and syntax:

netcfsvcutil.exe /language:<language> <serviceURL

For example, you can generate C# client code files for the DashboardService web service by running the following command:

netcfsvcutil.exe /language:cs http://localhost/DashboardService/ Dataservice.asmx

The output of this command is shown in the following screenshot:

C:\PROGRA ™i\M ic ro s o f t.NET\S DK\Co npac t Frame no rk\u 3.5\b in >n e t c f s u c ut i1.e xe /lan gua ge:es http://localhost/dashboardseruice/dataseruice.asnx Microsoft <R> .NET Compact Framework Service Model Metadata Tool [Microsoft (R) Hindous <R) Communication Foundation, Uersion] Copyright (c> Microsoft Corporation. fill rights reserved.

Attempting to download metadata from ' Jittp:/VlocalJiost/dasJiboardseru ice/dataseru ice.asmx' using US-Metadata Exchange or DISCO. Generating files...

C:\Program Files\Microsof t _NET\SDK\CompactFramework\u3 _5\bin\DataSeruice .cs C:\Program FilesNMicrosoft .NET\SDK\CompactFramework\u3 .SNbinNCFClientBase .cs

C:\PROGRA ~i\M ic ro s o f t.NET\S DK\Co mpac t Frame uork\u 3.5\b in >_

Now, create a new Smart Device Windows Application project, and import the following libraries to the project:

• System.ServiceModel

• System.Runtime.Serialization

Add the two generated files to your project. After that, add a new form to your project. Place a multiline text box and a button on this form (named btnInvoke). In the click event of this button, write the following code:

private void btnInvoke_Click(object sender, EventArgs e) {

System.ServiceModel.Channels.Binding _binding = DataServiceSoapClient.CreateDefaultBinding(); string _serverAddress =


//Replace with the correct address - keep in mind that this //code executes on your mobile device - it needs to connect //to the webservice sitting on the server _serverAddress = _serverAddress.Replace("localhost",

"edzehoo-pc"); EndpointAddress _endPoint = new


//Create the client

DataServiceSoapClient _client = new

DataServiceSoapClient(_binding, _endPoint);

//Get chart data for December 2009 txtResults.Text = _client.GetChartData(2 00 9, 12);

catch (Exception ex) {


That's all it takes. Make sure that an ActiveSync connection has been set up between the mobile device and your development machine. When you run this application and click the Invoke button, it will successfully connect to your web service and retrieve the data into the multiline text box (as shown in the following screenshot).

Connect to web servi +!

<ChartData> <RoadShowSales> < Dayl Value="6778" /><Day2 Value="41076" /xDay3 Value="49042" /><Day4 Value="14436" /xDay5 Value="34755" /><Day6 Value="21172" /xDay7 Value="29178" /xDayS Value="25262" /xDay9 Value="11077" /xDaylO Value="28299" /xDayll


Was this article helpful?

0 0
Living With Android

Living With Android

Get All The Support And Guidance You Need To Finally Be A Success At Using Android. This Book Is One Of The Most Valuable Resources In The World When It Comes To Playing With the Hottest Android Tablet.

Get My Free Ebook

Post a comment