How to use WCF services in .netstandard with Xamarin.Forms project? - wcf

I've created a Xamarin.Forms project with .netstandard 2.0 as PCL project. I'm trying to consume WCF services in that project. I've added the connected service for WCF service. When I'm trying to call any method provided in the service, it gives the error as below:
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 'GetData'. OperationFormatter
encountered an invalid Message body. Expected to find node type
'Element' with name 'GetData' and namespace 'http://tempuri.org/'.
Found node type 'Element' with name 'GetDataAsync' and namespace
'http://tempuri.org/'
I've also tried to change the .netstandard version to an older version but it gives the same error.

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).
One way to generate an older, compatible style of WCF proxy methods is to run SvcUtil.exe with the /async and /tcv:Version35 switches in a Developer Command Prompt. That will generate synchronous proxy methods, Begin/End style Asynchronous Programming Model (APM) callback proxy methods, and event-based proxy methods, all of which are compatible with Xamarin apps.
(Note: If you leave out the /async switch, SvcUtil.exe will generate the newer, incompatible Task-based proxy methods.)

Generate an older compatible style of WCF proxy methods via checked "Generate Synchronous Operations" checkbox on Configure WCF Web Service Reference screen:
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);

Related

How to consume WCF service using .net 5

I am trying add connected services to generate the proxy class and got this error.
Creating WCF Service proxy class
You can try to use dotnet-svcutil tool. The Windows Communication Foundation (WCF) dotnet-svcutil tool is a .NET tool that retrieves metadata from a web service on a network location or from a WSDL file, and generates a WCF class containing client proxy methods that access the web service operations.
Similar to the Service Model Metadata - svcutil tool for .NET Framework projects, the dotnet-svcutil is a command-line tool for generating a web service reference compatible with .NET Core and .NET Standard projects.
For more information about "WCF dotnet-svcutil tool for .NET Core", you can refer to this link.

WCF deserializing body error because of adding async behind the method name

I've created a WCF service with a method GetTestValue. I've also created a test application to test this service.
When I add this WCF service with connected service to the test application I can only call GetTestValueAsync, there is no GetTestValue method. Somehow this add process add this async thing behind the method name. So in this test application, the WCF call works fine when I call GetTestValueAsync. I get the result back.
Then I've created a Xamarin cross application app where I added this WCF service too, and when I call the GetTestValueAsync from this application I get the following error:
Error in deserializing body of request message for operation 'GetTestValue'. OperationFormatter encountered an invalid Message body. Expected to find node type 'Element' with name 'GetTestValue' and namespace 'http://tempuri.org/'. Found node type 'Element' with name 'GetTestValueAsync' and namespace 'http://tempuri.org/'
Somehow strange in the test project it works fine, and in the Xamarin cross application not.
Did someone have the same problem?
Why is this connected service always this Async to my method name?
How can I stop this Async being added to the method name?
Thanks for any help.
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 (bugzilla.xamarin.com Bug 51959).
One way to generate an older, compatible style of WCF proxy methods is to run SvcUtil.exe with the /async and /tcv:Version35 switches in a Developer Command Prompt. That will generate synchronous proxy methods, Begin/End style Asynchronous Programming Model (APM) callback proxy methods, and event-based proxy methods, all of which are compatible with Xamarin apps.
(Note: If you leave out the /async switch, SvcUtil.exe will generate the newer, incompatible Task-based proxy methods.)
Duplicate: By chance, I replied on a more recent duplicate of this question first:
How to use WCF services in .netstandard with Xamarin.Forms project?
I know this is old, but I had the same problem and searched a while. When you are adding the Reference in your project, you can check "generate synchronized operations" and the "Async"-suffix aren't generated anymore. My VS runs in german language, so I just translated the options. If your adding new methods the setting will be considered too.
Greetings from germany.

.Net Core calling all "old" WCFs possible?

