热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用消息头向服务发送额外的信息的实例演示

本文介绍了使用消息头向服务发送额外信息的实例演示。客户端通过OutgoingMessageHeaders添加信息,服务端通过IncomingMessageHeaders获取信息。同时解释了OperationContextScope的作用和使用方法。该实例涉及到WCF技术。

作用:使用消息头向服务发送额外的信息。

1.客户端代码如下:

   



1 namespace Client
2 {
3 class Program
4 {
5 static void Main(string[] args)
6 {
7 CalculatorClient client = new CalculatorClient("secure");
8 double n1 = 5.6;
9 double n2 = 7.3;
10 double result;
11
12 result = client.Add(n2, n1);
13 Console.WriteLine("执行加法后的结果为:{0}", result.ToString());
14
15 result = client.Subtract(n2, n1);
16 Console.WriteLine("执行减法后的结果为:{0}", result.ToString());
17
18 result = client.Multiply(n1, n2);
19 Console.WriteLine("执行乘法后的结果为:{0}", result.ToString());
20
21 result = client.Divide(n1, n2);
22 Console.WriteLine("执行除法后的结果为:{0}", result.ToString());
23
24 //CalculatorSessionClient clientSeesion = new CalculatorSessionClient();
25 //string s = clientSeesion.test("你好我做一个测试!");
26 //string b = clientSeesion.GetServiceDescriptionInfo();
27 //Console.WriteLine(s);
28 //Console.WriteLine(b);
29
30 Test();
31
32 }
33
34 static void Test()
35 {
36 CalculatorSessionClient wcfClient = new CalculatorSessionClient();
37 try
38 {
39 using (OperationContextScope scope = new OperationContextScope(wcfClient.InnerChannel))
40 {
41 MessageHeader header
42 = MessageHeader.CreateHeader(
43 "Service-Bound-CustomHeader",
44 "http://Microsoft.WCF.Documentation",
45 "Custom Happy Value."
46 );
47 OperationContext.Current.OutgoingMessageHeaders.Add(header);
48
49 // Making calls.
50 Console.WriteLine("Enter the greeting to send: ");
51 string greeting = Console.ReadLine();
52
53 //Console.ReadLine();
54 header = MessageHeader.CreateHeader(
55 "Service-Bound-OneWayHeader",
56 "http://Microsoft.WCF.Documentation",
57 "Different Happy Value."
58 );
59 OperationContext.Current.OutgoingMessageHeaders.Add(header);//TODO:自定义传出消息头的用处?
60
61 // One-way
62 wcfClient.test(greeting);
63
64
65 // Done with service.
66 wcfClient.Close();
67 Console.WriteLine("Done!");
68 Console.ReadLine();
69 }
70 }
71 catch (TimeoutException timeProblem)
72 {
73 Console.WriteLine("The service operation timed out. " + timeProblem.Message);
74 Console.ReadLine();
75 wcfClient.Abort();
76 }
77 catch (CommunicationException commProblem)
78 {
79 Console.WriteLine("There was a communication problem. " + commProblem.Message);
80 Console.ReadLine();
81 wcfClient.Abort();
82 }
83
84 }
85 }
86 }

2.服务端代码:

 



