WCF- 契约Contract(ServiceContract、OperationContract、DataContract、ServiceKnownType和DataMember)(转)

时间:2023-03-08 16:16:33
WCF- 契约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
{
  /**//// <summary>
  /// 人员管理接口
  /// </summary>
  // 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
  {
    /**//// <summary>
    /// 获取某人的姓名
    /// </summary>
    /// <param name="p">Person对象</param>
    /// <returns></returns>
    // 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
{
  /**//// <summary>
  /// 人员管理类
  /// </summary>
  public class PersonManager : IPersonManager
  {
    /**//// <summary>
    /// 获取某人的姓名
    /// </summary>
    /// <param name="p">Person对象</param>
    /// <returns></returns>
    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
{
  /**//// <summary>
  /// Person的实体类
  /// </summary>
  // Name - 数据契约的名称(会对应到相关的wsdl,默认情况下本例为类名“Person”)
  [DataContract(Name = "PersonModel")]
  public class Person
  {
    /**//// <summary>
    /// Person的实体类的Age属性
    /// </summary>
    // Name - 数据成员的名称(会对应到相关的wsdl,默认情况下本例为属性名“Age”)
    // IsRequired - 该值指示序列化引擎该成员在读取或反序列化时必须存在
    // Order - 数据成员在相关的wsdl中的顺序
    // EmitDefaultValue - 如果应该在序列化流中生成成员的默认值,则为 true,否则为 false,默认值为 true
    [DataMember(Name = "PersonAge", IsRequired = false, Order = 1)]
    public int Age { get; set; }
  
    /**//// <summary>
    /// Person的实体类的Name属性
    /// </summary>
    // Name - 数据成员的名称(会对应到相关的wsdl,默认情况下本例为属性名“Name”)
    // IsRequired - 该值指示序列化引擎该成员在读取或反序列化时必须存在
    // Order - 数据成员在相关的wsdl中的顺序
    // EmitDefaultValue - 如果应该在序列化流中生成成员的默认值,则为 true,否则为 false,默认值为 true
    [DataMember(Name = "PersonName", IsRequired = false, Order = 0)]
    public string Name { get; set; }
  }
}

本文示例代码或素材下载

双击代码全选
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
  
using System.ServiceModel;
using System.Runtime.Serialization;
  
namespace WCF.ServiceLib.Contract
{
  /**//// <summary>
  /// Student的实体类
  /// </summary>
  // Name - 数据契约的名称(会对应到相关的wsdl,默认情况下本例为类名“Student”)
  [DataContract(Name = "StudentModel")]
  public class Student : Person
  {
    /**//// <summary>
    /// Student的实体类的School属性
    /// </summary>
    // 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

双击代码全选
1
<%@ ServiceHost Language="C#" Debug="true" Service="WCF.ServiceLib.Contract.PersonManager" %>

  Web.config

双击代码全选
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?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

双击代码全选
1
2
3
4
5
6
7
8
9
10
11
<%@ 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" />
  &nbsp;
  <asp:Button ID="btnGetName" runat="server" Text="GetName"
    onclick="btnGetName_Click" />
</asp:Content>

  PersonManager.aspx.cs

双击代码全选
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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

双击代码全选
1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0"?>
<configuration>
 <system.serviceModel>
  <client>
   <!--address - 服务地址-->
   <!--binding - 通信方式-->
   <!--contract - 服务契约-->
   <endpoint address="<a href="http://localhost:3502/ServiceHost/Contract/PersonManager.svc">http://localhost:3502/ServiceHost/Contract/PersonManager.svc</a>" binding="basicHttpBinding" contract="Contract.IPersonManager" />
  </client>
 </system.serviceModel>
</configuration>

  运行结果:

  单击"btnGetName"后弹出提示框,显示"Name: webabcd"

  OK

http://tech.ddvip.com/2008-11/122664417492618_2.html