1

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

Schedule[] GetScheduleObjects();

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

[DataMember()]
public Task[] Tasks

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

数据合约

[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);

}
4

1 回答 1

0

您需要向 property 添加一个 setter Tasks,并且您还需要增加对至少 protected 的可见性Name- WCF需要使用这些来反序列化此类的对象。

作为次要问题,如果客户端生成代理(例如使用或Add Service Reference通过SvcUtil.exe),“代码背后” Tasksreturn _Tasks.ToArray();即与在代理生成期间选择的集合类)。但是,如果您共享 type ,则不会发生第二个问题。ScheduledTasksTasks

于 2013-09-17T15:29:31.133 回答