Consumming ASMX Services in Xamarin Forms

Also available in portuguese

When I started work with Xamarin Forms one of the first things I needed was consumming services. When you use Xamarin Forms consumming RESTFul API services is quite easy (I will post that later). But, ASMX services will need some additional implementation for different plataforms.

First of all, I have created a cross-plataform Xamarin Forms portable project using Visual Studio 2015. For the article, I don’t use any third party component to manage services. I do like to create my own implementation for understanding purpose.

Ok, let’s get started!

What we need?

1 – An interface for the service with all methods we want. It will be at portable project.
2 – The Service Web Reference. References the service in both Xamarin.iOS and Xamarin.Droid projects.
3 – A class that implemments the interface created above. That class will be wrapper for the service itself. It must be in both Xamarin.Droid and Xamarin.iOS projects. So, yes, you will need to create two classes.
4 – Add a Xamarin Forms dependency (dependency injection) for that interface in both Xamarin.Droid and Xamarin.iOS projects.
5 – A static property in App portable project to consume the service. That property will be “injected” by Xamarin Forms.

OK, stop! how that thing will work? Let’s get the android implementation…

Let’s say you have a view and you want to get all customers from your ASMX service. You have a method that receives a criteria filter and returns a list of customers.

First, reference that service in Xamarin.Droid project.

Second, create an interface “ICustomerSoapService” and a GetAllCustomers method.

public interface ICustomer
{
        string Name {get;set;}
        string Id {get;set;}
}

public interface ICustomerSoapService
{
        Task<List<ICustomer>> GetAllCustomers(string criteria = null);
}

Ok, now create a class “CustomerSoapService” in Xamarin.Droid project that implemments that interface “ICustomerSoapService”

[assembly: Dependency(typeof(FSL.XF2.Droid.CustomerSoapService))]

namespace FSL.XF2.Droid
{
    public sealed class CustomerSoapService : ICustomerSoapService
    {
        CustomersWs.Customers service;

        public CustomerSoapService()
        {
            service = new CustomersWs.Customers()
            {
                Url = "http://localhost/FSL.WS/Customers.asmx"
            };
        }

        public async Task<List<ICustomer>> GetAllCustomers(string criteria = null)
        {
            return await Task.Run(() =>
            {
                var result = service.GetAllCustomers();

                return new List<ICustomer>(result);
            });
        }
    }
}

The “CustomersWs” code above it’s your web service reference.

In your App portable project, create a property to be inject (dependency injection) by Xamarin Forms.

public partial class App : Application
{
        private static ICustomerSoapService _customerSoapService;
        public static ICustomerSoapService CustomerSoapService
        {
            get
            {
                if (_customerSoapService== null)
                {
                    _customerSoapService = DependencyService.Get<ICustomerSoapService>();
                }

                return _customerSoapService;
            }
        }
}

The property above is lazy loader, ti will be injected by Xamarin Forms only if someone uses it.

Finally you will call the method you want in your view:

var customers = await App.CustomerSoapService.GetAllCustomers();

This is it. I hope it helped.

Consumming ASMX Services in Xamarin Forms

ASMX in Xamarin Forms: Questions, suggestions and feedbacks will be appreciated. Good luck!

Do complete download of the source code on github.
About the Author:
He works as a solution architect and developer, has more than 16 years of experience in software development on several platforms and more than 14 years only for the insurance market.