Forum Xamarin Xamarin.Forms

Using WCF in Shared Project

LauraGrossoLauraGrosso ITMember, University
edited September 2014 in Xamarin.Forms

Hi all, I have this doubt.

With "old" Xamarin Android projects, the way to consume WCFs was: right click on the solution, add "web reference", use web reference in code (with intellisense and all).

How to reach the same with Shared Projects in Xamarin.Forms?

With shared code projects, I cannot add a web reference to the shared code. I can do it only in PCL projects.

  • Does this mean that I have to create the web references at iOS and Android level and call the plaform-dependent implementation with the Dependency Service, similarly to what I do with SQLite classes in PCL projects? Can you call Dependency services in shared code, or is it a PCL-only possibility, whereas in Shared projects you only have conditional compilation?

  • Do I have to download the code for the WCF proxy (as I have to download the class to use SQlite in Shared projects, rather than the package?)

  • Do I have to necessarily use a PCL project to use WCF web references?

thanks for your help!


  • CraigDunnCraigDunn USXamarin Team Xamurai

    You do not have to use a PCL (your third question), however the approach you choose from the first two options is really up to you. As you noted, you cannot add references of any sort to a Shared Project so you need to either:

    • add the references to each application, or

    • get the code that would normally be in the reference and put it in the shared project

    which is basically what you already asked - and the answer is probably "either". You can use the DependencyService in Shared Projects if you want, it is just that #if compiler directives are also available and sometimes make more sense if you are going down the Shared Project route.

  • LauraGrossoLauraGrosso ITMember, University

    Thanks Craig,
    This did the trick, as you suggested:

    • I created the web reference in the Android project

    • I copied ALL THE GENERATED CODE ( and reference.cs) in the shared project

    • I changed the namespace name (not to create confusion)

    From that moment, one can reference the object (whose name was given at web reference creation time) in the shared project.

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    Craig, I've the same problem with a pcl project.... I see (in XS where I've the PCL project) that I can "Add web reference" (only for WCF... the combo is disabled). I try to use the example and XS add the reference to the PCL project, but when I try to use the namespace (namespace, XS intellisense give me only "Geco", not "www....." (if I add refernce to Android project, XS create a namespace but intellisense stop to Geco.Android....)
    It other posts someone say that XS generate incorrect code for WS (it only works in Visual Studio)...
    So, can I add a reference to a WS in a PCL project?
    Do I create reference using Visual Studio and then use the code genereted in XS?
    Or... I CAN'T Reference to WebService in PCL?
    I've not found an answer in the web....

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    Hi, I've found this solution to add web references in PCL

    looks like it's actually a XS problem that is not capable of adding web reference correctly... but a confirmation of Xamarin guys would be appreciated

    Happy 2015

Sign In or Register to comment.