如何在Xamarin.Forms项目的.netstandard中使用WCF服务? [英] How to use WCF services in .netstandard with Xamarin.Forms project?
问题描述
我用.netstandard 2.0作为PCL项目创建了一个Xamarin.Forms项目。我正在尝试在该项目中使用WCF服务。我已为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,版本= 2.0.5.0,区域性=中性,
PublicKeyToken = 31bf3856ad364e35]]:反序列化
操作的请求消息正文时出错GetData。 OperationFormatter
遇到无效的消息正文。预期会找到名称为 GetData且名称空间为 http://tempuri.org/的节点类型
'Element'一个>。
找到节点类型'Element',名称为'GetDataAsync'和名称空间
' http://tempuri.org/ '
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/'
我也尝试将.netstandard版本更改为较旧的版本,但它给出了相同的错误。
I've also tried to change the .netstandard version to an older version but it gives the same error.
推荐答案
目前,Xamarin应用程序与 WCF Web服务参考连接的服务提供商为.NET Standard生成项目(bugzilla.xamarin.com 错误51959 )。
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).
一种生成较旧的兼容样式的WCF代理方法的方法是运行 SvcUtil.exe
和 / async
和 / tcv:Version35
在开发人员命令提示符。这将生成同步代理方法,Begin / End样式异步编程模型(APM)回调代理方法以及基于事件的代理方法,所有这些方法均与Xamarin应用程序兼容。
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.
(注意:如果省略 / async
开关, SvcUtil.exe
将生成较新的不兼容的任务的代理方法。)
(Note: If you leave out the /async
switch, SvcUtil.exe
will generate the newer, incompatible Task-based proxy methods.)
这篇关于如何在Xamarin.Forms项目的.netstandard中使用WCF服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!