【问题标题】:Xamarin.Forms Add Connected Service on WCF only generated async methodXamarin.Forms 在 WCF 上添加连接服务仅生成异步方法
【发布时间】:2019-02-18 09:42:17
【问题描述】:

我刚开始用 .Net Standard 2.0 (PCL) 项目做 Xamarin.Forms。我正在尝试使用我的 WCF Web 服务,但从未成功完成。

我创建了一个简单的 WCF,如下所示

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

实现如下

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

这是一个非常简单的 WCF,当我转到我的 Xamarin.Forms 并右键单击“连接的服务”时,没有“添加 Web 服务”,只有“添加连接的服务”,所以我选择如下

然后选择“Microsoft WCF Web Service Service Provider”

选择如下选项(我取消所有选项,因为如果我添加超过 1 个服务,它会崩溃)

当我查看创建的 reference.cs 时,只创建了异步方法。

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

1) 我可以知道为什么只创建 async 吗?是否适用于 .net 标准和核心,只会创建异步服务?当我在某处阅读时。

2) 如果是这样,我如何使用 Web 服务?

在我的 xaml.cs 文件中,我执行了以下操作,

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

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

但我遇到了错误,无法相应地工作。有人知道吗?

未处理的异常:

System.ServiceModel.FaultException`1[[System.ServiceModel.ExceptionDetail, System.ServiceModel, Version=2.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]:反序列化操作“GetHelloWorld”的请求消息正文时出错. OperationFormatter 遇到无效的消息正文。预计会找到名称为“GetHelloWorld”和命名空间“http://tempuri.org/”的节点类型“元素”。找到名为“GetHelloWorldAsync”和命名空间“http://tempuri.org/”的节点类型“元素”

【问题讨论】:

  • 为什么要直接将 WCF 服务连接到压力太大的移动应用程序,而没有明显的原因,为什么不使用轻量级 web-api?
  • 您有什么建议?我该怎么做?
  • @Jason 该示例正在使用 android 项目上的代理。我可以在.net 标准 PCL 项目上自己做吗?我正在尝试在 xaml 本身上使用它。有可能吗?
  • 是的。虽然使用 RESTful 服务会容易得多 - WCF 是一头野兽,我会尽可能避免使用它。

标签: wcf xamarin.forms async-await .net-standard-2.0


【解决方案1】:

目前,Xamarin 应用与 WCF Web 服务参考连接的服务提供程序为 .NET 标准项目 (bugzilla.xamarin.com Bug 51959) 生成的基于任务的异步 WCF 代理方法不兼容。

通过选中配置 WCF Web 服务参考屏幕上的“生成同步操作”复选框来生成旧的兼容样式的 WCF 代理方法:

使用网络服务:

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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-15
    • 2014-04-30
    • 2014-07-09
    • 2015-05-22
    相关资源
    最近更新 更多