In this lesson will we be discussing how to invoke web services from your Xamarin iOS and Android applications. There are a few different ways that these applications can connect with web service technologies. The Xamarin platform supports consuming different web service technologies, and includes in-built and third-party support for consuming RESTful, ASMX, and Windows Communication Foundation (WCF) services.
What is REST?
Representational State Transfer (REST) is an architectural style for building web services. REST requests are made over HTTP using the same HTTP verbs that web browsers use to retrieve web pages and to send data to servers. The verbs are:
- GET – this operation is used to retrieve data from the web service.
- POST – this operation is used to create a new item of data on the web service.
- PUT – this operation is used to update an item of data on the web service.
- PATCH – this operation is used to update an item of data on the web service by describing a set of instructions about how the item should be modified. This verb is not used in the sample application.
- DELETE – this operation is used to delete an item of data on the web service.
Web service APIs that adhere to REST are called RESTful APIs, and are defined using:
- A base URI.
- HTTP methods, such as GET, POST, PUT, PATCH, or DELETE.
- A media type for the data, such as JavaScript Object Notation (JSON).
The simplicity of REST has helped make it the primary method for accessing web services in mobile applications.
What is SOAP?
SOAP (Single Object Access Protocol) is a protocol specification for exchanging structured information in the implementation of Web Services in computer networks. It relies on Extensible Markup Language (XML) for its message format, and usually relies on other Application Layer protocols, most notably Hypertext Transfer Protocol (HTTP) and Simple Mail Transfer Protocol (SMTP), for message negotiation and transmission.
ASP.NET Web Service (ASMX)
ASMX provides the ability to build web services that send messages using the Simple Object Access Protocol (SOAP). SOAP is a platform-independent and language-independent protocol for building and accessing web services. Consumers of an ASMX service do not need to know anything about the platform, object model, or programming language used to implement the service. They only need to understand how to send and receive SOAP messages.
A SOAP message is an XML document containing the following elements:
- A root element named Envelope that identifies the XML document as a SOAP message.
- An optional Header element that contains application-specific information such as authentication data. If the Headerelement is present it must be the first child element of the Envelope element.
- A required Body element that contains the SOAP message intended for the recipient.
- An optional Fault element that’s used to indicate error messages. If the Fault element is present, it must be a child element of the Body element.
SOAP can operate over many transport protocols, including HTTP, SMTP, TCP, and UDP. However, an ASMX service can only operate over HTTP. The Xamarin platform supports standard SOAP 1.1 implementations over HTTP, and this includes support for many of the standard ASMX service configurations.
Generating a Proxy
A proxy must be generated in order to consume an ASMX service, which allows the application to connect to the service. The proxy is constructed by consuming service metadata that defines the methods and associated service configuration. This metadata is exposed as a Web Services Description Language (WSDL) document that is generated by the web service. The proxy is built by using Xamarin Studio or Visual Studio to add a web reference for the web service to the platform-specific projects.
The web service URL can either be a hosted remote source or local file system resource accessible via the file:/// path prefix, for example:
This generates the proxy in the Web or Service References folder of the project. Since a proxy is generated code, it should not be modified.
Windows Communication Foundation (WCF)
WCF is Microsoft’s unified framework for building service-oriented applications. It enables developers to build secure, reliable, transacted, and interoperable distributed applications.
WCF describes a service with a variety of different contracts which include the following:
- Data contracts – define the data structures that form the basis for the content within a message.
- Message contracts – compose messages from existing data contracts.
- Fault contracts – allow custom SOAP faults to be specified.
- Service contracts – specify the operations that services support and the messages required for interacting with each operation. They also specify any custom fault behavior that can be associated with operations on each service.
There are differences between ASP.NET Web Services (ASMX) and WCF, but it is important to understand that WCF supports the same capabilities that ASMX provides – SOAP messages over HTTP.
In general, the Xamarin platform supports the same client-side subset of WCF that ships with the Silverlight runtime. This includes the most common encoding and protocol implementations of WCF — text-encoded SOAP messages over the HTTP transport protocol using the BasicHttpBinding class. In addition, WCF support requires the use of tools only available in a Windows environment to generate the proxy.
Considerations for web services
When creating apps that require a web service it is important to note that the app will require constant connectivity via the mobile connection or wifi to work. This can cause some problems when users try to use your app in areas of bad connectivity or poor reception. We will look at how to check for both mobile and wifi connectivity in the demo.
In the video below I show you how to connect to a web service using an iOS project but if you are working with Android Xamarin projects, the code and steps are going to be exactly the same. Bonus!
This video will show you how to check for a Wi-Fi and Mobile Internet connections in your Android and iOS applications written with Xamarin. Once we have checked for network connectivity, I will show you how to connect to a web service using the Windows Communication Foundation (WCF).
Here is the link to the W3Schools web service used in this tutorial – http://www.w3schools.com/xml/tempconvert.asmx