2

I just begun to do Xamarin.Forms with .Net Standard 2.0 (PCL) project. I'm trying to consume my WCF web service but never got it successfully done.

I have created a simple WCF as below

[ServiceContract]
public interface IWcfConnection
{        
    [OperationContract]
    string GetHelloWorld();
}

the implementation as below

public class WcfConnection : IWcfConnection
{
    public string GetHelloWorld()
    {
        return "Hello World";
    }
}

It's a very simple WCF, when I go to my Xamarin.Forms and right click on the "Connected Service", there is no "Add Web Service", but only "Add Connected Service", so I selected that as below

enter image description here

Then select "Microsoft WCF Web Service Service Provider"

enter image description here

Select the option as below (I untick everything because if I add more than 1 service, it will crash)

enter image description here

When I look into the reference.cs created, there is only async method created.

public System.Threading.Tasks.Task<string> GetHelloWorldAsync()
{
    return base.Channel.GetHelloWorldAsync();
}

1) May I know why only async is created? Is it for .net standard and core, only async services will be created? As I read somewhere.

2) If so, how do I consume the web service?

In my xaml.cs file, I did the following,

WcfConnectionService.WcfConnectionClient client = new WcfConnectionService.WcfConnectionClient(new WcfConnectionService.WcfConnectionClient.EndpointConfiguration());

string abc = client.GetHelloWorldAsync().GetAwaiter().GetResult();

But I'm getting error and unable to work accordingly. Anybody got any idea?

Unhandled Exception:

System.ServiceModel.FaultException`1[[System.ServiceModel.ExceptionDetail, System.ServiceModel, Version=2.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: Error in deserializing body of request message for operation 'GetHelloWorld'. OperationFormatter encountered an invalid Message body. Expected to find node type 'Element' with name 'GetHelloWorld' and namespace 'http://tempuri.org/'. Found node type 'Element' with name 'GetHelloWorldAsync' and namespace 'http://tempuri.org/'

TPG
  • 2,811
  • 1
  • 31
  • 52
  • Why would you directly connect a WCF service to your mobile app that is too much pressure for no apparent reason why not just have a lightweight web-api? – FreakyAli Feb 18 '19 at 11:32
  • What is your suggestion? How should I do it? – TPG Feb 18 '19 at 13:25
  • https://learn.microsoft.com/en-us/xamarin/cross-platform/data-cloud/web-services/walkthrough-working-with-wcf – Jason Feb 18 '19 at 13:53
  • @Jason that example is consuming the proxy on android project. Can I do it itself on the .net standard PCL project? I'm trying to work it on the xaml itself. Is it possible? – TPG Feb 18 '19 at 14:42
  • Yes. Although using a RESTful service would be much easier - WCF is a beast and I would avoid it if at all possible. – Jason Feb 18 '19 at 14:45
  • A rest ful webapi middle layer may be? – FreakyAli Feb 18 '19 at 14:53
  • You mean using the asp.net web API? – TPG Feb 18 '19 at 15:18
  • There should be an option to generate the sync versions too but apparently there isn't... The only workaround I know of is to manually generate the client with `svcutil`. You'll also need to manually add your regular `System.ServiceModel` dll... I understand you're using .NET Standard here, but... VS allows you to do so with no complains and at the end [IT WORKS](https://imgur.com/rMSak4g)...:O) – jsanalytics Feb 19 '19 at 12:17
  • Well, one more thing: you can also use a `ChannelFactory` and skip altogether the whole drama with service references. Still need `System.ServiceModel.dll` though. And now you have 2 ideas...:O) – jsanalytics Feb 20 '19 at 08:53
  • After few days of struggle, I tried different approach by creating a new .net Framework project, use the "Add Service Reference.." to connect to my WCF. And then add reference to this project on my Xamarim.Forms .net Standard project. Now I'm able to call my web service with charm! Can even reference to the same object class and pass around easily. Seems like .net standard is not mature enough? – TPG Mar 20 '19 at 06:27

1 Answers1

1

At the moment Xamarin apps aren't compatible with the Task-based asynchronous WCF proxy methods that the WCF Web Service Reference connected service provider generates for .NET Standard projects (bugzilla.xamarin.com Bug 51959).

Generate an older compatible style of WCF proxy methods via checked "Generate Synchronous Operations" checkbox on Configure WCF Web Service Reference screen:

Generate Synchronous Operations

Consume the web service:

KimlikServiceReference.KPSPublicSoapClient soapClient = new KimlikServiceReference.KPSPublicSoapClient(KimlikServiceReference.KPSPublicSoapClient.EndpointConfiguration.KPSPublicSoap);
//KimlikServiceReference.TCKimlikNoDogrulaResponse response = soapClient.TCKimlikNoDogrulaAsync(TCKimlikNo, Ad, Soyad, DogumYili).Result;
bool result = soapClient.TCKimlikNoDogrula(TCKimlikNo, Ad, Soyad, DogumYili);
ishakkulekci
  • 811
  • 1
  • 10
  • 9