Is there any supported way to call asmx web servic

2019-09-18 21:12发布

问题:

We are having trouble figuring out how to call asmx web services from cross-platform Xamarin code. There seem to be four options. The first two seem flawed, and information is scarce on the other two.

Can anyone offer some advice? Surely there must be at least one supported way to call web services from cross platform Xamarin apps.

Here are the options we're aware of so far:

  1. We tried using WCF to make the calls, from a Portable Class Library (PCL). We got some incorrect behaviour, which we put down to the fact that WCF on Xamarin is "preview" only. (The generated XML was different from that produced by the same code running under other WCF implementations, and was not accepted by the target server.) Furthermore, WCF is not supported in PCL profiles that target Windows Phone 8.1. (!)
  2. We looked up whether we could drop the use of WCF, and use "old school" ASMX-type proxies from a PCL. There seems to be an enormous amount of conflicting information on whether this is even possible. For instance here, a Xamarin employee writes that it's not supported to use ASMX-style web references in PCL, and that you have to use WCF. But we've already established that using WCF could be a bad idea (see #1).
  3. Should we just roll our own calling code, based just on HttpClient?
  4. OR should we drop all use of PCLs, and use Shared Libraries instead?

Many thanks for any help or experiences you can share.

回答1:

here's my two cents, i think you should go with 3 or 4...

3 - You should rewrite your backend to be more restfull friendly, use lighter formats like json, maybe use asp.net web api since seems you already have a .net backend , and in the client yes use httpclient.

4 - Use shared projects, and WCF support should work ok. This would be ok, but if you are using Xamarin.Forms there's still some issues with Shared projects and xaml.

Depending of a lot of things i would personally go with 3 ..



回答2:

This question was, more or less, answered over on the Xamarin forums, here http://forums.xamarin.com/discussion/42693/what-is-the-definitive-best-way-to-call-web-services-from-cross-platform-code

The answer was, basically, "No, there is no definitive right answer".

Two specific points:

  • We have now isolated the specific problem that we were seeing to a known bug in Mono's implementation of DataContractSerializer. (It does not respect the EmitDefaultValue flag).
  • This finding gives us more confidence in the robustness of the Xamarin WCF implementation (because that's not where the bug was), but on the whole WCF has left us scarred by its complexity, and yet still frustrated by the lack of configurability in the Mono implementation (it's not as customizable as the full version). We are considering the ServerStack client-side classes as an alternative.