如何在 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"的请求消息.操作格式化程序遇到无效的消息正文.期望找到节点类型具有名称GetData"和命名空间http://tempuri.org/"的元素".找到名称为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 代理方法不兼容,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
开关在
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屋!