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

[索引页]
[源码下载] 


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


作者: webabcd


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


示例
1、服务
IPersonManager.cs
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Text; 
InBlock.gif 
InBlock.gif using System.ServiceModel; 
InBlock.gif using System.Runtime.Serialization; 
InBlock.gif 
InBlock.gif namespace WCF.ServiceLib.Contract 
InBlock.gif
InBlock.gif         /// <summary> 
InBlock.gif         /// 人员管理接口 
InBlock.gif         /// </summary> 
InBlock.gif         // Namespace - 服务契约的命名空间 
InBlock.gif         // Name - 服务契约的名称(会对应到相关的wsdl,默认情况下本例为接口名“IPersonManager”) 
InBlock.gif         // ConfigurationName - 服务契约在宿主中所配置的服务名称(默认情况下本例为类的全名“WCF.ServiceLib.Contract.IPersonManager”) 
InBlock.gif        [ServiceContract(Namespace =  "http://webabcd.cnblogs.com", Name = "IPersonManager", ConfigurationName = "ConfigurationNameTest")] 
InBlock.gif        // 服务已知类型 - Student(数据契约)继承自Person(数据契约),要指定Student为已知类型,其才会被序列化 
InBlock.gif        [ServiceKnownType(typeof(Student))] 
InBlock.gif        public interface IPersonManager 
InBlock.gif        { 
InBlock.gif                /// <summary> 
InBlock.gif                /// 获取某人的姓名 
InBlock.gif                /// </summary> 
InBlock.gif                /// <param name="p">Person对象</param> 
InBlock.gif                /// <returns></returns> 
InBlock.gif                // Name - 操作契约的名称(会对应到相关的wsdl,默认情况下本例为方法名“GetName”) 
InBlock.gif                [OperationContract(Name="GetPersonName")] 
InBlock.gif                string GetName([MessageParameter(Name = "person")] Person p); 
InBlock.gif        } 
InBlock.gif}
 
PersonManager.cs
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Text; 
InBlock.gif 
InBlock.gif using System.ServiceModel; 
InBlock.gif using System.Runtime.Serialization; 
InBlock.gif 
InBlock.gif namespace WCF.ServiceLib.Contract 
InBlock.gif
InBlock.gif         /// <summary> 
InBlock.gif         /// 人员管理类 
InBlock.gif         /// </summary> 
InBlock.gif         public  class PersonManager : IPersonManager 
InBlock.gif        { 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// 获取某人的姓名 
InBlock.gif                 /// </summary> 
InBlock.gif                 /// <param name="p">Person对象</param> 
InBlock.gif                 /// <returns></returns> 
InBlock.gif                 public  string GetName(Person p) 
InBlock.gif                { 
InBlock.gif                          return  "Name: " + p.Name; 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
Person.cs
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Text; 
InBlock.gif 
InBlock.gif using System.ServiceModel; 
InBlock.gif using System.Runtime.Serialization; 
InBlock.gif 
InBlock.gif namespace WCF.ServiceLib.Contract 
InBlock.gif
InBlock.gif         /// <summary> 
InBlock.gif         /// Person的实体类 
InBlock.gif         /// </summary> 
InBlock.gif         // Name - 数据契约的名称(会对应到相关的wsdl,默认情况下本例为类名“Person”) 
InBlock.gif        [DataContract(Name =  "PersonModel")] 
InBlock.gif         public  class Person 
InBlock.gif        { 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// Person的实体类的Age属性 
InBlock.gif                 /// </summary> 
InBlock.gif                 // Name - 数据成员的名称(会对应到相关的wsdl,默认情况下本例为属性名“Age”) 
InBlock.gif                 // IsRequired - 该值指示序列化引擎该成员在读取或反序列化时必须存在 
InBlock.gif                 // Order - 数据成员在相关的wsdl中的顺序 
InBlock.gif                 // EmitDefaultValue - 如果应该在序列化流中生成成员的默认值,则为 true,否则为 false,默认值为 true 
InBlock.gif                [DataMember(Name =  "PersonAge", IsRequired =  false, Order = 1)] 
InBlock.gif                 public  int Age { get; set; } 
InBlock.gif 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// Person的实体类的Name属性 
InBlock.gif                 /// </summary> 
InBlock.gif                 // Name - 数据成员的名称(会对应到相关的wsdl,默认情况下本例为属性名“Name”) 
InBlock.gif                 // IsRequired - 该值指示序列化引擎该成员在读取或反序列化时必须存在 
InBlock.gif                 // Order - 数据成员在相关的wsdl中的顺序 
InBlock.gif                 // EmitDefaultValue - 如果应该在序列化流中生成成员的默认值,则为 true,否则为 false,默认值为 true 
InBlock.gif                [DataMember(Name =  "PersonName", IsRequired =  false, Order = 0)] 
InBlock.gif                 public  string Name { get; set; } 
InBlock.gif        } 
InBlock.gif}
 
Student.cs
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Text; 
InBlock.gif 
InBlock.gif using System.ServiceModel; 
InBlock.gif using System.Runtime.Serialization; 
InBlock.gif 
InBlock.gif namespace WCF.ServiceLib.Contract 
InBlock.gif
InBlock.gif         /// <summary> 
InBlock.gif         /// Student的实体类 
InBlock.gif         /// </summary> 
InBlock.gif         // Name - 数据契约的名称(会对应到相关的wsdl,默认情况下本例为类名“Student”) 
InBlock.gif        [DataContract(Name =  "StudentModel")] 
InBlock.gif         public  class Student : Person 
InBlock.gif        { 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// Student的实体类的School属性 
InBlock.gif                 /// </summary> 
InBlock.gif                 // Name - 数据成员的名称(会对应到相关的wsdl,默认情况下本例为属性名“School”) 
InBlock.gif                 // IsRequired - 该值指示序列化引擎该成员在读取或反序列化时必须存在 
InBlock.gif                 // Order - 数据成员在相关的wsdl中的顺序 
InBlock.gif                 // EmitDefaultValue - 如果应该在序列化流中生成成员的默认值,则为 true,否则为 false,默认值为 true 
InBlock.gif                [DataMember(Name =  "School", IsRequired =  false, Order = 0)] 
InBlock.gif                 public  string School { get; set; } 
InBlock.gif        } 
InBlock.gif}
 
 
2、宿主
PersonManager.svc
<%@ ServiceHost Language="C#" Debug="true" Service="WCF.ServiceLib.Contract.PersonManager" %>
 
 
Web.config
<?xml version="1.0"?> 
<configuration> 
    <system.serviceModel> 
        <behaviors> 
            <serviceBehaviors> 
                <behavior name="ContractBehavior"> 
                    <!--httpGetEnabled - 使用get方式提供服务--> 
                    <serviceMetadata httpGetEnabled="true" /> 
                </behavior> 
            </serviceBehaviors> 
        </behaviors> 
        <services> 
            <!--name - 提供服务的类名--> 
            <!--behaviorConfiguration - 指定相关的行为配置--> 
            <service name="WCF.ServiceLib.Contract.PersonManager" behaviorConfiguration="ContractBehavior"> 
                <!--address - 服务地址--> 
                <!--binding - 通信方式--> 
                <!--contract - 服务契约--> 
                <endpoint address="" binding="basicHttpBinding" contract="ConfigurationNameTest" /> 
            </service> 
        </services> 
    </system.serviceModel> 
