[索引页]
[×××] 


化零为整WCF(2) - 契约Contract(ServiceContract、OperationContract、DataContract、ServiceKnownType和DataMember)


作者:webabcd


介绍
WCF(Windows Communication Foundation) - 契约(Contract):服务契约(ServiceContract),操作契约(OperationContract),数据契约(DataContract),服务已知类型(ServiceKnownType),数据成员(DataMember)。


示例
1、服务
IPersonManager.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;
using System.Runtime.Serialization;

namespace WCF.ServiceLib.Contract
{
         ///
         /// 人员管理接口
         ///

         // Namespace - 服务契约的命名空间
         // Name - 服务契约的名称(会对应到相关的wsdl,默认情况下本例为接口名“IPersonManager”)
         // ConfigurationName - 服务契约在宿主中所配置的服务名称(默认情况下本例为类的全名“WCF.ServiceLib.Contract.IPersonManager”)
        [ServiceContract(Namespace = "http://webabcd.cnblogs.com", Name = "IPersonManager", ConfigurationName = "ConfigurationNameTest")]
        // 服务已知类型 - Student(数据契约)继承自Person(数据契约),要指定Student为已知类型,其才会被序列化
        [ServiceKnownType(typeof(Student))]
        public interface IPersonManager
        {
                ///
                /// 获取某人的姓名
                ///

                /// "p">Person对象

                ///
                // Name - 操作契约的名称(会对应到相关的wsdl,默认情况下本例为方法名“GetName”)
                [OperationContract(Name="GetPersonName")]
                string GetName([MessageParameter(Name = "person")] Person p);
        }
}
 
PersonManager.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;
using System.Runtime.Serialization;

namespace WCF.ServiceLib.Contract
{
         ///
         /// 人员管理类
         ///

         public class PersonManager : IPersonManager
        {
                 ///
                 /// 获取某人的姓名
                 ///

                 /// "p">Person对象

                 ///
                 public string GetName(Person p)
                {
                         return "Name: " + p.Name;
                }
        }
}
 
Person.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;
using System.Runtime.Serialization;

namespace WCF.ServiceLib.Contract
{
         ///
         /// Person的实体类
         ///

         // Name - 数据契约的名称(会对应到相关的wsdl,默认情况下本例为类名“Person”)
        [DataContract(Name = "PersonModel")]
         public class Person
        {
                 ///
                 /// Person的实体类的Age属性
                 ///

                 // Name - 数据成员的名称(会对应到相关的wsdl,默认情况下本例为属性名“Age”)
                 // IsRequired - 该值指示序列化引擎该成员在读取或反序列化时必须存在
                 // Order - 数据成员在相关的wsdl中的顺序
                 // EmitDefaultValue - 如果应该在序列化流中生成成员的默认值,则为 true,否则为 false,默认值为 true
                [DataMember(Name = "PersonAge", IsRequired = false, Order = 1)]
                 public int Age { get; set; }

                 ///
                 /// Person的实体类的Name属性
                 ///

                 // Name - 数据成员的名称(会对应到相关的wsdl,默认情况下本例为属性名“Name”)
                 // IsRequired - 该值指示序列化引擎该成员在读取或反序列化时必须存在
                 // Order - 数据成员在相关的wsdl中的顺序
                 // EmitDefaultValue - 如果应该在序列化流中生成成员的默认值,则为 true,否则为 false,默认值为 true
                [DataMember(Name = "PersonName", IsRequired = false, Order = 0)]
                 public string Name { get; set; }
        }
}
 
Student.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;
using System.Runtime.Serialization;

namespace WCF.ServiceLib.Contract
{
         ///
         /// Student的实体类
         ///

         // Name - 数据契约的名称(会对应到相关的wsdl,默认情况下本例为类名“Student”)
        [DataContract(Name = "StudentModel")]
         public class Student : Person
        {
                 ///
                 /// Student的实体类的School属性
                 ///

                 // Name - 数据成员的名称(会对应到相关的wsdl,默认情况下本例为属性名“School”)
                 // IsRequired - 该值指示序列化引擎该成员在读取或反序列化时必须存在
                 // Order - 数据成员在相关的wsdl中的顺序
                 // EmitDefaultValue - 如果应该在序列化流中生成成员的默认值,则为 true,否则为 false,默认值为 true
                [DataMember(Name = "School", IsRequired = false, Order = 0)]
                 public string School { get; set; }
        }
}
 
 
2、宿主
PersonManager.svc
<%@ ServiceHost Language="C#" Debug="true" Service="WCF.ServiceLib.Contract.PersonManager" %>
 
 
Web.config


    
        
            
                
                    
                    
                

            

        

        
            
            
            
                
                
                
                
            

        

    

 
 
3、客户端
PersonManager.aspx
<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="PersonManager.aspx.cs"
        Inherits="Contract_PersonManager" Title="契约(ServiceContract、OperationContract、DataContract、ServiceKnownType和DataMember)" %>




        
         
                         />
 
PersonManager.aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Contract_PersonManager : System.Web.UI.Page
{
         protected void Page_Load( object sender, EventArgs e)
        {

        }

         protected void btnGetName_Click( object sender, EventArgs e)
        {
                 // Contract.IPersonManager pm = new Contract.PersonManagerClient();

                Contract.PersonManagerClient proxy = new Contract.PersonManagerClient();

                Contract.StudentModel sm = new Contract.StudentModel() { PersonName = txtName.Text };

                Page.ClientScript.RegisterStartupScript(
                         this.GetType(),
                         "js",
                         string.Format( "alert('{0}')", proxy.GetPersonName(sm)),
                         true);

                proxy.Close();
        }
}
 
Web.config


    
        
            
            
            
            
        

    

 
 
运行结果:
单击"btnGetName"后弹出提示框,显示"Name: webabcd"


OK
[×××]