【问题标题】:WCF call to "get" datamember in contract returns nullWCF 调用合同中的“get”数据成员返回 null
【发布时间】:2013-09-22 02:26:54
【问题描述】:

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

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

}

【问题讨论】:

  • 为什么任务有 2 个属性?您应该使用一个(数组)或另一个(列表),而不是两者。

标签: c# .net wcf nettcpbinding


【解决方案1】:

您需要向属性Tasks 添加一个setter,还需要将Name 的可见性提高到at least protected - WCF 需要使用这些来反序列化此类的对象.

作为次要问题,如果客户端生成代理(例如使用Add Service Reference 或通过SvcUtil.exe),“代码背后”Tasks(即与ScheduledTasks 耦合的return _Tasks.ToArray();)将是丢失,而客户端将只获得属性Tasks 的简单自动支持属性(在代理生成期间选择集合类)。但是,如果您share type,则不会发生第二个问题。

【讨论】:

    猜你喜欢
    • 2011-08-31
    • 1970-01-01
    • 2022-12-21
    • 2021-10-27
    • 2021-01-23
    • 2017-09-14
    • 2015-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多