Consuming ASMX Services In Xamarin Forms

When I started working with Xamarin forms, one of the first things I required was consuming services. When you use Xamarin forms, consuming RestFull API Services is quite easy (I will post about that later) but ASMX Services will need some additional implementation for different platforms.
 
First of all, I have created a cross-platform Xamarin forms portable project, using Visual Studio 2015. For the blog, I don't use any third party component to manage the Services. I do like to create my own implementation for understanding purposes.
 
Ok, let's get started.
 
What do we need?
  1. An interface for the Service with all the methods which we want. It will be at portable project.
  2. The Service Web Reference. This references the service in both Xamarin.iOS and Xamarin.Droid projects.
  3. A class that implements the interface created above. This class will be a wrapper for the Service itself. It must be in both Xamarin.Droid and Xamarin.iOS projects. Thus, you will need to create two classes.
  4. Add Xamarin forms dependency (dependency injection) for that interface in both Xamarin.Droid and Xamarin.iOS projects.
  5. A static property in the app, which is a portable project to consume the Service. This property will be "injected" by Xamarin forms. 
OK, stop. How does that thing will work? Let's get the Android implementation.
 
Let's say you have a view and you want to get all the customers from your ASMX Service. You have a method that receives a criteria filter and returns a list of the customers.
 
First, reference that Service in Xamarin.Droid project.
 
Second, create an interface "ICustomerSoapService" and a GetAllCustomers method.
  1. public interface ICustomer  
  2. {  
  3. string Name {get;set;}  
  4. string Id {get;set;}  
  5. }  
  6.   
  7. public interface ICustomerSoapService  
  8. {  
  9. Task<List<ICustomer>> GetAllCustomers(string criteria = null);  
  10. }  
Now create a class "CustomerSoapService" in Xamarin.Droid project that implements the interface "ICustomerSoapService". 
  1. [assembly: Dependency(typeof(FSL.XF2.Droid.CustomerSoapService))]  
  2.   
  3. namespace FSL.XF2.Droid  
  4. {  
  5.     public sealed class CustomerSoapService : ICustomerSoapService  
  6.     {  
  7.         CustomersWs.Customers service;  
  8.   
  9.         public CustomerSoapService()  
  10.         {  
  11.             service = new CustomersWs.Customers()  
  12.             {  
  13.                 Url = "http://localhost/FSL.WS/Customers.asmx"  
  14.             };  
  15.         }  
  16.   
  17.         public async Task<List<ICustomer>> GetAllCustomers(string criteria = null)  
  18.         {  
  19.             return await Task.Run(() =>  
  20.             {  
  21.                 var result = service.GetAllCustomers();  
  22.   
  23.                 return new List<ICustomer>(result);  
  24.             });  
  25.         }  
  26.     }  
  27. }  
The "CustomersWs" code, mentioned above, is your Web Service reference. In your App's portable project, create a property to be injected (dependency injection) by Xamarin forms. 
  1. public partial class App : Application  
  2. {  
  3.         private static ICustomerSoapService _customerSoapService;  
  4.         public static ICustomerSoapService CustomerSoapService  
  5.         {  
  6.             get  
  7.             {  
  8.                 if (_customerSoapService== null)  
  9.                 {  
  10.                     _customerSoapService = DependencyService.Get<ICustomerSoapService>();  
  11.                 }  
  12.   
  13.                 return _customerSoapService;  
  14.             }  
  15.         }  
  16. }   
The property, mentioned above, is a lazy loader and this will be injected by Xamarin Forms only if someone uses it. Finally, you will call the method which you want in your view. 
  1. var customers = await App.CustomerSoapService.GetAllCustomers();   
Download the full source code. 
 
I hope it helps.
 
Enjoy and good luck.