Constructor in WCF DataContract not reflected on Client

前端 未结 3 862
盖世英雄少女心
盖世英雄少女心 2020-12-06 02:41

I need to have some data members get some values when I create an instance of the DataContract on the client. This is not happening using constructors. I have searched throu

3条回答
  •  伪装坚强ぢ
    2020-12-06 02:58

    The code generator used to create WCF proxy classes creates compatible contract types, and doesn't used the exact same type as used by the WCF service. The easiest way to achieve what you want, is to create the constructor yourself on your client, as the code generated is partial:

    partial class Account
    {
        public Account()
        {
            AcountId = 5;
            AccountName = "ABC";
        }
    }
    

    If you don't want to do this, you can get WCF to reuse types that are already referenced by your client project. So if your data contract classes are in a separate library (as is recommended), you can reference that library and then reconfigure your WCF client project to reuse the shared types from the referenced assembly.

提交回复
热议问题