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:契约: 服 ...
随机推荐
- FFT模板(无讲解)
#include<bits/stdc++.h> using namespace std; ; const double pi=3.1415926535898; ],len; struct ...
- Saiku的下载与安装(一)
Saiku- 数据可视化的工具,连接数据源展示数据,并且可方便导出xls/csv/pdf等文件的工具 一.Saiku下载 社区网址:https://community.meteorite.bi/ 二. ...
- 201621123001 《Java程序设计》第10周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 2. 书面作业 本次PTA作业题集 异常 1. 常用异常 结合题集题目7-1回答 1.1 自己以前编写的代码中经常出 ...
- r-mq实现顺序消费,不重复消费
根据订单号,同一订单号的消息,会被发送到同一个topic下的同一个queue,发送端的有序,会导致topic中消息的有序,而consumer和queue是一对多?的关系.可以保证topic中的有顺序的 ...
- poj3279(dfs+二进制枚举思路)
题意转载自https://www.cnblogs.com/blumia/p/poj3279.html 题目属性:DFS 相关题目:poj3276 题目原文:[desc]Farmer John know ...
- 【转】CEF3加载网页---多字节字符集和UNICODE字符集
static char* MBSCToCEF(const char* mbcsStr) { wchar_t* wideStr; char* utf8Str; int charLen; charLen ...
- ios初识UITableView及简单用法二(模型数据)
// // ViewController.m // ZQRTableViewTest // // Created by zzqqrr on 17/8/24. // Copyright (c) 2017 ...
- python DRF操作流程
# 其实就是Django RESTful Framework # RESTful一种API的命名风格,主要因为前后端分离开发出现 # 前后端分离: 用户访问静态文件的服务器,数据全部由ajax请求给到 ...
- 2019-02-28-day001-python介绍
今日内容大纲: 01 cpu 内存 硬盘 操作系统 CPU:中央处理器,相当于人大脑.---------飞机 内存:临时存储数据. 8g,16g,-----------高铁 1,成本高. 2,断电即消 ...
- Microsoft Project 常用快捷键
任务升级 : ALT + SHIFT + 向左键 任务降级: ALT + SHIFT + 向右键 滚动到表头(第一个任务):Ctrl + HOME 滚动到表尾(最后一个任务):Ctrl + E ...