Passing an object to the WCF service method from an Android application


I know we can consume WCF service from android application, and passing values to WCF service method by passing values in Requested URL. but the service method I am calling take an Object as a parameter and I am not getting how to pass this object to service method? also the service I am using is a third party I can't make any modification in it.

Or is there any way to add service reference in android application?

Any help?


Services -- on WCF and every other technology stack -- communicate by sending messages not objects. That is the basis for interoperability, so it's really important to us. WCF translates between CLR lanaguage constructs, like those in C#, into the world of messages and back again. So we have the illusion that we're programming with .NET objects, but in reality WCF is making message-based communication happen on our behalf.

For SOAP services, messages are expressed in XML, and the message schemas (the contracts of the messages) are expressed in XSD. When you write a service contract that uses System.Object as a parameter, like this:

public interface IService1
  string GetData( object something );

then the message schema for the GetData request message looks like this:

<xs:element name="GetData">
      <xs:element minOccurs="0" name="something" nillable="true" type="xs:anyType"/>

The key here is that the type of the "something" element is "xs:anyType". In other words, absolutely any XML is allowed here. That is the loosest possible contract in the world of XML-based messaging. So loose, in fact, that most would-be clients of this service will have no idea what to do with the contents of the message because it could be anything. And that's what you said in the CLR world, too, System.Object is the base class of all reference types, so it could be anything.

In the Android (or any other technology stack) world, trying to consume this service, it is probably throwing up its hands and saying, "the service contract says this could be anything, but I don't have an acceptable 'this could be anything' type, so this isn't going to work." (You didn't describe the nature of the problem you're having on the Andriod side. That would be illuminating.)

If this 'so loose as to be meaningless' contract is written by a third-party, let me suggest three options.

First, get a different third-party service or make this one change. The problem is that they have written a service that has a lousy contract, and using that contract as the basis for interoperation (e.g., WCF and Android) is going to be problematic or fail outright. This topic is Service Contracts 101, it's not terribly exotic.

Second, if the third-party service supports JSON (in addition to XML) messages, that simplifies working with loose contracts.

Third, create a service that wraps the use of the third-party service. This new service would pass more meaningful types (strings, ints, classes with the [DataContract] attribute) and encapsulate the translation between meaningful types and System.Object. The Android client could then consume this new service that has a meaningful contract.

Hope that helps!