Consuming Web Services using Xamarin

This post explains how to integrate SOAP web service with mobile applications using Xamarin.

Consuming Web Services using Xamarin
Most of the real world apps are based on data and this data resides in a database on your server. To access this data, we generally create web services and these web services can be then consumed by clients be it a 3rd party application or a mobile app. When it comes to writing web service, we can create either of REST Services, WCF Services or SOAP Services. This post explains how to integrate SOAP web service with mobile applications using Xamarin. The Xamarin mobile frameworks provide inbuilt support to consume standard SOAP 1.1 services over the HTTP application layer transport protocol.

For this post, we have created a simple SOAP based ASP.NET web service (ASMX) with a simple Helloworld() function that returns the text “Hello world from WS”.
When accessed from the browser, the web service looks like below-



Below is how the web service code looks like–

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class MyService : System.Web.Services.WebService
{

[WebMethod]
public string HelloWorld()
{
return "Hello World from WS";
}
}

Once the web service is created, we need to access it in our mobile application. We will create a basic Android application and modify this application to change the button text using our web service. To consume the web service, we need to specifically perform the following steps –
• Generate a Proxy and adding to mobile application
• Invoke the web service methods using Proxy

Generate a Proxy

The very 1st step in consuming our web service is to generate a proxy of the web service which will allow the app to connect with the service and perform operations. The proxy can be generated directly by adding a web Reference to the project. This can be done by following steps –

1. Right click the project and click “Add Web Reference”.



2. Enter the URL of your Web service and hit the arrow button to read the web service WSDL. Type a name of your choice in “Web Reference name” and click “Add reference”



3. This is to be noted that as soon as the web reference is added, a reference to “System.Web.Services.dll” gets automatically added to the project.



Invoke the web service methods using Proxy

Once the proxy has been added to the project, you can use the proxy class to call methods from your web service. The reference name you entered in Step 2 above is translated into a namespace that holds all the classes defined by your web service. VS allows full intellisense support for your web service.


Now, let us make calls to this web service from our android application. The 1st change that we’re doing is changing the text from the button with the data returned from the web service.
1. For this, we get the reference of the button in the Activity file and add a click handler to the button.
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);

// Get our button from the layout resource,
// and attach an event to it
Button button = FindViewById<Button>(Resource.Id.MyButton);
button.Click += button_Click;
}

2. In the event handler, we create an object to our proxy class.

MyWebService.MyService myServ = new MyWebService.MyService();

3. An important thing to note here is, if you are testing your app in Android emulator and your web service is hosted in localhost, the emulator will not be able to parse the localhost address. This is because it is loopback address to the emulator. To resolve this problem, we will need to set the URL of the proxy to http://10.0.2.2:PORTNUMBER/SERVICE.ASMX as shown below. If you’re not using localhost as your web service URL, you do not need to bother about this step and you can proceed to step 4.

myServ.Url = "http://10.0.2.2:21211/MyService.asmx";

4. Call the HelloWorld() and set the button text to data returned. The final event handler looks like this –

void button_Click(object sender, EventArgs e)
{

try
{
MyWebService.MyService myServ = new MyWebService.MyService();
myServ.Url = "http://10.0.2.2:21211/MyService.asmx";
Button button = FindViewById<Button>(Resource.Id.MyButton);
button.Text = myServ.HelloWorld();
}
catch (Exception ex)
{
}
}

5. Now run the application. When the app runs, the below screen appears –


6. Clicking the button will make a call to our webservice and change text of the button as below


Source Code: AndroidWebServiceVS.zip

This is a very basic example of consuming web services from Android application. Hope you like this post. Cheers!

By Nitesh Luharuka   Popularity  (10765 Views)