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.
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <Button
- android:text="Show"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/btnAdd" />
- <TextView
- android:text=""
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/labelAPIResult" />
- </LinearLayout>
Write the code, mentioned below, to MainActivity.cs.
- namespace MyApp
- {
- [Activity(Label = "WCF Service in Xamarin Android App", MainLauncher = true, Icon = "@drawable/icon")]
- public class MainActivity : Activity
- {
- protected override void OnCreate(Bundle bundle)
- {
- base.OnCreate(bundle);
- SetContentView(Resource.Layout.Main);
-
- Button button = FindViewById<Button>(Resource.Id.btnAdd);
-
- button.Click += delegate
- {
- var apiResult = FindViewById<TextView>(Resource.Id.labelAPIResult);
- apiResult.Text = "";
- var request = System.Net.HttpWebRequest.Create("http:YourIPAddress:1234/MyRestService.svc/GetDetails/22");
- request.ContentType = "application/json";
- request.Method = "GET";
- using (System.Net.HttpWebResponse response = request.GetResponse() as System.Net.HttpWebResponse)
- {
- using (System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream()))
- {
- var content = reader.ReadToEnd();
- var result = System.Text.RegularExpressions.Regex.Replace(content, @"[^a-zA-Z0-9]", "");
- var addSpace = result.Replace("Name", " Name ");
- var addedSpace = addSpace.Replace("Id", " Id ");
- apiResult.Text = addedSpace;
- }
- }
- };
- }
- }
- }
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.
- <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.