客户端自定义消息查看器实现IClientMessageInspector接口在消息发送之前添加消息头

class ClientMessageInspector : System.ServiceModel.Dispatcher.IClientMessageInspector, System.ServiceModel.Description.IEndpointBehavior
{
public Dictionary<string, string> HeaderValue { get; set; } #region Implementation for IClientMessageInspector
public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState) { } public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
{
if (HeaderValue != null)
{
foreach (var v in HeaderValue)
{
request.Headers.Add(System.ServiceModel.Channels.MessageHeader.CreateHeader(v.Key, "client", v.Value));
}
} return null;
}
#endregion #region Implementation for IEndpointBehavior
public void AddBindingParameters(System.ServiceModel.Description.ServiceEndpoint serviceEndpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters) { } public void ApplyClientBehavior(System.ServiceModel.Description.ServiceEndpoint serviceEndpoint, ClientRuntime behavior)
{
//此处为Extension附加到ClientRuntime。
behavior.MessageInspectors.Add(this);
} public void ApplyDispatchBehavior(System.ServiceModel.Description.ServiceEndpoint serviceEndpoint, EndpointDispatcher endpointDispatcher)
{
//如果是扩展服务器端的MessageInspector,则要附加到EndpointDispacther上了。
//endpointDispatcher.DispatchRuntime.MessageInspectors.Add(this);
} public void Validate(System.ServiceModel.Description.ServiceEndpoint serviceEndpoint) { }
#endregion
}
//添加消息头
client.ChannelFactory.Endpoint.Behaviors.Add(new ClientMessageInspector()
{
HeaderValue = new Dictionary<string, string>() {
{"OrganID", GetAppSetting("OrganID")},
{"HandshakeCode", GetAppSetting("HandshakeCode")}
}
});

服务器端获取消息头

private string GetHeaderValue(string name)
{
var headers = OperationContext.Current.IncomingMessageHeaders;
var index = headers.FindHeader(name, "client");
if (index > -1)
{
return headers.GetHeader<string>(index);
} return string.Empty;
}

WCF自动添加消息头的更多相关文章

  1. PHP自动添加http://头 转换网址为链接

    有时候,当我们需要用户输入网址的时候,一般我们会让用户省略掉"http://",当提交完成后用代码自动再加上http://,若有需要,我们 还可将网址转换成链接的形式,类似于众多网 ...

  2. springboot集成swagger添加消息头(header请求头信息)

    springboot集成swagger上篇文章介绍: https://blog.csdn.net/qiaorui_/article/details/80435488 添加头信息: package co ...

  3. PHP 下载文件时自动添加bom头的方法

    首先弄清楚,什么是bom头?在Windows下用记事本之类的程序将文本文件保存为UTF-8格式时,记事本会在文件头前面加上几个不可见的字符(EF BB BF),就是所谓的BOM(Byte order ...

  4. 火狐添加消息头 Modify Header Value (HTTP Headers)

    火狐浏览器添加组件 : Modify Header Value (HTTP Headers)

  5. Prism for WPF 搭建一个简单的模块化开发框架(四)异步调用WCF服务、WCF消息头添加安全验证Token

    原文:Prism for WPF 搭建一个简单的模块化开发框架(四)异步调用WCF服务.WCF消息头添加安全验证Token 为什么选择wcf?   因为好像wcf和wpf就是哥俩,,, 为什么选择异步 ...

  6. Unity VS 创建脚本自动添加头注释-时间-描述-作者等信息

    Unity生成脚本自动添加头注释 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心 ...

  7. 去除partner页面消息 自动添加关注者的功能

    某些公司希望在partner页面说些partner的坏话,可是odoo居然自动添加了partner关注,这就尴尬了.... 如果恰搭建了邮件服务器,很有可能就自动发到了客户邮箱里,等着炸锅吧.... ...

  8. 【WCF】自定义地址头的筛选器

    前面的文章中,老周已向大伙伴们介绍了如何在终结点上使用地址头,只要服务是沿着该终结点调用的,那么每一次调用都会自动把地址头插入到SOAP消息的Header列表中. 而通过前一篇文章中的示例,大家也看到 ...

  9. JMS消息头

    一个消息对象分为三部分:消息头(Headers),属性(Properties)和消息体(Payload).对于StreamMessage和MapMessage,消息本身就有特定的结构,而对于TextM ...

随机推荐

  1. vmware 安装dos注意

    vmware创建ms-dos虚拟机,安状DOS71.ISO. 新创建的机器,启动次序为Removable Devices/Hard Device/CD-ROM Device 新建的机器,第一次启动时, ...

  2. 【python】安装指定模块

    使用pip 1.卸载模块 sudo pip uninstall xxx 2.安装指定版本模块 sudo pip install xxx==2.0.1.3

  3. 转载:《.NET 编程结构》专题汇总(C#)

    <.NET 编程结构>专题汇总(C#) - M守护神 - 博客园http://www.cnblogs.com/liusuqi/p/3213597.html 前言     掌握一门技术,首要 ...

  4. November 2nd Week 45th Wednesday 2016

    If your ship doesn't come in, swim out to it. 如果你的船不驶进来,那你就朝他游过去吧! Swim out to it, don't fear that y ...

  5. UIAlertController使用

    // 将UIAlertController模态出来 相当于UIAlertView show 的方法// 初始化一个一个UIAlertController    // 参数preferredStyle: ...

  6. CENTOS7 mysql 安装

    CentOS7的yum源中默认好像是没有mysql的.为了解决这个问题,我们要先下载mysql的repo源. 1. 下载mysql的repo源 $ wget http://repo.mysql.com ...

  7. 用.net 发送邮件

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.N ...

  8. php抽奖代码

    1.经典概率算法抽奖 $tmpItems = ['电脑'=>10, '相机'=>50, '100元现金'=>500]; $proSum = array_sum($tmpItems); ...

  9. 【Java并发系列01】Thread及ThreadGroup杂谈

    img { border: solid black 1px } 一.前言 最近开始学习Java并发编程,把学习过程记录下.估计不是那么系统,主要应该是Java API的介绍(不涉及最基础的概念介绍), ...

  10. strtok源码 bitset 空间压缩

    源代码里有一段: unsigned char map[32]; /* Clear control map */ for (count = 0; count < 32; count++) map[ ...