Reputation: 155
I am currently using Mono for Android to develop a mobile application. 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. 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#.
I have 2 questions:
If I am to use java how would I call a method that looks like the one below:
[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:. 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.
Upvotes: 3
Views: 1656
Reputation: 155
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. 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. I did that and my service works now.
Upvotes: 2
Reputation: 1258
If you add a Web Reference, some references classes including a client should be generated.
You can then create an instance of the generated client and call MyMethod
on the client.
So assuming you are using Visual Studio simply right click you MonoDroid project > Add Web Reference and enter the URL to your WCF service.
To call it you can do the following: I have added a reference to a service with namespace 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.
Upvotes: 0