1 out of 3 rated this helpful Rate this topic

Networking and Web Services Overview for Windows Phone

Windows Phone

December 15, 2011

The Internet is host to an extensive variety of web and data services that you can use in your Windows Phone applications to create compelling new user experiences. This topic introduces networking and web services, and describes the primary classes and utilities that you can use for building web-integrated Windows Phone applications.

This topic is divided into the following sections:

The end-user experience of any web-based application is highly dependent on the quality and availability of the device’s network connection. On Windows Phone OS 7.1, the Microsoft.Phone.Net.NetworkInformation namespace provides several classes that allow your application to learn more about the network status of the device it is running on. For example, your application can check whether a cellular-data or Wi-Fi connection is enabled. You can also use the API to set cellular or non-cellular network preferences. For more information, see Network and Network Interface Information for Windows Phone.

For bidirectional communication across the web, such as with a chat application, Windows Phone OS 7.1 supports sockets-based applications. With sockets, the client or the server can initiate communication and either endpoint can send messages to the other independently. Sockets applications use the System.Net.Sockets API. For more information, see Sockets for Windows Phone.

Much of the success of the Internet is due to the Hyper-text transfer protocol (HTTP). HTTP is a relatively simple and nearly ubiquitous networking protocol that web browsers and web service client applications use for exchanging information with servers across the Internet. HTTP is the foundation on which most web services are built.

Web services enable programmatic access to a wide variety of data over the Internet. A data service is an HTTP-based Web service that implements the Open Data Protocol (OData) to expose data as resources that are defined by a data model and addressable by Uniform Resource Identifiers (URIs).

Web and data services each use an open XML-based language to describe their web-based API. The Web Service Description Language (WSDL) is used to describe the services that a web service offers. The Conceptual Schema Definition Language (CSDL) describes the data model that a data service offers. For more information, see Web Services Description Language (WSDL) and Conceptual Schema Definition File Format.

Web Services

Because the vast majority of web services published on the Internet are based on HTTP, you can use the HttpWebRequest and WebClient classes to access web services from Windows Phone applications. To help ease the task of generating the additional code that web services often require, you can use the Silverlight Service Model Proxy Generation Tool (SLsvcUtil.exe) or the Visual Studio Add Service Reference feature to generate a proxy class. For an example of how to use the WebClient class to access an RSS feed, see How to: Create a Basic RSS Reader for Windows Phone.

A web service proxy class implements the serialization, request, and response code for a web service, based on the web service WSDL file. You can use the generated proxy class in your Windows Phone application for communicating with the corresponding web service. For more information, see Using SLsvcUtil.exe to Access a Service.

Important note Important Note:

Only code generated from the Silverlight 3 version of SLsvcUtil.exe is supported for use with Windows Phone applications. This version is included in Microsoft Visual Studio® 2010 Express for Windows® Phone and cannot generate code for features that are specific to Silverlight 4. For more information, see Networking in Silverlight for Windows Phone.

Data Services (OData)

A data service is an HTTP-based web service that implements the Open Data Protocol (OData) to expose data as resources that are defined by a data model and addressable by URIs. This enables you to access and change data using the semantics of representational state transfer (REST), specifically the standard HTTP verbs of GET, PUT, POST, and DELETE. For more information about OData, see Open Data Protocol (OData) Client for Windows Phone.

Because data services are based on HTTP, you can use the HttpWebRequest and WebClient classes to access data services from Windows Phone applications. To help ease the task of generating the additional code that a data service requires, you can use the WCF Data Service Client Utility, DataSvcUtil.exe, or the Visual Studio Add Service Reference feature to generate a proxy class based on the data service CSDL file. You can use the generated proxy class in your Windows Phone application for communicating with the corresponding data service. For more information, see How to: Consume an OData Service for Windows Phone.

Note Note:

WCF Data Services enables the creation and consumption of Open Data Protocol (OData) services from the web in .NET Framework applications. For more information, see the WCF Data Services Developer Center.

Classes and Utilities

The following list contains the classes that you can use directly to make web requests, as well as the utilities available to you to generate other classes optimized to make particular kinds of web requests from your Windows Phone applications:

  • WebClient Class: Provides common methods for sending data to and receiving data from a URI-based resource.

  • HttpWebRequest Class: Provides an HTTP-specific implementation of the abstract WebRequest class.

  • Silverlight Service Model Proxy Generation Tool (SLsvcUtil.exe): Generates proxy classes based on a web service WSDL file.

  • Visual Studio Add Service Reference Feature: Generates proxy classes based on either a web service WSDL file or a data service CSDL file.

  • WCF Data Service Client Utility (DataSvcUtil.exe): Generates proxy classes based on a data service CSDL file.

The following table shows which classes can be used for the various types of HTTP-based programming:

Class

General HTTP programming

HTTP-based web service

Data service

WebClient class

yes

yes

yes

HttpWebRequest class

yes

yes

yes

SLsvcUtil.exe generated classes

no

yes

no

Add Service Reference feature generated classes

no

yes

yes

DataSvcUtil.exe generated classes

no

no

yes

The WebClient and HttpWebRequest classes can be used for a wide range of HTTP-based programming, from general HTTP requests to programming web and data services. Depending on how your application uses a web or data service, using the WebClient or HttpWebRequest classes exclusively may require you to write a significant amount of code.

When developing a web or data services client application, an alternative to programming at the HTTP-level is to use a proxy class. A proxy class is a class that represents the web or data service that is based on the corresponding WSDL or CSDL file, respectively. See the following sections of this topic for more information.

When connecting to a web service that requires an application key, do not store the application key with an application that will be run on a device. Instead, you can create a proxy web service to authenticate a user and call an external cloud service with the application key. For more information about security recommendations, see Web Service Security for Windows Phone.

Each Windows Phone application is limited to a maximum of 6 simultaneous outgoing connections.

Networking support for Windows Phone is based on Silverlight 3. For a full list of differences in networking support for Windows Phone between Silverlight 3 and Silverlight 4, see Networking for Windows Phone.

When porting web service client code for use in a Windows Phone application, check the Silverlight APIs to ensure that methods used in the code are supported. For more information about supported Silverlight APIs for Windows Phone, see Class Library Support for Windows Phone.

Did you find this helpful?
(1500 characters remaining)