WCF 调用协定中的“获取”数据成员返回 null
本文关键字:获取 数据成员 返回 null 调用 WCF | 更新日期: 2023-09-27 18:31:43
我的服务协定中有一个操作协定,叫做
Schedule[] GetScheduleObjects();
我在那个操作合约中有一个名为"任务"的数据管理器,它返回一个子对象列表。
[DataMember()]
public Task[] Tasks
问题是当调用操作协定时,该方法会执行,但"任务"的"get"会发生两次。第一次它包含有效的运行时实例,第二次为 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);
}
您需要
向属性Tasks
添加一个资源库,并且还需要将Name
的可见性提高到至少受保护 - WCF 需要使用它们来反序列化此类的对象。
作为次要问题,如果客户端生成代理(例如,使用 Add Service Reference
或通过 SvcUtil.exe
),"代码隐藏"Tasks
(即 耦合到 ScheduledTasks
) 的return _Tasks.ToArray();
将丢失,客户端将只获得属性Tasks
的简单自动支持属性(在代理生成期间选择集合类)。但是,如果您共享类型,则不会发生第二个问题。