WCF Service返回类型 List

7 篇文章 0 订阅
服务器端的 WCF Service,提供三个返回类型分别为 List<string>、List<自定义类>、Dictionary<string,string> 的函数,给 WCF 客户端 ASP.NET 程序调用,执行结果如下图 6。
Server-side/IService.cs
using System.Collections.Generic;
using System.ServiceModel;
using System.Runtime.Serialization;
[ServiceContract]
[ServiceKnownType(typeof(Employee))]
public interface IService
{
    [OperationContract]
    string GetData(int value);
    [OperationContract]
    List<string> getListString();
    [OperationContract]
    [ServiceKnownType(typeof(Employee))]
    List<Employee> getListEmployee();
    [OperationContract]
    Dictionary<string, string> getDictionaryString();
}
[DataContract]
[KnownType(typeof(Employee))]
public class Employee
{
    public Employee(string name, int age, object oooo)
    {
        this.name = name;
        this.age = age;
        this.oooo = oooo;
    }
    [DataMember]
    public string name;
    [DataMember]
    public int age;
    [DataMember]
    public object oooo;
}
复制代码
Server-side/Service.cs
using System.Collections.Generic;
using System.Runtime.Serialization;
public class Service : IService
{
    public string GetData(int value)
    {
        return string.Format("You entered: {0}", value);
    }
    public List<string> getListString()
    {
        List<string> list1 = new List<string>();
        list1.Add("List string 元素一");
        list1.Add("List string 元素二");
        return list1;
    }
    public List<Employee> getListEmployee()
    {
        List<Employee> list2 = new List<Employee>();
        list2.Add(new Employee("吴宇泽", 18, new object()));
        list2.Add(new Employee("王大同", 20, new object()));
        return list2;
    }
    public Dictionary<string, string> getDictionaryString()
    {
        Dictionary<string, string> dict1 =  new Dictionary<string, string>();
        dict1.Add("吴宇泽", "程序员");
        dict1.Add("王大同", "业务员");
        return dict1;
    }
}
复制代码
Client-side/Default.aspx.cs
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ServiceReference1.ServiceClient prox = new ServiceReference1.ServiceClient();
        /*********** List<string> ***********/
        //string[] list1 = new string[2];  //未改设置前,Server 返回的 List<string>,Client 只能取得 string 数组
        List<string> list1 = new List<string>();       
        list1 = prox.getListString();
        Response.Write(list1[0] + "<br>");
        Response.Write(list1[1] + "<p>");
        /*********** List<自定义类> ***********/
        List<ServiceReference1.Employee> list2 = new List<ServiceReference1.Employee>();
        list2 = prox.getListEmployee();
       
        Response.Write(list2[0].name + "<br>");
        Response.Write(list2[0].age + "<br>");
        Response.Write(list2[0].oooo + "<p>");      //object 类型
        /*********** Dictionary<string,string> ***********/
        Dictionary<string, string> dict1 = new Dictionary<string, string>();
        dict1 = prox.getDictionaryString();
        foreach (KeyValuePair<string, string> kvp in dict1)
        {
            Response.Write(kvp.Key + ", " + kvp.Value + "<br>");
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值