在合同中WCF调用“get”datamember返回null

时间:2013-09-17 15:05:28

标签: c# .net wcf nettcpbinding

我的服务合同中有一份名为

的操作合同
Schedule[] GetScheduleObjects();

我在该操作合同中有一个名为“Tasks”的数据网,它返回一个子对象列表。

[DataMember()]
public Task[] Tasks

问题是当调用操作契约时,方法执行但“任务”的“获取”发生两次。它第一次包含有效的运行时实例,第二次它为null,这会导致序列化异常。尽管只有一次调用服务,但仍会发生这种情况绑定是使用双工代理的tcp连接。想法?????

Datacontract

[DataContract()]
public class Schedule
{
    public Schedule(string name)
    {
        this.Name = name;
    }

    [DataMember()]
    public string Name { get; private set; }

    [DataMember()]
    public bool Running { get; set; }

    /// <summary>
    /// Schedule Task is a DataMember object, do not modify
    /// </summary>
    [DataMember()]
    public Task[] Tasks
    {
        get { return _Tasks.ToArray(); }
    }

    private List<Task> _Tasks = new List<Task>();

     ///<summary>
     /// Use this property to add objects 
     ///</summary>
    public List<Task> ScheduleTasks 
    {
        get { return _Tasks; }
    }
}

服务合同

[ServiceContract(CallbackContract = typeof(ISummitDashboardCallbackContract))]
public interface ISchedulerContract : ISummitDashboardContract
{
    /// <summary>
    /// Sets the named schedule into "run" mode
    /// </summary>
    /// <param name="scheduleName"></param>
    /// <returns></returns>
    [OperationContract()]
    void StartSchedule(string scheduleName);

    /// <summary>
    /// Pauses the currently running schedule
    /// </summary>
    /// <param name="scheduleName"></param>
    [OperationContract()]
    void PauseSchedule(string scheduleName);

    /// <summary>
    /// Removes the named schedule from "run" mode
    /// </summary>
    /// <param name="scheduleName"></param>
    /// <returns></returns>
    [OperationContract()]
    void StopSchedule(string scheduleName);

    /// <summary>
    /// Flips the "active" state of the task with the named id
    /// </summary>
    /// <param name="scheduleName"></param>
    /// <param name="Id"></param>
    [OperationContract()]
    void ToggleTaskState(string scheduleName, string Id);

    /// <summary>
    /// Flips the "active" state of the action with the named id
    /// </summary>
    /// <param name="scheduleName"></param>
    /// <param name="Id"></param>
    /// <returns></returns>
    [OperationContract()]
    void ToggleActionState(string scheduleName, string Id);

    /// <summary>
    /// Returns the information to build the tree list in the dashboard
    /// </summary>
    /// <returns></returns>
    [OperationContract()]
    Schedule[] GetScheduleObjects();

    /// <summary>
    /// Returns the events of the scheduler
    /// </summary>
    /// <returns></returns>
    [OperationContract()]
    SchedulerEvent[] GetSchedulerEvents(int startIndex, int count, int eventLogEntryType);

}

1 个答案:

答案 0 :(得分:0)

您需要向属性Tasks添加setter,并且还需要将Name的可见性提高到at least protected - WCF需要按顺序使用这些反序列化该类的对象。

作为次要问题,如果客户端生成代理(例如,使用Add Service ReferenceSvcUtil.exe),则代码“{1}}(即Tasksreturn _Tasks.ToArray();耦合到ScheduledTasks)将丢失,客户端将只获得属性Tasks的简单自动后备属性(在代理生成期间选择集合类)。但是,如果你share type,那么第二个问题就不会发生。