I thought I would try out .Net Core (v1.1.2) and one of the things I wondered about was if it was possible to call a WCF service written in .Net Framework 4.5.2. I am using Visual Studio 2017 and installed the Microsoft WCF Web Service Reference Provider (version 0.5.10428.1201). When I ran it, it found the service, but I got this error message when trying to generate the code:
Scaffolding Code ...
Error:Warning: Unsupported message encoding value: 'Mtom'. It must be 'Text'.
Warning: Unsupported message encoding element type: 'System.ServiceModel.Channels.MtomMessageEncodingBindingElement'. It must be one of the following types: 'System.ServiceModel.Channels.BinaryMessageEncodingBindingElement', 'System.ServiceModel.Channels.TextMessageEncodingBindingElement.'
Warning: Endpoint 'WSHttpBinding_IDocumentSignService' at address 'http://xxxxxxxxxxxxx.svc' contains one or more bindings not compatible with .Net Core apps, skipping...
Error: No endpoints compatible with .Net Core apps were found.
Failed to generate service reference.
(I edited the url to http://xxxxxxxxxxxx.svc)
Does this mean that .Net Core apps can't call all WCF services yet? Will it only support a certain subset? So that if I am stuck in a world of WCF services I will have to stay away from .Net Core? Or is it simply a problem with the tooling?
I realize this has probably been asked before, but I could find no clear answer.
As explained by panagiotis's comment, you're consuming a SOAP service with MTOM encoding (what is not supported by .net Core)
Nevertheless, consuming WCF services works fine in .Net core, as long as it is not relaying on WS* bindings.
extra info here
I was also having trouble by consuming MTOM encoded SOAP services.
I explain how I fixed it here

Communicating with third-party web services from .NET

In the current project I need to communicate with third-party web services from .NET. The difficult part is constructing SOAP messages and posting to the server. After googling enough I found by using SoapClient class I can easily construct/send/receive soap message but I think for that I've to add references to WSE assemblies and it is kind of obsolete.
Is it the right way to use SoapClient for this scenario or any other better ways there in WCF?
If the service provides a .wsdl file, you can have VS generate strongly-typed proxy/wrapper classes for you by adding a service reference (this is done using the command-line tool wsdl.exe). Right-click on your project in Visual Studio and select "Add Service Reference..." and enter the URL of the .wsdl file. "Add Service Reference..." will generate client classes with methods and strongly-typed request and response types corresponding to the methods defined in the web service definition. The proxies can be configured to allow both synchronous and asynchronous method invocation.
See this MSDN article for more details.

WCF Service reference from Silverlight Class Library

I have a Silverlight application communicating with the server side through WCF services. Initially I had everything in the main Silverlight application, but now I wanted to factor our some classes to a separate Silverlight Class Library project. This however gave me some odd issues...
I wanted to factor the classes that does the WCF communication out to a separate project. So I:
Created a new project; Silverlight class library
Moved the classes from my Silverlight application to my Silverlight class library
Removed the Service reference in the application as I no longer call it from the app.
Added a Service reference from the class library project.
Now - compiling is fine and I get intellisense for the service stuff in the Class Library, so it seems to be fine. I also updated the service and got the updates in the Class Library.
But when running the application it fails when doing a service call giving the following error:
InvalidOperationException was unhandled by user code
Could not find default endpoint element that references contract 'MyServiceReference.IMyService' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.
Seems like it can't find the service, but why? I assume there should be no problem to have service references from a Silverlight Class Library as it allows me to add one?
IT can't find any service configuration - where do you have the config for the WCF service? By default, the client app (the EXE) will have a app.config that contains the service endpoints to connect to.
Also by default, class libraries (DLL's) don't have their own configuration but rely on their hosting app to provide the configuration for them.
So all in all - you're probably missing the config for the client endpoint. Most likely, it has been created as an app.config in the class library project, but that's not being used, really - you'll have to move the <system.serviceModel> section up to the main app's config (I'm not fluent in Silverlight, but you'll know where to put it).
Marc