如何在 WCF 中定义数据协定

本文关键字:数据 定义 WCF | 更新日期: 2023-09-27 17:56:05

我是WCF的新手。因此,这个问题。我正在将 asmx Web 服务转换为 WCF 服务。

我在定义数据协定时遇到问题。

我的服务合同有两个操作,一个返回一个Customer数组,其中Customer是我的自定义类型,带有一些字段。

在这里,我的问题是哪个需要声明为数据协定。

是包含Customer数组的Customer类还是Customers类,还是两者兼而有之?

请做建议

如何在 WCF 中定义数据协定

您应该阅读这篇关于数据协定:使用数据协定的 MSDN 文章。

总之:

您应该在服务中使用的跨线的每个类上定义 DataContract 属性,因此您需要在 CustomerCustomers 上都具有该属性。

请检查此示例代码。在下面的类文件中,我为我的 wcf 服务创建了数据协定。使用命名空间 System.Runtime.Serialization 来序列化数据。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
namespace BusinessLogic
{
     [DataContract]
     [Serializable]
     public class POSTheaterListArgs
    {
    public POSTheaterListArgs()
    {
        TheaterDates = new List<string>();
    }
    [DataMember]
    public List<string> TheaterDates { get; set; }
    [DataMember]
    public int TheaterId { get; set; }
    [DataMember]
    public int NumofScreen { get; set; }
    [DataMember]
    public int? CompanyId { get; set; }
    [DataMember]
    public int ChainId { get; set; }
    [DataMember]

    private int _Number;
    public int Number { get { return _Number; } set { _Number = value; } }
    private bool _status;
    public bool status { get { return _status; } set { _status = value; } }
}

对于数组数据

    public POSTheaterListArgs()
    {
        TheaterDates = new List<string>();
    }
    [DataMember]
    public List<string> TheaterDates { get; set; }

将返回列表数组结构。

这可能会对你有所帮助。