WCF 之 消息契约(MessageContract)
对于SOAP来说主要由两部分构成Header和Body,他们两个共同构成了SOAP的信封,通常来说Body保存具体的数据内容,Header保存一些上下文信息或关键信息。
比如:在一些情况下,具有这样的要求:当序列化一个对象并生成消息的时候,希望将部分数据成员作为SOAP的报头,部分作为消息的主体。比如说,我们有一个服务操作采用流的方式进行文件的上载,除了以流的方式传输以二进制表示的文件内容外,还需要传输一个额外的基于文件属性的信息,比如文件格式、文件大小等。一般的做法是将传输文件内容的流作为SOAP的主体,将其属性内容作为SOAP的报头进行传递。这样的功能,可以通过定义消息契约来实现。
由此可见,MessageContract的主要作用就是给我们提供了自己来操作SOAP的一种方式。
MessageContractAttribute:对控制消息头和消息体元素提供了强力支持。
所支持的属性: MessageHeaderAttribute 和 MessageBodyMemberAttribute。
用于及用途:
[1] 添加自定义头(custom headers);
[2] 控制消息是否被包装;
[3] 控制签名与加密;
一、[MessageContract]:
[1] 将一个类型转换为SOAP消息
类型可以包含消息头和消息体的元素
[2] 能够设置IsWrapped, ProtectionLevel
[3] 可以设置显式Name, Namespace
如下面的代码:
[MessageContract(IsWrapped=true, ProtectionLevel=ProtectionLevel.Sign)]
public class SaveLinkRequest
{…}
[MessageContract]
public class SaveLinkResponse
{…}
二、[MessageHeader]:
1 应用到消息契约的域(fields)或者(properties)
为创建自定义头提供了简单的方法
2 能够提供Name, Namespace, ProtectionLevel
3 可以设置SOAP协议的设置:Relay, Actor,MustUnderstand
三、[MessageBody]:
[1] 应用到消息契约的域(fields)或者属性(properties)
[2] 能够拥有多个body元素
– 等价于在操作中拥有多个参数
– 返回多个复杂类型数据的唯一方法
• 总是提供顺序(Order)
• 可以设置Name, Namespace, ProtectionLevel
[MessageContract(IsWrapped = true, ProtectionLevel = ProtectionLevel.Sign)]
public class SaveEventRequest
{
private string m_licenseKey;
private LinkItem m_linkItem;
[MessageHeader(ProtectionLevel = ProtectionLevel.EncryptAndSign)]
public string LicenseKey
{
get { return m_licenseKey; }
set { m_licenseKey = value; }
}
[MessageBodyMember]
public LinkItem LinkItem
{
get { return m_linkItem; }
set { m_linkItem = value; }
}
}
[MessageContract]
public class SaveEventResponse
{
}
那么如何应用消息契约那?不要急,下面来介绍,还是来看代码吧:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization; namespace ContentTypes
{
[DataContract]
public class LinkItem
{
private long m_id;
private string m_title;
private string m_description;
private DateTime m_dateStart;
private DateTime m_dateEnd;
private string m_url; [DataMember]
public long Id
{
get { return m_id; }
set { m_id = value; }
} [DataMember]
public string Title
{
get { return m_title; }
set { m_title = value; }
}
[DataMember]
public string Description
{
get { return m_description; }
set { m_description = value; }
}
[DataMember]
public DateTime DateStart
{
get { return m_dateStart; }
set { m_dateStart = value; }
}
[DataMember]
public DateTime DateEnd
{
get { return m_dateEnd; }
set { m_dateEnd = value; }
}
[DataMember]
public string Url
{
get { return m_url; }
set { m_url = value; }
}
}
}
LinkItem
重点看Message和Service代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using ContentTypes; namespace WcfServiceLibraryDemo
{
[MessageContract(IsWrapped = false)]
public class SaveGigRequest
{
private LinkItem m_linkItem; [MessageBodyMember]
public LinkItem Item
{
get { return m_linkItem; }
set { m_linkItem = value; }
}
} [MessageContract(IsWrapped = false)]
public class SaveGigResponse
{
} [MessageContract(IsWrapped = false)]
public class GetGigRequest
{
private string m_licenseKey; [MessageHeader]
public string LicenseKey
{
get { return m_licenseKey; }
set { m_licenseKey = value; }
}
} [MessageContract(IsWrapped = false)]
public class GetGigResponse
{
private LinkItem m_linkItem; public GetGigResponse()
{
} public GetGigResponse(LinkItem item)
{
this.m_linkItem = item;
} [MessageBodyMember]
public LinkItem Item
{
get { return m_linkItem; }
set { m_linkItem = value; }
}
}
}
Message
只要记住一条就行了,凡是有[MessageHeader]或[MessageBody]的那些属性,它们就是在客户端调用服务相应方法的参数。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using ContentTypes; namespace WcfServiceLibraryDemo
{
[ServiceContract(Name = "GigManagerServiceContract", Namespace = "http://www.cnblogs.com/Charlesliu", SessionMode = SessionMode.Required)]
public interface IGigManagerService
{
[OperationContract]
SaveGigResponse SaveGig(SaveGigRequest requestMessage); [OperationContract]
GetGigResponse GetGig(GetGigRequest requestMessage);
}
}
IService
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using ContentTypes; namespace WcfServiceLibraryDemo
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class GigManagerService : IGigManagerService
{ private LinkItem m_linkItem; #region IGigManager Members public SaveGigResponse SaveGig(SaveGigRequest requestMessage)
{
m_linkItem = requestMessage.Item;
return new SaveGigResponse();
} public GetGigResponse GetGig(GetGigRequest requestMessage)
{
if (requestMessage.LicenseKey != "XXX")
throw new FaultException("Invalid license key."); return new GetGigResponse(m_linkItem);
} #endregion
}
}
Service
客户端代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WinTest.MyServiceReference; namespace WinTest
{
public partial class Form1 : Form
{
MyServiceReference.GigManagerServiceContractClient m_proxy = new WinTest.MyServiceReference.GigManagerServiceContractClient(); public Form1()
{
InitializeComponent();
} private void cmdSave_Click(object sender, EventArgs e)
{
LinkItem item = new LinkItem(); item.Id = int.Parse(this.txtId.Text);
item.Title = this.txtTitle.Text;
item.Description = this.txtDescription.Text;
item.DateStart = this.dtpStart.Value;
item.DateEnd = this.dtpEnd.Value;
item.Url = this.txtUrl.Text; m_proxy.SaveGig(item);
} private void cmdGet_Click(object sender, EventArgs e)
{
LinkItem item = m_proxy.GetGig("XXX");
if (item != null)
{
this.txtId.Text = item.Id.ToString();
this.txtTitle.Text = item.Title;
this.txtDescription.Text = item.Description; if (item.DateStart != DateTime.MinValue)
this.dtpStart.Value = item.DateStart;
if (item.DateEnd != DateTime.MinValue)
this.dtpEnd.Value = (DateTime)item.DateEnd; this.txtUrl.Text = item.Url;
}
}
}
}
客户端
IsWrapped、WrapperName、WrapperNamespace:IsWrapped表述的含义是是否为定义的主体成员(一个或者多个)添加一个额外的根节点。WrapperName和WrapperNamespace则表述该根节点的名称和命名空间。IsWrapped、WrapperName、WrapperNamespace的默认是分别为true、类型名称和http://tempuri.org/。如果我们将IsWrapped的属性设为false,那么套在Address节点外的Customer节点将会从SOAP消息中去除。
我们在使用中发现一个特点,用这种方式序列化的实体类不能当作参数直接传递,客户端会把对象的一个参数拆分为多个属性作为参数。
WCF 之 消息契约(MessageContract)的更多相关文章
- 重温WCF之消息契约(MessageContract)(六)
对于SOAP来说主要由两部分构成Header和Body,他们两个共同构成了SOAP的信封,通常来说Body保存具体的数据内容,Header保存一些上下文信息或关键信息.比如:在一些情况下,具有这样的要 ...
- C# 的 WCF文章 消息契约(Message Contract)在流(Stream )传输大文件中的应用
我也遇到同样问题,所以抄下做MARK http://www.cnblogs.com/lmjq/archive/2011/07/19/2110319.html 刚做完一个binding为netTcpBi ...
- WCF契约之---服务契约 、数据契约、 消息契约
本篇博文只是简单说下WCF中的契约的种类.作用以及一些简单的代码示例.在WCF中契约分为服务契约.数据契约和消息契约.下面对这几种契约进行简单的介绍. 服务契约 服务契约描述了暴露给外部的类型(接口或 ...
- WCF技术剖析之十八:消息契约(Message Contract)和基于消息契约的序列化
原文:WCF技术剖析之十八:消息契约(Message Contract)和基于消息契约的序列化 [爱心链接:拯救一个25岁身患急性白血病的女孩[内有苏州电视台经济频道<天天山海经>为此录制 ...
- WCF把书读薄(3)——数据契约、消息契约与错误契约
上一篇:WCF把书读薄(2)——消息交换.服务实例.会话与并发 十二.数据契约 在实际应用当中数据不可能仅仅是以int Add(int num1, int num2)这种简单的几个int的方式进行传输 ...
- wcf消息契约
1.最多一个参数和一个返回值,返回值和参数的类型都是消息类型. 下面的代码为定义一个消息契约的实例 [MessageContract] public class MyMessage { ...
- [WCF编程]4.契约概述
一.契约的基本概念 契约是消息参与者之间的约定.在SOA架构中,契约提供了服务通信所必需的元数据.契约用来定义数据类型,操作,消息交换模式和消息交换使用的传输协议.契约通常是在标准化平台中使用与编程语 ...
- WCF中DataContract和MessageContract的区别
一.代码案例 首选建立2个WCF Service,它们分别使用不同的Contract,同时创建一个Console控制台项目,作为Client: 其中,WcfServiceWithDataContrac ...
- 【原创经验分享】WCF之消息队列
最近都在鼓捣这个WCF,因为看到说WCF比WebService功能要强大许多,另外也看了一些公司的招聘信息,貌似一些中.高级的程序员招聘,都有提及到WCF这一块,所以,自己也关心关心一下,虽然目前工作 ...
随机推荐
- django配置Ueditor
1.安装DjangoUeditor pip install DjangoUeditor 2.在Django中安装DjangoUedito app,在INSTALL_APPS里面增加DjangoUedi ...
- Codeforces 521 E cycling city
cf的一道题,非常有意思,题目是问图中是否存在两个点,使得这两个点之间有三条路径,而且三条路径没有公共点. 其实就是判断一下是否为仙人掌就行了,如果不是仙人掌的话肯定就存在,题目难在输出路径上,改了半 ...
- 【20181026T2】**图【最小瓶颈路+非旋Treap+启发式合并】
题面 [错解] 最大最小?最小生成树嘛 蛤?还要求和? 点分治? 不可做啊 写了个MST+暴力LCA,30pts,140多行 事后发现30分是给dijkstra的 woc [正解] 树上计数问题:①并 ...
- 【二分答案】【DFS】【分类讨论】Gym - 100851F - Froggy Ford
题意:河里有n块石头,一只青蛙要从左岸跳到右岸,你可以再在任意一个位置放一块石头,使得在最优方案下,青蛙单步跳的距离的最大值最小化,输出该位置. 将原图视作完全图,二分答案mid,然后在图中只保留小于 ...
- 最封闭的开源系统,话说Android的八宗罪
最封闭的开源系统,话说Android的八宗罪 http://www.ifanr.com/9833 Android 分裂的多面性 http://tech.cncms.com/shouji/android ...
- vagrant 常用命令以及常用操作
列出这些命令,主要是防止脑内存不足.目前这些命令是我常用的,以后其他命令用的多,我再继续添加... 分享些本人用的百度网盘box,国外的太坑... 本人分享的百度网盘:http://pan.baidu ...
- List集合多次排序
写在前面: 有时候我们在查询数据展示到前台页面的时候,需要对数据进行排序,特别是按照多个字段进行排序,会很麻烦写的代码也比较多.这个时候java8的特性可以让我们很方便的对数据进行排序. 话不多说,直 ...
- bzoj 3969: [WF2013]Low Power 二分
3969: [WF2013]Low Power Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnli ...
- hust 1590 - 方块游戏 数学
1590 - 方块游戏 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/problem/show/1590 D ...
- 《python学习手册》第35章 异常的设计
嵌套异常处理器 其实我们主要需要搞清楚的问题应该是这样的,当异常发生的时候,无论是简单的异常处理还是复杂的异常处理,我们都应该能够清楚的了解到异常运行到哪里,被谁捕获了,现在控制权到了哪里了,下面我们 ...