wcf之OperationContextScope
作用:使用消息头向服务发送额外的信息。
1.客户端代码如下:
namespace Client
{
class Program
{
static void Main(string[] args)
{
CalculatorClient client = new CalculatorClient("secure");
double n1 = 5.6;
double n2 = 7.3;
double result; result = client.Add(n2, n1);
Console.WriteLine("执行加法后的结果为:{0}", result.ToString()); result = client.Subtract(n2, n1);
Console.WriteLine("执行减法后的结果为:{0}", result.ToString()); result = client.Multiply(n1, n2);
Console.WriteLine("执行乘法后的结果为:{0}", result.ToString()); result = client.Divide(n1, n2);
Console.WriteLine("执行除法后的结果为:{0}", result.ToString()); //CalculatorSessionClient clientSeesion = new CalculatorSessionClient();
//string s = clientSeesion.test("你好我做一个测试!");
//string b = clientSeesion.GetServiceDescriptionInfo();
//Console.WriteLine(s);
//Console.WriteLine(b); Test(); } static void Test()
{
CalculatorSessionClient wcfClient = new CalculatorSessionClient();
try
{
using (OperationContextScope scope = new OperationContextScope(wcfClient.InnerChannel))
{
MessageHeader header
= MessageHeader.CreateHeader(
"Service-Bound-CustomHeader",
"http://Microsoft.WCF.Documentation",
"Custom Happy Value."
);
OperationContext.Current.OutgoingMessageHeaders.Add(header); // Making calls.
Console.WriteLine("Enter the greeting to send: ");
string greeting = Console.ReadLine(); //Console.ReadLine();
header = MessageHeader.CreateHeader(
"Service-Bound-OneWayHeader",
"http://Microsoft.WCF.Documentation",
"Different Happy Value."
);
OperationContext.Current.OutgoingMessageHeaders.Add(header);//TODO:自定义传出消息头的用处? // One-way
wcfClient.test(greeting); // Done with service.
wcfClient.Close();
Console.WriteLine("Done!");
Console.ReadLine();
}
}
catch (TimeoutException timeProblem)
{
Console.WriteLine("The service operation timed out. " + timeProblem.Message);
Console.ReadLine();
wcfClient.Abort();
}
catch (CommunicationException commProblem)
{
Console.WriteLine("There was a communication problem. " + commProblem.Message);
Console.ReadLine();
wcfClient.Abort();
} }
}
}
2.服务端代码:
namespace Microsoft.ServiceModel.Samples
{
class Program
{
static void Main(string[] args)
{
//创建一个ServiceHost
using (ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService)))
{
// Open the ServiceHost to create listeners
serviceHost.Open();
Console.WriteLine("服务已经开启!");
Console.WriteLine("按回车键结束服务!");
Console.WriteLine();
Console.ReadLine(); serviceHost.Close(); }
} }
[ServiceContract]//定义服务协定完成
public interface ICalculator
{
[OperationContract]
double Add(double n1, double n2);
[OperationContract]
double Subtract(double n1, double n2);
[OperationContract]
double Multiply(double n1, double n2);
[OperationContract]
double Divide(double n1, double n2);
} [ServiceContract]
public interface ICalculatorSession
{
[OperationContract]
string test(string s); [OperationContract]
string GetServiceDescriptionInfo();
} public class CalculatorService : ICalculator, ICalculatorSession
{
public double Add(double n1, double n2)
{
return n1 + n2;
} public double Subtract(double n1, double n2)
{
return n1 - n2;
} public double Multiply(double n1, double n2)
{
return n1 * n2;
} public double Divide(double n1, double n2)
{
return n1 / n2;
} public string test(string s)
{
Console.WriteLine("Service Said" + s);
WriteHeaders(OperationContext.Current.IncomingMessageHeaders);
return s;
} public string GetServiceDescriptionInfo()
{
StringBuilder sb = new StringBuilder();
OperationContext operationContext = OperationContext.Current;
ServiceHost serviceHost = (ServiceHost)operationContext.Host;
ServiceDescription dec = serviceHost.Description;
sb.Append("Base addresses:\n");
foreach (Uri url in serviceHost.BaseAddresses)
{
sb.Append(" " + url + "\n");
}
sb.Append("Service endpoint:\n");
foreach (ServiceEndpoint endPoint in dec.Endpoints)
{
sb.Append("Address:" + endPoint.Address + "\n");
sb.Append("Binding:" + endPoint.Binding + "\n");
sb.Append("Contract:" + endPoint.Contract + "\n");
} return sb.ToString();
} private void WriteHeaders(MessageHeaders headers)
{
foreach (MessageHeaderInfo header in headers)
{
Console.WriteLine("\t" + header.Actor);
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("\t" + header.Name);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("\t" + header.Namespace);
Console.WriteLine("\t" + header.Relay);
if (header.IsReferenceParameter == true)
{
Console.WriteLine("IsReferenceParameter header detected: " + header.ToString());
}
}
Console.ResetColor();
}
} }
这个实例演示的是客户端通过OutgoingMessageHeaders添加了一些信息,然后服务端通过IncomingMessageHeaders
附:(也许你会有疑问,事情都是OperationContext做的,要OperationContextScope 干什么:个人理解OperationContextScope 类似于数据库连接对象类,wcfClient.InnerChannel就好像是连接字符串,告诉要连接到哪去,整个OperationContextScope 块就像是数据库中的当期连接,信息头就好像是sql语句,出了OperationContextScope 块范围就好数据库断开了连接,进行其他操作要重新连接,OperationContext 就好像是Command对象执行一些操作)
OperationContextScope 对象建立了当前操作上下文之后,可以使用 OperationContext 执行以下操作:
访问和修改传入和传出消息头和其他属性。
访问运行库,包括调度程序、主机、信道和扩展。
访问其他类型的上下文,如安全、实例和请求上下文。
访问与 OperationContext 对象关联的信道,或(如果信道实现System.ServiceModel.Channels.ISession)访问关联信道的会话标识符。
创建了 OperationContextScope 后,将存储当前的 OperationContext,并且新的 OperationContext 由Current 属性所返回。释放 OperationContextScope 后,将还原原始 OperationContext。
wcf之OperationContextScope的更多相关文章
- 【.net深呼吸】(WCF)OperationContextScope 的用途
一个WCF服务可以实现多个服务协定(服务协定实为接口),不过,每个终结点只能与一个服务协定关联,并指定调用的唯一地址.那么,binding是干吗的?binding是负责描述通信的协议,以及消息是否加密 ...
- 关于WEB Service&WCF&WebApi实现身份验证之WCF篇(2)
因前段时间工作变动(换了新工作)及工作较忙暂时中断了该系列文章,今天难得有点空闲时间,就继续总结WCF身份验证的其它方法.前面总结了三种方法(详见:关于WEB Service&WCF& ...
- 重温WCF之发送和接收SOAP头(三)
SOAP头可以理解为一种附加信息,就是附加到消息正文的内容. 既然消息头是附加信息,那有啥用呢?你可别说,有时候还真有不少用处.举个例子,WCF的身份验证是不是很麻烦?还要颁发什么证书的(当然不是荣誉 ...
- 三十、【C#.Net开发框架】WCFHosting服务主机的利用WCF服务通讯和实现思路
回<[开源]EFW框架系列文章索引> EFW框架源代码下载V1.3:http://pan.baidu.com/s/1c0dADO0 EFW框架实例源代码下载:http://p ...
- 十九、【.Net开源】EFW框架核心类库之WCF控制器
回<[开源]EFW框架系列文章索引> EFW框架源代码下载V1.1:http://pan.baidu.com/s/1qWJjo3U EFW框架实例源代码下载:http://pan.baid ...
- WCF note1
Summary of WCF Client to use service use ChannelFactory to create proxy to use service. Client code ...
- 传说中的WCF(4):发送和接收SOAP头
如果你实在不明白Header是个啥玩意儿,你就想一想你发送电子邮件时,是不是有个叫“附件”的东东?对啊,那么SOAP头是不是可以理解为一种附加信息?就是附加到消息正文的内容. 消息正文又是啥?WCF除 ...
- WCF - 消息
SOAP SOAP是Simple Object Access Protocol(简单对象访问协议)的简称 而如今SOAP已经成为了符合W3C制定的SOAP规范的消息 允许您使用 XML 在通过低层 I ...
- WCF - 地址
WCF顾名思义 即解决在windows平台下与各种平台中的程序之间通信的问题 而终结点则是WCF通信的唯一手段 终结点承载了所有通信的功能 一个WCF服务是通过对应的终结点发布出来的 发布出来的数据称 ...
随机推荐
- springmvc:BeanNameViewResolver访问内部资源视图对象和访问外部资源视图对象
<!-- 处理器映射器 --> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerM ...
- 在Linux下安装aws命令行操作
使用安装包安装 环境: Linux, OS X, or Unix Python 2 version 2.6.5+ or Python 3 version 3.3+ 检查Python版本 $ pytho ...
- C++的vector学习abc
开始学习和使用vector了,用到之后再去学似乎神迹的感觉啊,就像跑一下就能给个糖吃哈哈 百度上的六种初始化的方法就不再说了,那些方法都很对. 如果没有值初始化,系统会自行初始化,目前我遇到的是用脚标 ...
- 我的R代码备份
1 #f1=scan(file="f1.txt"); 2 #f2=scan(file="f2.txt"); 3 f=scan(file="5.2_ ...
- jupyter
Pip install jupyter To run: jupyter notebook 基本操作 执行当前cell,并自动跳到下一个cell:Shift Enter 执行当前cell,执 ...
- windows下捕获dump之Google breakpad_client的理解
breakpad是Google开源的一套跨平台工具,用于dump的处理.很全的一套东西,我这里只简单涉及breakpad客户端,不涉及纯文本符号生成,不涉及dump解析. 一.使用 最简单的是使用进程 ...
- [转]Linux下用gcc/g++生成静态库和动态库(Z)
Linux下用gcc/g++生成静态库和动态库(Z) 2012-07-24 16:45:10| 分类: linux | 标签:链接库 linux g++ gcc |举报|字号 订阅 ...
- mysql用户备份与修复
1.修复表repair table tb1 [use frm]; #红色部分代表可添加也可不加, 2.show variables like '%timeout%'; #查询关键字 3. 更改数据, ...
- K最近邻
k算法实现的步骤: 第一:确定K值(就是指最近邻居的个数).一般是一个奇数,因为测试样本个数有限, 第二:确定度量的长度,也就是余弦值,根据公式来算: 然后根据这个距离,排序大小,从中选出前k ...
- hdu 4616 Game
http://acm.hdu.edu.cn/showproblem.php?pid=4616 要记录各种状态的段 a[2][4] a[0][j]表示以trap为起点一共有j个trap的最优值 a[1 ...