Forum Xamarin.Android

How To Consume WCF Basic Service from Xamarin for Android

I have build a basic WCF service that does nothing but add two numbers together. This service is accessible via basicHTTPbinding and is working correctly. I am able to create new ASP.NET applications and consume this service when adding a service reference to the WSDL file.

However, I am having lots of trouble getting this service to work in Xamarin's Android Emulator.

Here is what I did (and you can to to reproduce):

  1. Create a basic WCF service project and run it so that you get your dynamic port.
  2. Within a new Xamarin project, setup your AVD and get that thing booted up (it takes a while)
  3. Add a "Web Reference" and use the URL to your WSDL (i.e. http://localhost:87336:/Service.svc?WSDL) and set your Web Reference Name for access.
  4. Add your code
    SampleService.AddingService addServ = New SampleService.AddingService();
    SameleService.AddNumbersDC addNum = new SampleService.AddNumbersDC():
    addNum.num1 = 15;
    addNum.num2 = 35;

  5. Here is my issue, the WCF "AddNumbers" method accepts a datacontract of "AddNumbersDC" as its ONLY argument and returns and integer.
    When I am trying to consume the service within my application, it is requiring three arguments. The first is the datacontract, and the second and third arguments are OUT parameters (which I don't understand because even when I try to use them it errors out with System.Net.WebExcption.

Am I missing something here or is there no way to consume WCF services using Xamarin? Now, I did read the partial tutorial explaining that you need to generate your "AddingService.cs" files using Silverlight's SlSvcUtil.exe commands. I did not have any luck with this either.

Please help all!


  • ChrisHonselaarChrisHonselaar NLBeta ✭✭✭

    Now, I did read the partial tutorial explaining that you need to generate your "AddingService.cs" files using Silverlight's SlSvcUtil.exe commands. I did not have any luck with this either.

    That's the right direction. So what error messages are you getting with that, or where specifically are you getting stuck? Can you attach the Android Log and IIS Log?

  • MartinBauligMartinBaulig DEXamarin Team Xamurai

    If you think that it's generating an incorrect client proxy, could you please also attack the "AddingService.cs" file and the WSDL that you generated it from?

  • technoNJtechnoNJ INMember

    Hi Matt,
    I am having similar problem consuming WCF on my android application. Please let me know if you had any luck with this. Basically I want to retrieve a set of data from a SQL table using WCF, store it in a list and then consume it on my Xamarin Android App.

    So far:
    -- I have been able to create the WCF webservice and populate the list with the appropriate data. -- I have added the service as a Web Reference (using the WSDL) to my application.

    However I am not able to find how to consume this service in the app? The tutorial I read on Xamarin website is for consuming WCF on iOS app. Is it possible to do the same for android app? If so, please guide me with the process.

    Thanks a lot!

  • ChrisHonselaarChrisHonselaar NLBeta ✭✭✭

    @technoNJ - yes, the basic process is the same. Where are you getting stuck exactly?

  • ProfunctionalProfunctional USMember

    Is consuming WCF services supported in Android?

  • ChrisHonselaarChrisHonselaar NLBeta ✭✭✭

    @Profunctional, yes. Please see the documentation.
    However, if you have a choice, REST services are recommended.

  • ProfunctionalProfunctional USMember

    I'd also like to use REST services, but none of the JSON parsers are cross compatible. I just downloaded Newtonsoft's parser and there are two dlls. One for ios and one for Android. That would mean no sharing of a controller class to handle parsing. Is this correct?

  • MattSoloMattSolo USMember

    I would like to come back here to let you know what I have found "the hard way". First and foremost, the Android Emulator that ships with Xamarin cannot locate "localhost". You can test this by accessing your WSDL file in your computers browser (e.g. http://localhost:87336:/Service.svc?WSDL) and then fire up the world's slowest emulator and try to pull up that same WSDL in that. You will not be able to locate it. I supposed that I need to publish the "sample" web service to a real web server and try again but this is a lot of work just to test a product like Xamarin.

    Does anyone know if I am doing something wrong? I cannot turn on WiFi in the emulator so that it is on my network (this makes sense tho). Also, replacing "localhost" with the machine name did not help either.

    So part of the reason why the emulator is not able to consume my services has been because it cannot locate them using the URI I provided.

    (Very Complicated)

  • ChrisHonselaarChrisHonselaar NLBeta ✭✭✭

    The Android Emulator has its own IP address entirely (in contrast to the iOS simulator). You will need to open up your local machines firewall and use its IP address.

  • SanderESanderE USMember ✭✭

    I'd also like to use REST services, but none of the JSON parsers are cross compatible. I just downloaded Newtonsoft's parser and there are two dlls. One for ios and one for Android. That would mean no sharing of a controller class to handle parsing. Is this correct?

    You can still share a controller class that handles the parsing between Android and iOS, even though they are in seperate assemblies. You can create an iOS Library project (YourProject.Shared.iOS) which holds a reference to NewtonsoftJson.MonoTouch.dll and a sepearte Android library (YourProject.Shared.Android) which holds a reference to NewtonsoftJson.MonoDroid.dll and use file linking to include the same Controller.cs file in both of these projects.

    Because the namespacing of the newtonsoft library is the exact same in both assemblies you will be able to build both projects with the same source file, each using their own version of the newtonsoft assembly.

  • ChrisHonselaarChrisHonselaar NLBeta ✭✭✭
    edited June 2013

    I'd also like to use REST services, but none of the JSON parsers are cross compatible

    Json.Net is available as a Portable Class Library, or you can create your own PCL out of the source code. So, it's cross-platform compatible with only one .DLL.

  • ProfunctionalProfunctional USMember

    @sanderE - I don't see a Newtonsoft.Json.Monotouch.dll.

    There are two folders with the download:


    This is what I'm confused about. If my controller class is to be shared, why is the dll dependent on ios or android?

  • CheesebaronCheesebaron DKInsider, University mod
    edited June 2013

    There is a PCL version of the JSON.Net. As far as I know the API is the same on both versions of the DLL.

  • SanderESanderE USMember ✭✭
    edited June 2013

    here are two folders with the download:

    android/Newtonsoft.Json.dll ios/Newtonsoft.Json.dll

    This is what I'm confused about. If my controller class is to be shared, why is the dll dependent on ios or android?

    Because they are simply built for the two different target platforms, but behave the same way. Using the PCL version is also an option, but using the Newtonsoft.Json.dll assembly from the android folder in an Android project and using the iOS assembly in the iOS project while using file linking to reference the same controller file would also still work.

  • MattSoloMattSolo USMember


    Forgive my elementary question here, but how do I find the emulator's IP address? Do I have to do anything to the network's firewall? I would think that since I have installed Xamarin (and its emulator) in Visual Studio that consuming a localhost WCF service would be a natural capability that I wouldn't have to specifically setup.

  • ChrisHonselaarChrisHonselaar NLBeta ✭✭✭
    edited June 2013

    No, you might have to try disabling your local Windows/OS X firewall. Although this should not usually be necessary.

    Getting your emulator's IP for local access can be done in code (check here in the ProtoPad source code). But, why would you need that? I assume you're trying to reach your desktop Service from Android, not the other way around? So, you just need to get your desktop IP address instead, through ipconfig or what have you.

    Again, also make sure you have made a very conscious decision for using WCF SOAP instead of more normal REST services. WCF at this point (from a mobile client at least) could only be really recommended if you need to be working with an existing WCF infrastructure for some reason, like having to call a corporate intranet service that you just cannot change to REST.

  • MattSoloMattSolo USMember

    Thanks for the link about the emulator.

    My goal is to have a business layer that is easily consumed by a web application, desktop application, and mobile clients.

    I chose WCF because all of the Microsoft platforms that I develop for easily consume WCF.

    I am, however, open to choosing alternative architecture styles.

    What is the most productive way of creating RESTful web services in dotNet that can be consumed (easily) by web, desktop, and mobile apps?

  • MattSoloMattSolo USMember


    U R awesome!

    Your thorough answers and willingness to explain high-level concepts is what makes these forums such a resource.

    Based on the information you have sent, I am going to opt for the RESTful WCF services.

    Implementing this on the back end will work great for all of my apps.

  • KathirVelKathirVel USMember ✭✭

    I am new bee to Monodriod Development. We were using WCF RESTful service for getting online database data's and the Json output is '[{"Id":20,"Name":"Leo Messi"},{"Id":3,"Name":"Ronaldo"}]'. . How to consume this json to Spinner drop down in Monodriod.
    Please provide some samples to work on the same.

  • ChrisHonselaarChrisHonselaar NLBeta ✭✭✭

    @sucheendarnathk - just use any of the REST client frameworks I mentioned above and this should be very easy.

  • KathirVelKathirVel USMember ✭✭

    I have tried to get Json array from Monodroid via REST service. But its failed in response.
    It showing error as
    " {System.Net.WebException: The request timed out
    at System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult) [0x00000] in :0
    at System.Net.HttpWebRequest.GetResponse () [0x00000] in :0
    at SampleSpinner.Activity1.OnCreate (Android.OS.Bundle bundle) [0x00039] in e:\Kathir\Project\MonoForAndriod\SampleApps\SampleSpinner\Activity1.cs:45 }"

    Find below my sample for retrieving Json collection array
    var request = HttpWebRequest.Create("");
    request.ContentType = "application/json";
    request.Method = "GET";
    using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
    var content = reader.ReadToEnd();

    How to retrieve the RESTful service data in Mono. Kindly suggest how to retrieve data and assign to spinner control.

  • ChrisHonselaarChrisHonselaar NLBeta ✭✭✭

    Have you disabled the firewall? Even the Android emulator runs on its own IP address.

  • KathirVelKathirVel USMember ✭✭

    Ya i have retrieved the Json data in Mono. But How to assign the Json (Array collection) to Spinner control with display and value member fields.Kindly suggest.

  • KathirVelKathirVel USMember ✭✭
    edited July 2013

    we are using custom adapter class for binding spinner controls. We were trying to achieve one custom adapter with many spinner binding . we want to set different Display member for spinner's. But we couldn't find which spinner control called in GetView method.
    Kindly suggest how to achieve the same. Find below my sample code

    public override View GetView(int position, View convertView, ViewGroup parent)

    View view = convertView; // re-use an existing view, if one is available
    if (view == null) // otherwise create a new one
    view = context.LayoutInflater.Inflate(Android.Resource.Layout.SimpleListItem1, null);

    // Here i need to check for which spinner is called. So that we can change Display member accordingly.
    view.FindViewById(Android.Resource.Id.Text1).Text = items[position].Name.ToString();
    return view;


  • KathirVelKathirVel USMember ✭✭

    Kindly guide me. I am waiting for your kind reply..

  • ChrisHonselaarChrisHonselaar NLBeta ✭✭✭

    Sorry, I am really busy right now due to workload and family stuff. However, what you are looking to do is very easy and standard, just search this forum or Google and you will find lots of examples.

  • KathirVelKathirVel USMember ✭✭

    Hi chris,

    We tried google searching but couldn't find a solution. Kindly suggest us..

    I am waiting for your response.

  • manishtyagimanishtyagi USMember

    Hi Chris,
    As per your docs ([]) link, I created a wcf service, using SilverlightServiceModel Proxy Generation Tool (SLSvcUtil) created proxy, Hosted this service to server.
    I am able to consume this service to my code but when during invocation of method of wcf I got System.Reflection.TargetInvocationException.

    so do you have any solution of this please help me or send me the link about the same.

  • gokhanbeyazgokhanbeyaz USMember

    project is very lacking.not worth the money to give.very few examples of.No connection to the SQL Server instances.Do Norhtwind sample database and application examples.Use simple example WebService.

  • JavierLealJavierLeal USMember

    Hi all, I have the same problem with System.Reflection.TargetInvocationException service, as I have no option to do a Rest service if the question is ultimately must say that WCF does not support Xamarin or some configuration? I read in a forum that is a theme versions will be true?

  • PallaviAgPallaviAg USMember

    Hi ,

    Even I have created WCF rest service with JSON format . I want consume that service for Android and iOS development in Xamarin for Visual Studio. I tried the example mentioned in xamarin documentation but is very simple example. I want display Json Object but not able to do so. I am searching any example or tutorial which can help me. But No luck ...if some body has figured it out please post a sample. it would be highly appreciated.


  • SKallSKall USMember ✭✭✭✭

    I have not played with WCF services in a while but I did write an example for Android and iOS. I never completed the guide but you can follow the steps to get the service running. It is using a public weather WCF service and works for iOS, Android and there is also WP8 sample there.

    Copy the code and give it a try. If you have any issues let me know and I will be glad to help out.

  • PallaviAgPallaviAg USMember

    Hi Sami, Thanks for helping.
    Basically I need one simple example for xamarin for android and iOS. where if we click on button then record is added into the databse. Through the WCF service .Not sure how to do that. I have added the code snippest of WCF service here and I am using webHTTP binding for wcf service.

    please have a look

Sign In or Register to comment.