Skip to content

Step by step WCF client creation for iOS & Android

Sami M. Kallio edited this page Oct 11, 2013 · 4 revisions

Creating the core base

For this step you will need SlSvcUtil to create the proxy files. You can download Microsoft Silverlight SDK if you don't have it already in your machine.

  1. Open SimplyMobile.sln located in the root folder
  2. Add Solution Folder Core/Plugins/Weather
  3. Create new project class library (C# -> Library) SimplyMobile.Plugins.WeatherWcfService (remove any automatically created classes) and change it to .NET 4.5 runtime
  4. Create WCF Proxy using SlSvcUtil (command prompt):
  1. Add the created source file to the project

    • Right click on SimplyMobile.Plugins.WeatherWcfService project
    • Add -> Add files -> select WeatherForecastService.cs
  2. Solve the missing namespace errors by adding references to:

    • System.Runtime.Serialization
    • System.ServiceModel
  3. Create a partial class for the WeatherForecastServiceClient

    • Create DefaultEndPoint from the config file created by the SlSvcUtil
    • Create DefaultBindings
    • Create a default static property returning new instance with the default settings
  4. Lets give it a quick try from the console application:

    public static void Main (string[] args)
    {
    	IWeatherForecastService service = WeatherForecastServiceClient.Default;
    
        AutoResetEvent e = new AutoResetEvent(false);
    
    	service.BeginGetCitiesByCountry (
            "USA", 
            (r) =>
    		{
    			var cities = service.EndGetCitiesByCountry (r);
                foreach (var city in cities)
                {
                    Console.WriteLine(city);
                }
                e.Set();
    		}, 
    		null);
    
        e.WaitOne();								
    }
    
  5. Lets wrap this into a simpler interface IWeatherService:

     Task<string[]> GetCitiesByCountryAsync(string country);
    
     Task<Weather> GetWeatherAsync(string city, string country);
    

Creating Android class library from the core files:

  1. Open SimplyMobile Android project from SimplyMobile/Android/SimplyMobile.sln
  2. Add solution folder Weather to Plugins/
  3. Create new Android Library Project called SimplyMobile.Plugins.WeatherWcfService.Android
  4. Copy the new SimplyMobile.Plugins.WeatherWcfService.Android.csproj file into SimplyMobile\Core\Plugins\Weather\SimplyMobile.Plugins.WeatherWcfService
  5. Delete the project from Solution and select "Delete from Disk" option
  6. Right-click on Plugins/Weather folder and select Add -> Add existing project -> Select Core/Plugins/Weather/SimplyMobile.Plugins.WeatherWcfService/SimplyMobile.Plugins.WeatherWcfService.Android.csproj
  7. On Xamarin Studio right click on the project and select Display Options -> Show all files
  8. Select the source code files (Ctrl + left click) and then right click -> Include to Project
  9. Reverse step 7 and unselect "Show all files"
  10. Remove "Resources" folder if you haven't done that already
  11. Repeat step 6 from earlier chapter: Solve the missing namespace errors by adding references to:
    • System.Runtime.Serialization
    • System.ServiceModel
  12. Compile and the Android service library is ready

Create Android test application (to be continued shortly...)