With the release of Windows Phone 8.1, a new feature has emerged in developing applications for the Windows / Windows phone store with a common code base. These are the so-called
universal applications based on a more general API and the reusability of 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 it to a 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 of the service;
- Extensibility, as practice has shown, may require support for various authentication schemes.
A SOAP request is a specially formed XML document. All you need to do is serialize the request data to XML, put it in the Body element and send it in the body of the HTTP POST request. The structure of the response is similar, the result is obtained from the Body element.

')
Based on the
HttpClient, we implement the base class to form a request. As a result, we obtain a function of the following form:
public async 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 on the basis of 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.
To obtain information about the service can be approached from different sides:
- Work with raw WSDL. It is possible, but for the task in view it is too laborious, let's go better along the path of least resistance;
- Using the System.ServiceModel.Description.MetadataExchangeClient class is the most reasonable option, but the implementation of this class does not allow working with any WSDL. If the document contains an import element, then the MetadataExchangeClient is thrown out with an error;
- Use the SvcUtil console utility included in 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 take as a basis.
The source code obtained using SvcUtil cannot be directly used for Windows Phone store applications due to the presence of the following unsupported constructs:
- Client service based on
System.ServiceModel.ClientBase. , .
System.ServiceModel.ClientBase. , .
The System.SerializableAttribute
and System.ComponentModel.DesignerCategoryAttribute
attributes. These attributes are not supported, delete.
Properties public System.Xml.XmlElement[] Any { get; set; }
public System.Xml.XmlElement[] Any { get; set; }
public System.Xml.XmlElement[] Any { get; set; }
. Change the type to System.Xml.Linq.XElement[]
.