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" /> <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)(转)的更多相关文章
- wcf契约随记
1.wcf契约分为:服务契约,操作契约,消息契约.数据契约 -------------------服务契约: [ServiceContract( Name = "name_IUser&quo ...
- WCF契约定义及主要用途
我们在使用WCF时,对其制定各种各样的规则,就叫做WCF契约.任何一个分布式的应用程序在传递消息的时候都需要实现制定一个规则. WCF配置文件相关操作技巧解析 全方位解读WCF Address配置文件 ...
- 【WCF--初入江湖】06 WCF契约服务行为和异常处理
06 WCF契约服务行为和异常处理 一.WCF契约服务行为 [1] 服务行为可以修改和控制WCF服务的运行特性. 在实现了WCF服务契约后,可以修改服务的很多执行特性. 这些行为(或者特性)是通过配置 ...
- 谈谈WCF中的Data Contract(3):WCF Data Contract对Collection & Dictionary的支持
谈谈WCF中的Data Contract(3):WCF Data Contract对Collection & Dictionary的支持 在本篇文章上一部分Order Processing的例 ...
- WCF - 契约
契约就是双方或多方就某个问题达成的一种的共识 服务提供者通过契约的形式将服务公布出来 服务消费者根据契约来消费 这样通过契约这个中间者就可以规范服务提供的内容 而服务消费者也可以根据契约来使用服务端 ...
- WCF契约之---服务契约 、数据契约、 消息契约
本篇博文只是简单说下WCF中的契约的种类.作用以及一些简单的代码示例.在WCF中契约分为服务契约.数据契约和消息契约.下面对这几种契约进行简单的介绍. 服务契约 服务契约描述了暴露给外部的类型(接口或 ...
- XML序列化与REST WCF Data Contract匹配时遇到的2个问题
问题一: XML序列化与RESTful WCF Data Contract不能匹配,无法传递类的值. 现象: 给类加上[Serializable]Attribute,可以成功序列化,但是WCF Ser ...
- 再说WCF Data Contract KnownTypeAttribute
WCF 中的序列化是用DataContractSerializer,所有被[DataContract]和[DataMemeber]标记的类和属性会被DataContractSerializer序列化. ...
- wcf契约版本处理与异常处理(随记)
-----------版本控制策略:必须支持向后兼容:----就是当服务端发生改变,但客户端未更新会不会发生错误: 一旦契约发布,若要契约发生变化,如何不影响客户端使用: ----wsdl:契约: 服 ...
随机推荐
- 二. Python基础(2)--语法
二. Python基础(2)--语法 1.实现一个简单的登录系统 '''# 形式1 n = 1 while n < 4: name = input("请输入姓名\n" ...
- 5.6 C++重载下标操作符
参考:http://www.weixueyuan.net/view/6384.html 总结: 下标操作符是必须要以类的成员函数的形式进行重载的.其在类中的声明格式如下: 返回类型 & ...
- 关于macroblaze的一些理解(更新中)
(1)添加*.elf文件: 在Design Sources工作目录中右键选择添加源文件,找到SDK目录中对应的文件夹下的Debug内*.elf文件,将其添加.然后,源文件目录更新,多出一个ELF文件夹 ...
- Java与C++简单对比
Java语言让编程者无法找到指针来直接访问内存,并且增添了自动的内存管理功能,从而有效的组织了C/C++语言中指针操作失误,如滥用指针所造成的系统崩溃,Java的指针在虚拟机内部使用,这保证了Java ...
- FindResource () RT_HTML 为什么总是出错呢 ?
#include <windows.h> #include <commdlg.h> #include <ole2.h> BOOL GetHtmlResource(L ...
- softmax与多分类
sotfmax 函数在机器学习和深度学习中有着广泛的应用, 主要用于多分类问题. softmax 函数 1. 定义 假定数组V,那么第i个元素的softmax值为 也就是该元素的指数 除以 所有元素的 ...
- webpack对多个模块依赖进行打包
[ webpack3.0.0刚刚出来 所以文章是跟着低版本 教程 操作熟悉 结果好多对不上喔] 七:webpack对多个模块依赖进行打包 通过一刚开始我们了解到 webpack支持commonJS ...
- L259 合同
We are satisfied with the terms of this contract for the most part, but we feel that your terms of p ...
- day 54 jQuery 的初步基础
jQuery介绍 jQuery是一个轻量级的.兼容多浏览器的JavaScript库. jQuery使用户能够更方便地处理HTML Document.Events.实现动画效果.方便地进行Ajax交互, ...
- Windows 窗体设计器生成的代码
namespace 窗体的浮动及隐藏{ partial class Form1 { /// <summary> /// 必需的设计器变量. ...