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:契约: 服 ...
随机推荐
- 读书笔记 C# yield return与yield break执行顺序的浅析
yield return可一次返回一个元素,并保留当前在代码中的位置,下次调用当前迭代器函数时,将从该位置从新执行.也就是说执行了yield return的时候,迭代器函数就返回了一个元素给forea ...
- 类型重命名 typedef
所谓数据重命名就是给数据类型起一个新的名字,比如int 这个数据类型,可以给他起一个新的名字叫 my int.他俩的用法.特点.属性等是一模一样,仅仅名字不同而已. 作用:1,增加代码的可读性.2,让 ...
- 《Python》网络编程之黏包
黏包 一.黏包现象 同时执行多条命令之后,得到的结果很可能只有一部分,在执行其他命令的时候又接收到之前执行的另外一部分结果,这种显现就是黏包. server端 import socket sk = s ...
- webView 的种种
1.关于UI 我们在设置webview的时候,有时候会发现在加载的过程中会出现一个黑色的条条,在加载完成的时候有得时候继续存在,有得时候消失不见. 这个黑边是由于webView.scrollview向 ...
- import 语句
声明package的语句必须在java类的有效代码第一行,所import语句要放在package 声明语句之后. import的语法格式为: import+空格+类全限定名+: 该语句的作用是, ...
- TCP/IP协议 计算机间的通讯,传输、socket 传输通道
#! /usr/bin/env python3 # -*- coding:utf-8 -*- #TCP/IP简介 #为了把全世界的所有不同类型的计算机都连接起来,就必须规定一套全球通用的协议,为了实现 ...
- 4.6 C++抽象基类和纯虚成员函数
参考:http://www.weixueyuan.net/view/6376.html 总结: 在C++中,可以通过抽象基类来实现公共接口 纯虚成员函数没有函数体,只有函数声明,在纯虚函数声明结尾加上 ...
- freemarker中的null异常处理以及!与??的使用(转)
原文链接: https://blog.csdn.net/mexican_jacky/article/details/50638062 阅读数:6304 如工程包含: 在user中我们有个角色,那么我们 ...
- IBM MQ 集成CXF 发送JMS 消息
0.POM依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w ...
- Day19作业及默写
三级菜单 menu = { '北京': { '海淀': { '五道口': { 'soho': {}, '网易': {}, 'google': {} }, '中关村': { '爱奇艺': {}, '汽车 ...