您的位置:首页 > 其它

WCF学习:关于数据类(DataContract)中逻辑方法或复写方法未被初始化的原因及解决方案

2013-08-19 09:20 567 查看
在数据类(DataContract)中添加逻辑方法或复写如ToString()方法,WCF都不会对这些方法进行初始化,原因在于:

Data contract is for transporting data - not for transporting logic. When you use add service reference, add web reference, xsd.exe, wsdl.exe
or svcutil.exe only properties are described - all your methods declared on server are lost

(Data Contract类是传输数据 - 而不是传输逻辑。在使用“添加服务引用”,“添加WEB引用”,xsd.exe,wsdl.exe或者svcutil.exe,只会描述属性 - server端所有的方法定义都会丢失(即不做初始化)

解决方案:

The ugly way: share the library with data contract between service and client instead of creating a new type on the client through one of the mentioned tools

Those tools should create partial classes. Your
ToString
method
obviously belongs to the client side because it is related to data presentation so create second partial part of the contract class on the client and declare
ToString
method
in that class

大致的意思是在客户端为server端Data Contract类新建部分类,在部分类中实现逻辑

参考:
http://stackoverflow.com/questions/10401186/tostring-method-overriding-in-datacontract-in-wcf
http://stackoverflow.com/questions/1181039/wcf-datacontract-tostring-function
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