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 FilesMicrosoft.NETSDKCompactFrameworkv3.5in
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:
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 =
DataServiceSoapClient.EndpointAddress.Uri.ToString();
//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
EndpointAddress(_serverAddress);
//Create the client
DataServiceSoapClient _client = new
DataServiceSoapClient(_binding, _endPoint);
try
{
//Get chart data for December 2009
txtResults.Text = _client.GetChartData(2009, 12);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
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).
18.223.160.61