如何使用此示例实现客户端

How to implement the client using this example

我正在使用这个 Return list in WCF 示例,但我无法正确实现客户端代码。这个例子有效。我希望在客户端传输列表。

到目前为止我的代码:

List<Person> aPerson = new List<Person>()
Person y = new Person()'
aPerson.Add(y.id, y.name, y.adress, y.salary, y.country)

这是服务器:

[DataContract]
public class Person
{
    public string Id;
    public string name;
    public string address;
    public string salary;
    public string country;

    public Person()
    { }

    public Person(string _id, string _name, string _address, string _salary, string _country)
    {
        Id = _id;
        name = _name;
        address = _address;
        salary = _salary;
        country = _country;
    }

    [DataMember]
    public string Idps
    {
        get { return Id; }
        set { Id = value; }
    }

    [DataMember]
    public string nameps
    {
        get { return name; }
        set { name = value; }
    }

    [DataMember]
    public string addressps
    {
        get { return address; }
        set { address = value; }
    }

    [DataMember]
    public string salaryps
    {
        get { return salary; }
        set { salary = value; }
    }

    [DataMember]
    public string countryps
    {
        get { return country; }
        set { country = value; }
    }

}


public List <Person> GetData(string Id)
    {

        //Create a List of Person objects
        List<Person>employeelist =new List<Person>();

        employeelist.Add(new Person("10", "name", "myAdress", "1000", "myCountry");

        }

        //Return the list that contains Person objects
        return employeelist;

    }

我不知道如何使用上面的代码实现客户端。服务器 returns 列表,我想将列表存储在客户端本地。

我认为您最好完成完整的端到端示例,如下所示:http://msdn.microsoft.com/en-us/library/bb386386.aspx 这应该可以帮助您加快使用 WCF 的速度。

但是,作为一个快速启动......我假设你在你的界面中用 [OperationContract] 属性装饰了你的方法 GetData?

然后在客户端上您需要引用 WCF 服务。添加服务时,您应该单击对话框左下角的 Advanced 按钮。将 集合类型 下拉列表从 System.Array 更改为 System.Collections.Generic.List.

最后,您的客户端应该能够使用如下代码调用该服务:

public void SampleClientCode()
{
    using (var client = new ServiceReference1.Service1Client())
    {
        List<Person> results = client.GetData("12345");

        // Now do something with the data... Example
        string firstPersonsName = results.First().nameps;
    }
}

注意:属性 Person class 中的命名约定不是很好,应该修改。