[英]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. 我有一个WCF Restful服务呼叫,需要多个参数。
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. 我有一个基于C#/ winform的测试应用程序。 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. 我很难弄清楚在客户端调用该服务的C#代码是什么样子。
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 谢谢,JB
You should be able to call the service like a normal method after setting up an endpoint for ISampleService
. 在为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. WCF尽力将其转换为远程过程调用。 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. 我想到的最简单的方法是创建一个接受ArrayList的RESTFul接口。
On the client side, the desired complex parameters (classes) are serialized to a string and then inserted into the ArrayList. 在客户端,所需的复杂参数(类)被序列化为字符串,然后插入到ArrayList中。
On the service side : 1) Verify the ArrayList contains the desired number of parameters 2) Deserialize the complex objects from the incoming ArrayList 在服务方面:1)验证ArrayList是否包含所需数量的参数2)从传入的ArrayList中反序列化复杂对象
I'm not sure if this is the most elegant or 'accepted" way to do this, but it works. 我不确定这是否是最优雅或“被接受”的方式,但是它可以工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.