Consume WCF Services In Xamarin Android App

Introduction
Xamarin is a platform to develop cross-platform and multi-platform apps (like Windows phone, Android, iOS). In Xamarin, the code sharing concept is used. In Xamarin Studio, Visual Studio is also available. 
 
WCF 
Windows Communication Foundation (WCF) is a framework for building service-oriented applications. Using WCF, you can send data as asynchronous messages from one service end-point to another. A service end-point can be part of a continuously available service, hosted by IIS, or it can be a service hosted in an application. 
 
Step 1
 
 Create a new blank Android app, using Visual Studio.
 
Step 2
 
In your project solution, there is a Main.axml file. Now, open this file in designer view. Drag and drop one Button and one TextView from Toolbox window.
 
On button click event, I am going to write the code to call a WCF Service.
I have created one WCF Service for this application and hosted this Service on IIS. I am going to call this Service into Android application. For more information, click here
 
 If we run this service into a browser, then we will see the following screen shot.
 
 
 
Step 3
Write the code, mentioned below, into Main.axml.
 
 
  1. <?xml version="1.0" encoding="utf-8"?>    
  2.     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  3.         android:orientation="vertical"    
  4.         android:layout_width="match_parent"    
  5.         android:layout_height="match_parent">    
  6.         <Button    
  7.             android:text="Show"    
  8.             android:layout_width="match_parent"    
  9.             android:layout_height="wrap_content"    
  10.             android:id="@+id/btnAdd" />    
  11.         <TextView    
  12.             android:text=""    
  13.             android:layout_width="match_parent"    
  14.             android:layout_height="wrap_content"    
  15.             android:id="@+id/labelAPIResult" />    
  16.     </LinearLayout>   
 Write the code, mentioned below, to MainActivity.cs.
 
  1. namespace MyApp    
  2.     {    
  3.         [Activity(Label = "WCF Service in Xamarin Android App", MainLauncher = true, Icon = "@drawable/icon")]    
  4.         public class MainActivity : Activity    
  5.         {    
  6.             protected override void OnCreate(Bundle bundle)    
  7.             {    
  8.                 base.OnCreate(bundle);    
  9.                 SetContentView(Resource.Layout.Main);    
  10.         
  11.                 Button button = FindViewById<Button>(Resource.Id.btnAdd);    
  12.         
  13.                 button.Click += delegate    
  14.                 {                    
  15.                     var apiResult = FindViewById<TextView>(Resource.Id.labelAPIResult);    
  16.                     apiResult.Text = "";    
  17.                     var request = System.Net.HttpWebRequest.Create("http:YourIPAddress:1234/MyRestService.svc/GetDetails/22");    
  18.                     request.ContentType = "application/json";    
  19.                     request.Method = "GET";    
  20.                     using (System.Net.HttpWebResponse response = request.GetResponse() as System.Net.HttpWebResponse)    
  21.                     {    
  22.                         using (System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream()))    
  23.                         {    
  24.                             var content = reader.ReadToEnd();    
  25.                             var result = System.Text.RegularExpressions.Regex.Replace(content, @"[^a-zA-Z0-9]""");    
  26.                             var addSpace = result.Replace("Name"" Name ");    
  27.                             var addedSpace = addSpace.Replace("Id"" Id ");    
  28.                             apiResult.Text = addedSpace;    
  29.                         }    
  30.                     }    
  31.                 };    
  32.             }     
  33.         }    
  34.     }   
 Step 4
 Note
Do not forget to add the required permission of "Internet" into your application. For this, go to Project menu, click on properties of Project, then click Android Manifest. In Required Permission, check the checkbox of "Internet". This will add the permission in AndroidManifest.xml, as below.
  1. <uses-permission android:name="android.permission.INTERNET" />   
 Step 5
Now, run the application on emulator.
 
 
 
 Summary
 
This article will help the fresher candidates to learn consuming WCF Services in Xamarin Android app. 
 


Similar Articles