1 namespace Microsoft.ServiceModel.Samples
2 {
3 class Program
4 {
5 static void Main(string[] args)
6 {
7 //创建一个ServiceHost
8 using (ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService)))
9 {
10 // Open the ServiceHost to create listeners
11 serviceHost.Open();
12 Console.WriteLine("服务已经开启!");
13 Console.WriteLine("按回车键结束服务!");
14 Console.WriteLine();
15 Console.ReadLine();
16
17 serviceHost.Close();
18
19 }
20 }
21
22 }
23 [ServiceContract]//定义服务协定完成
24 public interface ICalculator
25 {
26 [OperationContract]
27 double Add(double n1, double n2);
28 [OperationContract]
29 double Subtract(double n1, double n2);
30 [OperationContract]
31 double Multiply(double n1, double n2);
32 [OperationContract]
33 double Divide(double n1, double n2);
34 }
35
36 [ServiceContract]
37 public interface ICalculatorSession
38 {
39 [OperationContract]
40 string test(string s);
41
42 [OperationContract]
43 string GetServiceDescriptionInfo();
44 }
45
46 public class CalculatorService : ICalculator, ICalculatorSession
47 {
48 public double Add(double n1, double n2)
49 {
50 return n1 + n2;
51 }
52
53 public double Subtract(double n1, double n2)
54 {
55 return n1 - n2;
56 }
57
58 public double Multiply(double n1, double n2)
59 {
60 return n1 * n2;
61 }
62
63 public double Divide(double n1, double n2)
64 {
65 return n1 / n2;
66 }
67
68 public string test(string s)
69 {
70 Console.WriteLine("Service Said" + s);
71 WriteHeaders(OperationContext.Current.IncomingMessageHeaders);
72 return s;
73 }
74
75 public string GetServiceDescriptionInfo()
76 {
77 StringBuilder sb = new StringBuilder();
78 OperationContext operatiOnContext= OperationContext.Current;
79 ServiceHost serviceHost = (ServiceHost)operationContext.Host;
80 ServiceDescription dec = serviceHost.Description;
81 sb.Append("Base addresses:\n");
82 foreach (Uri url in serviceHost.BaseAddresses)
83 {
84 sb.Append(" " + url + "\n");
85 }
86 sb.Append("Service endpoint:\n");
87 foreach (ServiceEndpoint endPoint in dec.Endpoints)
88 {
89 sb.Append("Address:" + endPoint.Address + "\n");
90 sb.Append("Binding:" + endPoint.Binding + "\n");
91 sb.Append("Contract:" + endPoint.Contract + "\n");
92 }
93
94 return sb.ToString();
95 }
96
97 private void WriteHeaders(MessageHeaders headers)
98 {
99 foreach (MessageHeaderInfo header in headers)
100 {
101 Console.WriteLine("\t" + header.Actor);
102 Console.ForegroundColor = ConsoleColor.White;
103 Console.WriteLine("\t" + header.Name);
104 Console.ForegroundColor = ConsoleColor.Yellow;
105 Console.WriteLine("\t" + header.Namespace);
106 Console.WriteLine("\t" + header.Relay);
107 if (header.IsReferenceParameter == true)
108 {
109 Console.WriteLine("IsReferenceParameter header detected: " + header.ToString());
110 }
111 }
112 Console.ResetColor();
113 }
114 }
115
116 }

这个实例演示的是客户端通过OutgoingMessageHeaders添加了一些信息,然后服务端通过IncomingMessageHeaders

附:(>也许你会有疑问,事情都是OperationContext做的,要class="selflink">OperationContextScope 干什么:个人理解OperationContextScope 类似于数据库连接对象类,wcfClient.InnerChannel就好像是连接字符串,告诉要连接到哪去,整个class="selflink">OperationContextScope 块就像是数据库中的当期连接,信息头就好像是sql语句,出了class="selflink">OperationContextScope 块范围就好数据库断开了连接,进行其他操作要重新连接,>OperationContext 就好像是Command对象执行一些操作)

class="selflink">OperationContextScope 对象建立了当前操作上下文之后,可以使用 OperationContext 执行以下操作:



  • 访问和修改传入和传出消息头和其他属性。


  • 访问运行库,包括调度程序、主机、信道和扩展。


  • 访问其他类型的上下文,如安全、实例和请求上下文。


  • 访问与 OperationContext 对象关联的信道,或(如果信道实现System.ServiceModel.Channels.ISession)访问关联信道的会话标识符。


创建了 class="selflink">OperationContextScope 后,将存储当前的 OperationContext,并且新的 OperationContext 由Current 属性所返回。释放 class="selflink">OperationContextScope 后,将还原原始 OperationContext。

