对于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)的更多相关文章

  1. 重温WCF之消息契约(MessageContract)(六)

    对于SOAP来说主要由两部分构成Header和Body,他们两个共同构成了SOAP的信封,通常来说Body保存具体的数据内容,Header保存一些上下文信息或关键信息.比如:在一些情况下,具有这样的要 ...

  2. C# 的 WCF文章 消息契约(Message Contract)在流(Stream )传输大文件中的应用

    我也遇到同样问题,所以抄下做MARK http://www.cnblogs.com/lmjq/archive/2011/07/19/2110319.html 刚做完一个binding为netTcpBi ...

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

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

  4. WCF技术剖析之十八:消息契约(Message Contract)和基于消息契约的序列化

    原文:WCF技术剖析之十八:消息契约(Message Contract)和基于消息契约的序列化 [爱心链接:拯救一个25岁身患急性白血病的女孩[内有苏州电视台经济频道<天天山海经>为此录制 ...

  5. WCF把书读薄(3)——数据契约、消息契约与错误契约

    上一篇:WCF把书读薄(2)——消息交换.服务实例.会话与并发 十二.数据契约 在实际应用当中数据不可能仅仅是以int Add(int num1, int num2)这种简单的几个int的方式进行传输 ...

  6. wcf消息契约

    1.最多一个参数和一个返回值,返回值和参数的类型都是消息类型. 下面的代码为定义一个消息契约的实例 [MessageContract]    public class MyMessage    {   ...

  7. [WCF编程]4.契约概述

    一.契约的基本概念 契约是消息参与者之间的约定.在SOA架构中,契约提供了服务通信所必需的元数据.契约用来定义数据类型,操作,消息交换模式和消息交换使用的传输协议.契约通常是在标准化平台中使用与编程语 ...

  8. WCF中DataContract和MessageContract的区别

    一.代码案例 首选建立2个WCF Service,它们分别使用不同的Contract,同时创建一个Console控制台项目,作为Client: 其中,WcfServiceWithDataContrac ...

  9. 【原创经验分享】WCF之消息队列

    最近都在鼓捣这个WCF,因为看到说WCF比WebService功能要强大许多,另外也看了一些公司的招聘信息,貌似一些中.高级的程序员招聘,都有提及到WCF这一块,所以,自己也关心关心一下,虽然目前工作 ...

随机推荐

  1. poj3259(spfa)

    自己的第一道spfa,纪念一下,顺便转载一下spfa的原理.先po代码: #include <iostream> #include <queue> using namespac ...

  2. 【BZOJ 4229】 4229: 选择 (线段树+树链剖分)

    4229: 选择 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 67  Solved: 41 Description 现在,我想知道自己是否还有选择. ...

  3. 一个安卓应用 多少个 dalvik 虚拟机

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 一个

  4. Effective Java部分读书笔记

    2.创建和销毁对象 1.使用静态工厂方法代替构造器 一般使用构造器(构造函数)创建对象实例,还可以使用静态工厂方法来创建对象实例. 优点 使用静态工厂方法代替构造器创建对象实例有以下优点: 1)静态构 ...

  5. ngxin error日志

    日志模块ngx_errlog_module对于支持可变参数平台提供的三个接口 #define ngx_log_error(level, log, ...) \ if ((log)->log_le ...

  6. leetcode187. Repeated DNA Sequences

    https://leetcode.com/problems/repeated-dna-sequences/#/description   https://leetcode.com/problems/r ...

  7. 【转】2012年7月9 – 知名网页游戏公司 PHP高级工程师 最新面试题

    开头先唠叨两句,今天下午,上海的天热的让人窒息啊.Google下地图,好远!要做公交,想想就是人挤人.咬了下牙,打的,尼玛百来块啊,有木有!麻麻的,更让我萌生买车的决心了. 到了公司,环境不错.前台拿 ...

  8. java反射机制简单介绍

    1.字节码.所谓的字节码就是当java虚拟机载入某个类的对象时,首先须要将硬盘中该类的源码编译成class文件的二进制代码(字节码),然后将class文件的字节码载入到内存中,之后再创建该类的对象 2 ...

  9. .NET:C#的匿名委托 和 Java的匿名局部内部类

    背景 这几天重温Java,发现Java在嵌套类型这里提供的特性比较多,结合自身对C#中匿名委托的理解,我大胆的做了一个假设:Java的字节码只支持静态嵌套类,内部类.局部内部类和匿名局部内部类都是编译 ...

  10. Eclipse搭建Gradle环境

    转自:http://blog.sina.com.cn/s/blog_4b20ae2e0102uz4t.html 1.上Grandle官网下载Gradle,地址:http://www.gradle.or ...