无法使用 Xamarin 和 WCF 访问 Web 服务 [英] Can't access webservice using Xamarin and WCF

查看:16
本文介绍了无法使用 Xamarin 和 WCF 访问 Web 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Xamarin 和 WCF 使用公共 Web 服务.对于此演示,我将使用 Xamarin.iOS.

I would like to consume a public web service using Xamarin and WCF. For this demo, I'll be using Xamarin.iOS .

这是我尝试使用的(公共)网络服务:

This is the (public) webservice I'm trying to consume:

http://www.webservicex.net/globalweather.asmx?WSDL

在 Xamarin Studio 中,我添加了一个带有来自 Web 服务的 URL 的 Web 引用.所选框架设置为 Windows Communication Foundation (WCF).

Inside Xamarin Studio, I add a Web Reference with the URL from the webservice. The selected Framework is set to Windows Communication Foundation (WCF).

现在,我使用以下代码连接服务:

Now, I'm using the following code to connect with the service:

var _client = new GlobalWeatherSoapClient();
_client.BeginGetWeather ("Berlin", "Germany", (ar) => {
    var result = _client.EndGetWeather(ar);
}, null);

在执行此代码时,我收到 System.NullReferenceException.这就是问题所在,为什么它不能正常工作?

When executing this code, I'm getting a System.NullReferenceException. This is the problem, why isn't it working correct?

最奇怪的部分:当我不使用 WCF,而是选择 .NET 2.0 Web 服务 作为框架时,一切似乎都正常.

The strangest part: When I'm not using WCF, but select .NET 2.0 Web Services as Framework, everything seems to be working fine.

我看不出我的 WCF 代码有什么问题 - 根据 文档,一切正常.

I can't see what's wrong with my WCF code - according to the docs, everything should work ok.

希望有人能帮帮我!提前致谢!

I hope somebody can help me out! Thanks in advance!

推荐答案

您没有关注 文档.

引自该页面:

要生成 WCF 服务的代理以在 Xamarin.iOS 项目中使用,使用 Microsoft SilverlightServiceModel 代理生成工具(SLSvcUtil) 随 Windows 上的 Silverlight SDK 一起提供.这个工具允许指定可能需要的附加参数保持与服务端点配置的合规性.

To generate a proxy to a WCF service for use in Xamarin.iOS projects, use the Microsoft SilverlightServiceModel Proxy Generation Tool (SLSvcUtil) that ships with the Silverlight SDK on Windows. This tool allows specifying additional arguments that may be required to maintain compliance with the service endpoint configuration.

因此,首先需要使用 slsvcutil 在 Windows 机器上创建代理.通过 Xamarin Studio 向项目添加 WCF 引用不起作用.它仅适用于 .NET 2.0 Web 服务,这就是该选项可以的原因.

So, first thing is that you need to create a proxy on a Windows machine with slsvcutil. Adding WCF references to project through Xamarin Studio does not work. It only works for .NET 2.0 Web Services, that's why that option is OK.

第二件事,在使用 slsvcutil 在 Windows 上创建代理后,您需要将其添加到您的项目中.

Second thing, after you have created your proxy on Windows with slsvcutil, you need to add it to your project.

第三,你需要像这样初始化它,使用基本的http绑定(同样,来自上面链接的代码):

Third, you need to initialize it like this, with basic http binding (again, code from the above link):

var binding = new BasicHttpBinding () {
        Name= "basicHttpBinding",
        MaxReceivedMessageSize = 67108864,
};
//...
client = new Service1Client (binding, new EndpointAddress ("http://192.168.1.100/Service1.svc"));

建议:忘记 iOS 上的 WCF.该文章包含有关客户端-服务器通信的其他方式的非常有用的信息.

Suggestion: forget about WCF on iOS. That article contains very useful information on other means of client-server communication.

这篇关于无法使用 Xamarin 和 WCF 访问 Web 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