</configuration>
 
 
3、客户端
PersonManager.aspx
<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="PersonManager.aspx.cs" 
        Inherits="Contract_PersonManager" Title="契约(ServiceContract、OperationContract、DataContract、ServiceKnownType和DataMember)" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server"> 
</asp:Content> 
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"> 
        <asp:TextBox ID="txtName" runat="server" Text="webabcd" /> 
          
        <asp:Button ID="btnGetName" runat="server" Text="GetName"    
                 /> 
</asp:Content>
 
PersonManager.aspx.cs
InBlock.gif using System; 
InBlock.gif using System.Collections; 
InBlock.gif using System.Configuration; 
InBlock.gif using System.Data; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Web; 
InBlock.gif using System.Web.Security; 
InBlock.gif using System.Web.UI; 
InBlock.gif using System.Web.UI.HtmlControls; 
InBlock.gif using System.Web.UI.WebControls; 
InBlock.gif using System.Web.UI.WebControls.WebParts; 
InBlock.gif using System.Xml.Linq; 
InBlock.gif 
InBlock.gif public partial  class Contract_PersonManager : System.Web.UI.Page 
InBlock.gif
InBlock.gif         protected  void Page_Load( object sender, EventArgs e) 
InBlock.gif        { 
InBlock.gif 
InBlock.gif        } 
InBlock.gif 
InBlock.gif         protected  void btnGetName_Click( object sender, EventArgs e) 
InBlock.gif        { 
InBlock.gif                 // Contract.IPersonManager pm = new Contract.PersonManagerClient(); 
InBlock.gif 
InBlock.gif                Contract.PersonManagerClient proxy =  new Contract.PersonManagerClient(); 
InBlock.gif 
InBlock.gif                Contract.StudentModel sm =  new Contract.StudentModel() { PersonName = txtName.Text }; 
InBlock.gif 
InBlock.gif                Page.ClientScript.RegisterStartupScript( 
InBlock.gif                         this.GetType(), 
InBlock.gif                         "js"
InBlock.gif                         string.Format( "alert('{0}')", proxy.GetPersonName(sm)), 
InBlock.gif                         true); 
InBlock.gif 
InBlock.gif                proxy.Close(); 
InBlock.gif        } 
InBlock.gif}
 
Web.config
<?xml version="1.0"?> 
<configuration> 
    <system.serviceModel> 
        <client> 
            <!--address - 服务地址--> 
            <!--binding - 通信方式--> 
            <!--contract - 服务契约--> 
            <endpoint address="http://localhost:3502/ServiceHost/Contract/PersonManager.svc" binding="basicHttpBinding" contract="Contract.IPersonManager" /> 
        </client> 
    </system.serviceModel> 
</configuration>
 
 
运行结果:
单击"btnGetName"后弹出提示框,显示"Name: webabcd"


OK
[源码下载]     



     本文转自webabcd 51CTO博客,原文链接: http://blog.51cto.com/webabcd/344106 ,如需转载请自行联系原作者





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值