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

来源:互联网 发布:软件设计方案及要求 编辑:程序博客网 时间:2024/06/05 04:54


   在数据类(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

原创粉丝点击