示例

  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

WCF- 契约Contract(ServiceContract、OperationContract、DataContract、ServiceKnownType和DataMember)(转)的更多相关文章

  1. wcf契约随记

    1.wcf契约分为:服务契约,操作契约,消息契约.数据契约 -------------------服务契约: [ServiceContract( Name = "name_IUser&quo ...

  2. WCF契约定义及主要用途

    我们在使用WCF时,对其制定各种各样的规则,就叫做WCF契约.任何一个分布式的应用程序在传递消息的时候都需要实现制定一个规则. WCF配置文件相关操作技巧解析 全方位解读WCF Address配置文件 ...

  3. 【WCF--初入江湖】06 WCF契约服务行为和异常处理

    06 WCF契约服务行为和异常处理 一.WCF契约服务行为 [1] 服务行为可以修改和控制WCF服务的运行特性. 在实现了WCF服务契约后,可以修改服务的很多执行特性. 这些行为(或者特性)是通过配置 ...

  4. 谈谈WCF中的Data Contract(3):WCF Data Contract对Collection & Dictionary的支持

    谈谈WCF中的Data Contract(3):WCF Data Contract对Collection & Dictionary的支持 在本篇文章上一部分Order Processing的例 ...

  5. WCF - 契约

    契约就是双方或多方就某个问题达成的一种的共识  服务提供者通过契约的形式将服务公布出来 服务消费者根据契约来消费 这样通过契约这个中间者就可以规范服务提供的内容 而服务消费者也可以根据契约来使用服务端 ...

  6. WCF契约之---服务契约 、数据契约、 消息契约

    本篇博文只是简单说下WCF中的契约的种类.作用以及一些简单的代码示例.在WCF中契约分为服务契约.数据契约和消息契约.下面对这几种契约进行简单的介绍. 服务契约 服务契约描述了暴露给外部的类型(接口或 ...

  7. XML序列化与REST WCF Data Contract匹配时遇到的2个问题

    问题一: XML序列化与RESTful WCF Data Contract不能匹配,无法传递类的值. 现象: 给类加上[Serializable]Attribute,可以成功序列化,但是WCF Ser ...

  8. 再说WCF Data Contract KnownTypeAttribute

    WCF 中的序列化是用DataContractSerializer,所有被[DataContract]和[DataMemeber]标记的类和属性会被DataContractSerializer序列化. ...

  9. wcf契约版本处理与异常处理(随记)

    -----------版本控制策略:必须支持向后兼容:----就是当服务端发生改变,但客户端未更新会不会发生错误: 一旦契约发布,若要契约发生变化,如何不影响客户端使用: ----wsdl:契约: 服 ...

随机推荐

  1. 理解JavaScript的运行

    JavaScript可以运行在head和body标签中! HTML的脚本必须放在<script></script>标签中间! 浏览器会解释并执行位于script标签中的脚本! ...

  2. awk使用教程

    gawk - pattern scanning and processing language 基本用法:gawk [options] 'program' FILE ... program:PATTE ...

  3. VS2010编译Unigine_2010源码

    VS2010编译Unigine_2010源码[Debug版本] 1.Laucher工程属性改为控制台项目 2.Unigine工程编译时的Warnning LNK2019 a.属性--常规-目标文件名改 ...

  4. Android : 修改内核源码 and 编译、打包成新的boot.img

    一.Android内核源码的下载: 1.Google GIT地址: $ git clone https://android.googlesource.com/kernel/common.git $ g ...

  5. C#正则表达式MatchCollection类

    认识MatchCollection 类 表示通过以迭代方式将正则表达式模式应用于输入字符串所找到的成功匹配的集合. 命名空间:  System.Text.RegularExpressions 属性:C ...

  6. Python pandas快速入门

    Python pandas快速入门2017年03月14日 17:17:52 青盏 阅读数:14292 标签: python numpy 数据分析 更多 个人分类: machine learning 来 ...

  7. “Cannot make a static reference to the non-static method”处理方法

    报错原文:Cannot make a static reference to the non-static method maxArea(Shape[]) from the type ShapeTes ...

  8. jq设置控件可用不可用

    $("#tj").attr("disabled", true); //不可用 $("#tj").removeAttr("disab ...

  9. Python 封装

    # 封装: # 1. 对属性的封装 # 2. 对功能的封装 # 3. 模块 # 4. 包 class Student: def __init__(self, num, name, clazz): se ...

  10. 交换机的默认网关(跨网段telnet)

    实验要求:配置一台交换机,并配置默认网关,使不同网段的主机能够远程telnet连接到交换机 拓扑图如下: 交换机配置: enable 进入特权模式 configure terminal 进入全局模式 ...