【问题标题】:WCF querying an array of objectsWCF 查询对象数组
【发布时间】:2012-04-10 16:08:31
【问题描述】:

我仍在研究 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;

【问题讨论】:

  • 请粘贴服务接口和服务实现类的代码。另请粘贴 Group 类的代码。当代码可用时会更容易回答
  • PS 我知道从应用程序发送的查询很好,因为后端日志系统将其标记为成功查询并在日志文件中返回结果
  • 很酷的问题。 app.config 文件呢?它们在您的两个应用中是否相同?
  • 我检查了服务参考和 app.config,它们看起来很好,所有设置都在那里。

标签: c# wcf serialization


【解决方案1】:

我终于找到了解决办法:

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


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

        toReturn.groups = Groups;

        listBox1.ItemsSource = toReturn.groups;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    • 2021-09-24
    • 2023-03-09
    • 2017-05-22
    • 1970-01-01
    • 1970-01-01
    • 2012-05-05
    相关资源
    最近更新 更多