2

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

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

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

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

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!

4

1 回答 1

6

You are not following the docs.

Quote from that page:

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.

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.

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

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

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

于 2013-09-30T10:52:55.503 回答