WCF查询对象数组

时间:2012-04-10 16:08:32

标签: c# wcf serialization

我仍在研究WCF解决方案,该解决方案应该能够查询程序的后端并返回结果。

后端存储一个名为Groups的对象字典,可以使用以下函数查询它们:

  • GetGroup按ID
  • 获取单个群组
  • GetGroups按标签获取群组列表。

GetGroup可以正常使用WCF测试客户端和我构建的应用程序。 它适用于应用程序的以下代码:

        List<string> values = new List<string>();
        GroupServiceClient client = new GroupServiceClient("WSHttpBinding_IGroupService");
        www.test.co.uk.programme.programme Group = new www.test.co.uk.programme.programme();
        DateTime time = DateTime.Now;
        values.Clear();
        client.Open();

        Group.number = textBox1.Text;
        client.GetGroup(ref time, ref Group);

        GroupStorageMessage toReturn = new GroupStorageMessage();
        toReturn.group = Group;

        selectedGroupId = Convert.ToString(toReturn.group.number);

        values.Add(Convert.ToString(toReturn.group.number));
        values.Add(Convert.ToString(toReturn.group.name));

        listBox1.ItemsSource=values;

        client.Close();

GetGroups与WCF测试客户端完美配合,但与我的应用程序无关。

它会按原样发送查询,但它会返回Null(请注意,此代码是另一个应用程序,我使用的是引用而不是代理文件)

        ServiceReference1.programme Group = new ServiceReference1.programme();
        ServiceReference1.GroupServiceClient Client = new ServiceReference1.GroupServiceClient();
        DateTime Time = DateTime.Now;

        Client.Open();

        string[] aa = new string[1];

        aa[0] = textBox1.Text;
        Group.tags = aa;
        Client.GetGroups(ref Time, Group);

        ServiceReference1.GroupArrayMessage toReturn = new ServiceReference1.GroupArrayMessage();

        ServiceReference1.programme[] Groups = new ServiceReference1.programme[1];

        toReturn.groups = Groups;   = returns null

在新的ServiceReference1.programme [1]中;我实际上在猜测放在那里的东西。

接口:

[ServiceContract(Namespace = "http://www.Test.co.uk/groupstorage")]
public interface IGroupStorageService
{
    /**
     * Get a group from the collection of groups
     */
    [OperationContract]
    GroupStorageMessage GetGroup(GroupStorageMessage message);
    /**
     * Add a group to the collection of groups
     */
    [OperationContract]
    void AddGroup(GroupStorageMessage message);
    /**
     * Remove a group from the collection of groups
     */
    [OperationContract]
    void RemoveGroup(GroupStorageMessage message);
    /**
     * Update a group in the collection of groups
     */
    [OperationContract]
    void UpdateGroup(GroupStorageMessage message);

    [OperationContract]
    GroupArrayMessage GetGroups(GroupStorageMessage message);
}

消息合同:

[MessageContract]
public class GroupArrayMessage
{
    /**
     * Message header is the timestamp when the message was created
     */
    [MessageHeader(Name = "time")]
    public DateTime Time;
    /**
     * Message body is a collection of Users
     */
    [MessageBodyMember(Name = "groups")]
    public Group[] Groups;
}

小组联系(有时称为节目)

[DataContract(Namespace = "http://www.test.co.uk/programme", Name = "programme")]
public class Group
{
    /**
     * The number representing the Programme (Programme ID)
     */
    [DataMember(Name = "number")]
    public string Number;
    /**
     * The name of the Programme
     */
    [DataMember(Name = "name")]
    public string Name;
    /// <summary>
    /// Add Tags
    /// </summary>
    [DataMember(Name = "tags")]
    public string[] Tags;

1 个答案:

答案 0 :(得分:4)

我终于找到了解决方案:

        GroupService.GroupArrayMessage toReturn = new GroupService.GroupArrayMessage();


        GroupService.programme[] Groups = Client.GetGroups(ref Time, Group);

        toReturn.groups = Groups;

        listBox1.ItemsSource = toReturn.groups;