WCF DataContract问题 [英] WCF DataContract Problem
问题描述
在WCF中,我有一个课程为
In WCF,I have a class as
[DataContract]
public class test : Object
{
public string a = "";
[OperationContract]
public void add(test b)
{
a = b.a;
}
}
我有一个这样的函数
[OperationContract]
字符串SetData(test obj);
[OperationContract]
string SetData(test obj);
现在在客户端,我无法访问测试类的add方法
Now at client side i am not able to access the add method of the test class
推荐答案
如MSDN中所述,数据合约是服务与客户端之间的正式协议,它抽象地描述了要被交换.也就是说,进行通信时,客户端和服务不必共享相同的类型,而只需共享相同的数据合同. 数据协定为每个参数或返回类型精确定义要序列化(转换为XML)的数据以进行交换."因此,您只能在数据协定中公开属性,而不能公开方法.
As outlined in MSDN, "A data contract is a formal agreement between a service and a client that abstractly describes the data to be exchanged. That is, to communicate, the client and the service do not have to share the same types, only the same data contracts. A data contract precisely defines, for each parameter or return type, what data is serialized (turned into XML) to be exchanged." So you can only expose properties not method in a data contract.
因此,您的合同应类似于
So you contract should look like
[DataContract]public class Test { [DataMember] public string A{ { get;set; }}Then your service Contract should be public IMyService:Object{[OperationContract] string SetData(Test a);[OperationContract]string Add(Test a);}//Implement your service class public MyService:IMyService{string SetData(Test a){//Your logic return string;}string Add(Test a){//your logic //return string; }}
这篇关于WCF DataContract问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!