我用 .netstandard 2.0 作为 PCL 项目创建了一个 Xamarin.Forms 项目。我正在尝试在该项目中使用 WCF 服务。我已经为 WCF 服务添加了连接服务。当我尝试调用服务中提供的任何方法时,它会给出如下错误:
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 版本更改为旧版本,但它给出了相同的错误。
最佳答案
目前,Xamarin 应用程序与 WCF Web Service Reference 的基于任务的异步 WCF 代理方法不兼容。连接的服务提供程序为 .NET Standard 项目生成 (bugzilla.xamarin.com Bug 51959)。
生成旧的、兼容样式的 WCF 代理方法的一种方法是运行 SvcUtil.exe
与 /async
和 /tcv:Version35
在 Developer Command Prompt 中切换.这将生成同步代理方法、开始/结束样式异步编程模型 (APM) 回调代理方法和基于事件的代理方法,所有这些都与 Xamarin 应用程序兼容。
(注意:如果您忽略 /async
开关,SvcUtil.exe
将生成更新的、不兼容的基于任务的代理方法。)
关于wcf - 如何在 Xamarin.Forms 项目中使用 .netstandard 中的 WCF 服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47989563/