Work with SOAP service from Windows Phone 8.1 application

With the release of Windows Phone 8.1, a new opportunity has appeared in the development of applications for the Windows / Windows phone store with a common code base. These are so-called universal applications based on a more general API and the ability to reuse XAML markup in Visual Studio 2013 right out of the box.

If a Windows store application uses WCF to work with SOAP services, then an attempt to port to Windows phone may fail. As it turned out, the System.ServiceModel namespace is no longer available. Accordingly, a replacement is required that meets the following requirements:
  • Ease of use, similar to the old Add Service Reference, where the output is the generated code of a strongly typed client service;
  • Extensibility, as practice has shown, may require support for various authentication schemes.


A SOAP request is a specially crafted XML document. All you need to do is serialize the request data in XML, put it in the Body element and send it in the HTTP POST body of the request. The structure of the answer is similar, the result is obtained from the Body element.



Based on HttpClient, we implement the base class for generating the request. As a result, we obtain a function of the following form:

publicasync Task<TResponse> CallAsync<TRequest, TResponse>(string action, TRequest request)
{
	IHttpContent httpContent = GetHttpContent(action, request);
	var response = await Client.PostAsync(EndpointAddress, httpContent);
	var responseContent = await response.Content.ReadAsStringAsync();
	return GetResponse<TResponse>(responseContent);
}


It remains only to substitute the corresponding classes TRequest and TResponse which can be obtained based on the description of the service. SOAP services are described using Web Services Description Language ( WSDL ), a language that describes the service API in the form of operations and data types.

Getting information about the service can be approached from different angles:

  • Work with raw WSDL. It is possible, but for the task at hand too laborious, we will go better along the path of least resistance;
  • Using the System.ServiceModel.Description.MetadataExchangeClient class is the most reasonable option, however, the implementation of this class does not allow working with any WSDL. If the document contains an import element , then MetadataExchangeClient falls out with an error;
  • Use the SvcUtil console utility included with the .Net Framework SDK. At the output, we get the same source code as when using the Add Service Reference from Visual Studio, which we will take as a basis.


The source code obtained using SvcUtil cannot be directly used for Windows Phone store applications due to the following unsupported designs:

  • Service client based on System.ServiceModel.ClientBase. Использовать нельзя, но можно взять за основу интерфейс сервиса.
    Attributes System.SerializableAttributeand System.ComponentModel.DesignerCategoryAttribute. These attributes are not supported, delete.
    Of properties public System.Xml.XmlElement[] Any { get; set; }. Change the type to System.Xml.Linq.XElement[].

Also popular now: