How To Consume WCF Basic Service from Xamarin for Android

SoloSolo Matt SoloUSMember

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!

«1

Posts

  • ChrisHonselaarChrisHonselaar Chris Honselaar 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 Martin Baulig DEMember 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 Kruti Chudgar 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 Chris Honselaar 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 Chris Honselaar 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?

  • SoloSolo Matt Solo 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 Chris Honselaar 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 SanderE 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 Chris Honselaar 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:

    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?

  • CheesebaronCheesebaron Tomasz Cielecki DKMember, Insider, University ✭✭✭✭
    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.

    http://james.newtonking.com/archive/2012/04/24/json-net-4-5-release-4-portable-class-library-build.aspx

  • SanderESanderE SanderE 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.

  • SoloSolo Matt Solo USMember

    @ChrisHonselaar

    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 Chris Honselaar 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.

  • SoloSolo Matt Solo 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?

  • ChrisHonselaarChrisHonselaar Chris Honselaar NLBeta ✭✭✭
    edited June 2013
    • Consuming REST services: The Xamarin docs actually have a pretty good overview on some of the main options.
    • Creating REST services, again lots of great options, just a few:
      • use the standard ASP.Net Web API: easy, fast, no 3rd party dependencies
      • use ServiceStack: integrated 3rd party (but open source) solution for both producing and consuming REST services
      • you can even use WCF to create a REST service

    What is the most productive depends on your experience and what architectural style you feel most comfortable with. For a Green Field project, I would personally probably go for ServiceStack end-to-end.

    The main advantages of RESTful services are commonly cited as:

    • simplicity (makes for an easier debugging / testing experience)
    • usable from pretty much anything that talks HTTP (try talking to a WCF/SOAP service from Javascript without pulling your hair out..)
    • better performance (less protocol overhead)
    • provide binary data/documents without wrapping

    WCF also has its unique advantages, such as the use of data contracts and automatically generated proxies. However, with a solution like ServiceStack, you are pretty much taken care of in that regard also.

    Most of the other WCF features are not very popular outside of the enterprise space (WS-Reliable Messaging / WS-AtomicTtransactions / WS-SecureConversation).

  • SoloSolo Matt Solo USMember

    @Chris.

    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 sucheendarnath k 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 Chris Honselaar NLBeta ✭✭✭

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

  • KathirVelKathirVel sucheendarnath k 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("http://192.168.0.217/testjson/service1.svc/string/2"); 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 Chris Honselaar NLBeta ✭✭✭

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

  • KathirVelKathirVel sucheendarnath k 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 sucheendarnath k USMember
    edited July 2013

    Hi, 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();
    view.FindViewById(Android.Resource.Id.Text1).SetTextColor(Android.Graphics.Color.Black); return view;

    }

  • KathirVelKathirVel sucheendarnath k USMember

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

  • ChrisHonselaarChrisHonselaar Chris Honselaar 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 sucheendarnath k USMember

    Hi chris,

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

    I am waiting for your response.

  • manishtyagimanishtyagi manish tyagi USMember

    Hi Chris, As per your docs ([http://docs.xamarin.com/guides/cross-platform/application_fundamentals/introduction_to_web_services]) 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.

«1
Sign In or Register to comment.