JohnB February 2016

How do I pass multiple parameters to a WCF Restful service from a c# client?

I have a WCF Restful service call which expects multiple parameters.

Consider the following data and service contracts.

public class ClassA
{
   public string aString{ get; set;}
   public int aInt {get; set;}
}

public class ClassB
{
   public string bString{ get; set;}
   public int bInt {get; set;}
}

[ServiceContract]
public interface ISampleService
{
   [OperationContract(IsOneWay = false)]
   ClassC GetSomeData(ClassA classA, string sValue, ClassB classB);
}

I have a C#/winform based test application. I know all these parameters need to be wrapped before calling the service. I'm having difficulty figuring out what the C# code to call the service would look like on the client side.

Can someone show me an example of how I would structure the code on the client side to call the above defined service?

Thanks, JB

Answers


Georg Patscheider February 2016

You should be able to call the service like a normal method after setting up an endpoint for ISampleService.

var result = ISampleService.GetSomeData(
    new A { aString = "A string" }, 
    "someValue", 
    new B()
);

WCF does its magic to transform this to a remote procedure call. Just make sure all the parameters you want to pass are serializable.


JohnB February 2016

The easiest way I figured out to do this was to create a RESTFul interface that accepts an ArrayList.

On the client side, the desired complex parameters (classes) are serialized to a string and then inserted into the ArrayList.

On the service side : 1) Verify the ArrayList contains the desired number of parameters 2) Deserialize the complex objects from the incoming ArrayList

I'm not sure if this is the most elegant or 'accepted" way to do this, but it works.

Post Status

Asked in February 2016
Viewed 3,182 times
Voted 9
Answered 2 times

Search




Leave an answer