服务器端的 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>");
}
}
}
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>");
}
}
}