简体   繁体   中英

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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