简体   繁体   English

Mono for Android或Java和WCF

[英]Mono for Android or Java and WCF

I am currently using Mono for Android to develop a mobile application. 我目前正在使用Mono for Android来开发移动应用程序。 I have been trying to consume a WCF web service by adding a web reference to it but I can't seem to make the call that way. 我一直试图通过添加Web引用来使用WCF Web服务,但我似乎无法以这种方式进行调用。 I am now considering to bite the bullet and rewrite the code using Java which I am not as good at as I am with C#. 我现在正在考虑咬紧牙关并使用Java重写代码,我不像C#那样擅长。

I have 2 questions: 我有两个问题:

  1. How do I consume a WCF webservice using Mono for android. 如何使用Mono for android使用WCF Web服务。
  2. If I am to use java how would I call a method that looks like the one below: 如果我要使用java,我将如何调用如下所示的方法:

     [OperationContract] [WebInvoke(Method = "POST", UriTemplate = "/MyMethod", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] void MyMethod(CustomObjectFromDataContract c_Object); 

When I make the call I get a MessageBox that says Unhandled exception System.Net.WebException:. 当我进行调用时,我得到一个MessageBox,其中显示未处理的异常System.Net.WebException:。 When I step into the code I see that the error happens when you call 当我进入代码时,我发现当你打电话时会发生错误

  [System.Web.Services.Protocols.SoapDocumentMethodAttribute    ("http://tempuri.org/IMyService/MyMethod", RequestNamespace="http://tempuri.org/",    ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
    public void MyMethod([System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]   CustomObjectFromDataContract c_Object) {
    this.Invoke("MyMethod", new object[] {
                    c_Object});
    }

The invoke is the one throwing the exception. 调用是抛出异常的那个。

I have resolved the issue. 我已经解决了这个问题。 Here are the steps to make this work: 1. The service has to be a RESTful service 2. Instead of referencing Localhost (which I was in the generated code) use the IP address of the hosting machine. 以下是使其工作的步骤:1。服务必须是RESTful服务2.而不是引用Localhost(我在生成的代码中)使用主机的IP地址。 I think this is because Android runs in Dalvik VM which I suspect has a different local host from the one my Dev computer is using. 我认为这是因为Android在Dalvik VM中运行,我怀疑它与我的Dev计算机使用的本地主机不同。 I did that and my service works now. 我做到了,我的服务现在有效。

If you add a Web Reference, some references classes including a client should be generated. 如果添加Web引用,则应生成一些包含客户端的引用类。 You can then create an instance of the generated client and call MyMethod on the client. 然后,您可以创建生成的客户端的实例,并在客户端上调用MyMethod

So assuming you are using Visual Studio simply right click you MonoDroid project > Add Web Reference and enter the URL to your WCF service. 因此,假设您使用的是Visual Studio,只需右键单击MonoDroid项目>添加Web引用,然后输入WCF服务的URL。

To call it you can do the following: I have added a reference to a service with namespace Example.WebReference 要调用它,您可以执行以下操作:我已添加对具有名称空间Example.WebReference的服务的引用

I would then call it in the following way: 然后我会用以下方式调用它:

Example.WebReference.ServiceElement client = new Example.WebReference.ServiceElement();
var output = client.MyMethod(parameter);

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM