当前位置: 代码迷 >> Web Service >> WCF中的DataContract约据对于普通类的影响
  详细解决方案

WCF中的DataContract约据对于普通类的影响

热度:364   发布时间:2013-01-08 14:02:13.0
WCF中的DataContract契约对于普通类的影响
在下刚接触WCF,对于自定义返回数据类型有些迷惑,在书上看到自定义数据类型是在IService命名空间中定义的,需要添加datacontract契约,但是我希望能在IService之外的地方定义数据类型,如果加上datacontract契约的话不知道有没有什么影响呢?
namespace WcfService.SysConfig
{
    // 注意: 如果更改此处的接口名称“IService1”,也必须更新 App.config 中对“IService1”的引用。
    [ServiceContract]
    public interface ISysNavService
    {
        [OperationContract]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);

        // 任务: 在此处添加服务操作
    }

    // 使用下面示例中说明的数据协定将复合类型添加到服务操作
    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";

        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }

        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }
}
如果我在一个新命名空间A中定义InventoryRecord,因为在数据访问层中也需要使用InventoryRecord类型,
namespace WcfService.A
{
    // 使用下面示例中说明的数据协定将复合类型添加到服务操作
    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";

        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }

        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }
  相关解决方案