Click here to Skip to main content
15,880,608 members
Articles / Mobile Apps

Xamarin Form Consuming WCF

Rate me:
Please Sign up or sign in to vote.
3.76/5 (12 votes)
15 Jun 2016CPOL2 min read 39.8K   13   4
An absolute trick to consume WCF WebService at Xamarin Form PCL root

Introduction

Xamarin Form could be the most attractive tool on mobile App development. But right now, consuming WCF from Xamarin Form PCL can be still a kind of challenge. To get this feature, our development team spent a lot of effort. The final success approach is rather tricky. To share with the community, we would like to post a detailed step by step description here.

Background

The development environment is Windows 10 Home and Visual Studio 2015 Community. We tried a lot to create a WCF proxy in PCL based on online documentation and posts. But none of them works as is. Later, we tried a little bit other way, and it works.

Step By Step Description

1. Create a solution of Xamarin Form application with highest .NET version:

Image 1

2. Create an additional project of WCF application:

Image 2

3. The Solution Explorer looks like this:

Image 3
Remarks: Build project WcfService1. Otherwise it cannot be referenced.

4. Try to add the WebService1 into PCL as service reference:

Image 4
You see, there is no menu item Service Reference… beneath the menu item Reference…. This is the key point of this post - get the menu item Service Reference… appearing.

All we need to do is setup Profile 78 on PCL. Following approach makes the tricky:

5. Temporally disable the NuGet package by means of changing the package name like following example: Change packages.config to packages.config.BAK:

Image 5
Without this approach, the project settings change to Profile 78 might not be allowed.

6. Now right click the PCL project to open Properties:

Image 6

7. Click button Change… to open the dialogbox Change Targets:
Change to settings showing above then click button OK. Change packages.config.BAK back to packages.config.

Image 7

8. Now repeat to try to add the WCF proxy, the menu item “Service Reference…” should appear:

Image 8
Click the menu item Service Reference…, dialogbox Add Service Reference will appear. See the next item:

9. Clicking button Discover, it find the Service1 just created:

Image 9
Click button OK.

10. The WCF proxy is created as ServiceReference1:

Image 10

11. To test the proxy, create a method callWCF() in root file App.cs and revoke from inside the contractor in an asynchronously manner:

public App()
{
    …
    callWCF();
}


private void callWCF()
{
    var wcf = new ServiceReference2.Service1Client();
    wcf.GetDataCompleted += Wcf_GetDataCompleted; // Receive feedback asynchronously
    wcf.GetDataAsync(123456); // Send request asynchronously
}

private void Wcf_GetDataCompleted(object sender, ServiceReference2.GetDataCompletedEventArgs e)
{
    string str = e.Result; // This value should be: "You entered: 123456"
    throw new NotImplementedException();
}

By this case, the retrieved value from WCF should be “You entered: 123456” at point e.Result.

12. Note: The WebService method GetData() is as attached as following:

public string GetData(int value)
{
    return string.Format("You entered: {0}", value);
}

Points of Interest

The trick here to realize Profile 78 could be an exited approach to resolve WCF sonsuming issue.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer Hiway International Ltd
Canada Canada
We are software development / research firm. Be composed of crazy and energy full dream chasers, we focus on tomorrow software technique, to achieve from impossible to possible.

Comments and Discussions

 
QuestionAny advice on converting to secure endpoint Pin
littleGreenDude20-Jun-17 3:32
littleGreenDude20-Jun-17 3:32 
I followed your suggested approach for implementing WCF in the PCL. However, I'm having difficulty getting it to work under https.

More background and details...

environment notes: iOS iPhone 7 Plus simulator WCF endpoint hosted on IIS 8 with wildcard cert VS 2017

Previous testing done over http to WCF internally hosted IIS endpoint worked fine.

Confirmed that the message service WSDL is accessible and includes https

On the client side, right clicked the service reference and changed the URL to the HTTPS endpoint service reference.

VS indicates that is connecting to the service and updating the configuration. The app then builds and runs on the simulator. However, posts to the message service that were successful under http are now returning ACCESS DENIED when hitting the https message service endpoint.

What other steps are required? Does the certificate have to be referenced from the client app?

Any chance of a follow on article that migrates the PCL WCF implementation to a secure (SSL cert) end point?
GeneralSystem.Reflection.TargetInvocationException: An exception occurred during the operation, making the result invalid. Pin
Member 1301772022-Feb-17 7:52
Member 1301772022-Feb-17 7:52 
GeneralRe: System.Reflection.TargetInvocationException: An exception occurred during the operation, making the result invalid. Pin
Member 1333526029-Jul-17 2:28
Member 1333526029-Jul-17 2:28 
QuestionCrashing Visual Studio Pin
snoei29-Dec-16 23:52
snoei29-Dec-16 23:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.