-
Notifications
You must be signed in to change notification settings - Fork 42
Step by step WCF client creation for iOS & Android
Sami M. Kallio edited this page Oct 11, 2013
·
4 revisions
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.
- Open SimplyMobile.sln located in the root folder
- Add Solution Folder Core/Plugins/Weather
- Create new project class library (C# -> Library) SimplyMobile.Plugins.WeatherWcfService (remove any automatically created classes) and change it to .NET 4.5 runtime
- Create WCF Proxy using SlSvcUtil (command prompt):
- C:\Program Files (x86)\Microsoft SDKs\Silverlight\v5.0\Tools>SlSvcUtil.exe
- http://www.restfulwebservices.net/wcf/WeatherForecastService.svc
- /namespace:*,SimplyMobile.Plugins.WeatherWcfService
- /directory:\SimplyMobile\Core\Plugins\Weather\SimplyMobile.Plugins.WeatherWcfService
-
Add the created source file to the project
- Right click on SimplyMobile.Plugins.WeatherWcfService project
- Add -> Add files -> select WeatherForecastService.cs
-
Solve the missing namespace errors by adding references to:
- System.Runtime.Serialization
- System.ServiceModel
-
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
-
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(); }
-
Lets wrap this into a simpler interface IWeatherService:
Task<string[]> GetCitiesByCountryAsync(string country); Task<Weather> GetWeatherAsync(string city, string country);
- Open SimplyMobile Android project from SimplyMobile/Android/SimplyMobile.sln
- Add solution folder Weather to Plugins/
- Create new Android Library Project called SimplyMobile.Plugins.WeatherWcfService.Android
- For this example we will use sharing technique Clone Project Files
- Copy the new SimplyMobile.Plugins.WeatherWcfService.Android.csproj file into SimplyMobile\Core\Plugins\Weather\SimplyMobile.Plugins.WeatherWcfService
- Delete the project from Solution and select "Delete from Disk" option
- Right-click on Plugins/Weather folder and select Add -> Add existing project -> Select Core/Plugins/Weather/SimplyMobile.Plugins.WeatherWcfService/SimplyMobile.Plugins.WeatherWcfService.Android.csproj
- On Xamarin Studio right click on the project and select Display Options -> Show all files
- Select the source code files (Ctrl + left click) and then right click -> Include to Project
- Reverse step 7 and unselect "Show all files"
- Remove "Resources" folder if you haven't done that already
- Repeat step 6 from earlier chapter: Solve the missing namespace errors by adding references to:
- System.Runtime.Serialization
- System.ServiceModel
- Compile and the Android service library is ready