wcf之OperationContextScope,布布扣,bubuko.com


推荐阅读
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
    本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • 《数据结构》学习笔记3——串匹配算法性能评估
    本文主要讨论串匹配算法的性能评估,包括模式匹配、字符种类数量、算法复杂度等内容。通过借助C++中的头文件和库,可以实现对串的匹配操作。其中蛮力算法的复杂度为O(m*n),通过随机取出长度为m的子串作为模式P,在文本T中进行匹配,统计平均复杂度。对于成功和失败的匹配分别进行测试,分析其平均复杂度。详情请参考相关学习资源。 ... [详细]
  • 本文介绍了通过ABAP开发往外网发邮件的需求,并提供了配置和代码整理的资料。其中包括了配置SAP邮件服务器的步骤和ABAP写发送邮件代码的过程。通过RZ10配置参数和icm/server_port_1的设定,可以实现向Sap User和外部邮件发送邮件的功能。希望对需要的开发人员有帮助。摘要长度:184字。 ... [详细]
  • 动态规划算法的基本步骤及最长递增子序列问题详解
    本文详细介绍了动态规划算法的基本步骤,包括划分阶段、选择状态、决策和状态转移方程,并以最长递增子序列问题为例进行了详细解析。动态规划算法的有效性依赖于问题本身所具有的最优子结构性质和子问题重叠性质。通过将子问题的解保存在一个表中,在以后尽可能多地利用这些子问题的解,从而提高算法的效率。 ... [详细]
  • Java验证码——kaptcha的使用配置及样式
    本文介绍了如何使用kaptcha库来实现Java验证码的配置和样式设置,包括pom.xml的依赖配置和web.xml中servlet的配置。 ... [详细]
  • 高质量SQL书写的30条建议
    本文提供了30条关于优化SQL的建议,包括避免使用select *,使用具体字段,以及使用limit 1等。这些建议是基于实际开发经验总结出来的,旨在帮助读者优化SQL查询。 ... [详细]
  • 本文介绍了指针的概念以及在函数调用时使用指针作为参数的情况。指针存放的是变量的地址,通过指针可以修改指针所指的变量的值。然而,如果想要修改指针的指向,就需要使用指针的引用。文章还通过一个简单的示例代码解释了指针的引用的使用方法,并思考了在修改指针的指向后,取指针的输出结果。 ... [详细]
  • 在project.properties添加#Projecttarget.targetandroid-19android.library.reference.1..Sliding ... [详细]
  • 猜字母游戏
    猜字母游戏猜字母游戏——设计数据结构猜字母游戏——设计程序结构猜字母游戏——实现字母生成方法猜字母游戏——实现字母检测方法猜字母游戏——实现主方法1猜字母游戏——设计数据结构1.1 ... [详细]
  • CentOS 7部署KVM虚拟化环境之一架构介绍
    本文介绍了CentOS 7部署KVM虚拟化环境的架构,详细解释了虚拟化技术的概念和原理,包括全虚拟化和半虚拟化。同时介绍了虚拟机的概念和虚拟化软件的作用。 ... [详细]
  • 本文介绍了一种解析GRE报文长度的方法,通过分析GRE报文头中的标志位来计算报文长度。具体实现步骤包括获取GRE报文头指针、提取标志位、计算报文长度等。该方法可以帮助用户准确地获取GRE报文的长度信息。 ... [详细]
  • PDF内容编辑的两种小方法,你知道怎么操作吗?
    本文介绍了两种PDF内容编辑的方法:迅捷PDF编辑器和Adobe Acrobat DC。使用迅捷PDF编辑器,用户可以通过选择需要更改的文字内容并设置字体形式、大小和颜色来编辑PDF文件。而使用Adobe Acrobat DC,则可以通过在软件中点击编辑来编辑PDF文件。PDF文件的编辑可以帮助办公人员进行文件内容的修改和定制。 ... [详细]
author-avatar
mobiledu2502862343
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有