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服务是通过对应的终结点发布出来的 发布出来的数据称 ...
随机推荐
- netty4 Handler的执行顺序
转载:https://my.oschina.net/jamaly/blog/272385 Handler在netty中,无疑占据着非常重要的地位.Handler与Servlet中的filter很像,通 ...
- Solr 4.3.0 配置Data import handler时出错
启动solr的时候,居然出现了如下的错误: org.apache.solr.common.SolrException: RequestHandler init failure at or ...
- 闲聊CSS之关于clearfix--清除浮动[转]
.clearfix:after { content: " "; display: block; clear: both; height:; } .clearfix { zoom:; ...
- qml json 解析到 ListView
https://github.com/kromain/qml-utils/tree/master/JSONListModel 非常棒!! 实现的原理如下文: http://goessner.net/a ...
- fwite写入文件
用双引号(")定义字符串,PHP 懂得更多特殊字符的转义序列: 转移序列 说明 \n 换行 \r 回车 \t 水平制表符 \[/td> 反斜线 \$ 美元符号 \" 双引号 ...
- UDPsocket编程
socket编程UDP模式, package com.wtd.socket.udp; import java.io.IOException; import java.net.DatagramPacke ...
- FFT一周目开坑!
先来一段非递归! #include<bits/stdc++.h> using namespace std; #define N ((1<<18)+3) ); struct ve ...
- 类似UC天气下拉和微信下拉眼睛头部弹入淡出UI交互效果(开源项目)。
Android-PullLayout是github上的一个第三方开源项目,该项目主页是:https://github.com/BlueMor/Android-PullLayout 原作者项目意图实现 ...
- HDU 2181 哈密顿绕行世界问题 dfs 难度:1
http://acm.hdu.edu.cn/showproblem.php?pid=2181 只有20个城市,而且每个点的度数恰好是3,也就意味着,对于即将进入环中的点,入度1,出度2,下一个点只有两 ...
- C#常用实例
1 時間 1.1 顯示在走的時間 控件:TextBox為顯示日期時間,命名為txtDateTimer Timer為時鐘,命名為time private void dtDateTimer_Tick(ob ...