传说中的WCF(4):发送和接收SOAP头

时间:2022-11-17 15:52:45

如果你实在不明白Header是个啥玩意儿,你就想一想你发送电子邮件时,是不是有个叫“附件”的东东?对啊,那么SOAP头是不是可以理解为一种附加信息?就是附加到消息正文的内容。

消息正文又是啥?WCF除了流模式传输数据外,剩下的基本来说就是消息模式。我们不妨这样理解,WCF的服务器端和客户端是通过消息来交互的,就像 我们之间在发短信一样,我发给你,你可以回复我,这叫“双工”,不好读吧,叫双向好了;你心情不好的时候,可以不回我短信,这叫“单工”,还是不好听,叫 单向吧。

对于“消息”,更NB一点的理解就是:客户端每调用一次服务器方法,就是向服务器发送一条消息。嗯,这个理解较为直观,是吧?先不管它专业不专业,能弄懂就是王道。

既然消息头是附加信息,那有啥用呢?你可别说,有时候还真有不少用处。举个例子,WCF的身份验证是不是很麻烦?还要颁发什么证书的(当然不是荣誉 证书),如果只是验证一个客户端的身份,如用户名什么的,那么,在调用服务方法时,动态加入一些消息头,到了服务器端就获取并验证消息头。这样一来,是不 是也实现身份验证?

呵呵,这样做自然没有安装证书并加密消息那么安全,不过,对于一般的使用场合,也足矣。

那,如何传递消息头?当然是客户端发送,服务器端接收的情况较多了。操作方法和技巧也没什么神奇之处,所以,发果你记忆力好,你不妨把这些代码背下来。呵呵,开个玩笑。

首先,实现服务器端,在OperationContract方法中通过OperationContext.Current.IncomingMessageHeaders就能得到从客户端收到的消息头了,记得引入System.ServiceModel命名空间。

看看我写的东东。

[using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Channels; namespace Server
{
[ServiceContract]
public interface IService
{
[OperationContract]
void TestMethod();
} public class MyService : IService
{
public void TestMethod()
{
int index = OperationContext.Current.IncomingMessageHeaders.FindHeader("header", "http://my");
if (index != -1)
{
string hd = OperationContext.Current.IncomingMessageHeaders.GetHeader<string>(index);
Console.WriteLine("收到的标头:{0}", hd);
}
}
} class Program
{
static void Main(string[] args)
{
Uri baseURI = new Uri("http://localhost:7999/service");
using (ServiceHost host = new ServiceHost(typeof(MyService), baseURI))
{
BasicHttpBinding binding = new BasicHttpBinding();
binding.Security.Mode = BasicHttpSecurityMode.None;
host.AddServiceEndpoint(typeof(IService), binding, "http://localhost:9000/testsv");
ServiceMetadataBehavior behavior = new ServiceMetadataBehavior()
{
HttpGetEnabled = true
};
host.Description.Behaviors.Add(behavior); host.Opened += new EventHandler(delegate(object sender, EventArgs e)
{
Console.WriteLine("服务已经启动。");
}); // 启动服务
try
{
host.Open();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
} Console.ReadKey();
host.Close();
}
}
}
}

OperationContext.Current.IncomingMessageHeaders.FindHeader方法可以用来查找消息头,

方法调用后返回一个索引,从0开始的,你懂的,像数组一样。

得到索引后,再通过 OperationContext.Current.IncomingMessageHeaders.GetHeader<string>返回对应消息头的值。注意,头名称和命名空间要和在客户端中插入的消息一致,不然,恭喜你找不到Header。

至于那个T嘛,好理解,你在客户端中插入消息头时,Value用了什么类型,这个T就最好对应着,你想它返回字符串,T就为string,想让它返回整型,T就为int了。

OK,在客户端添加,服务引用,注意看代码,如何插入消息头。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels; namespace Client
{
class Program
{
static void Main(string[] args)
{
WS.ServiceClient myclient = new WS.ServiceClient();
using (OperationContextScope scope=new OperationContextScope(myclient.InnerChannel))
{
MessageHeader myHeader = MessageHeader.CreateHeader(
"header", "http://my", "你好,这是消息头。");
OperationContext.Current.OutgoingMessageHeaders.Add(myHeader); // 调用方法
myclient.TestMethod();
Console.WriteLine("服务方法已调用。");
}
Console.ReadKey();
}
}
}

好了,差不多了,这时候,可以测一测了,先运行服务器端,再运行客户端。

传说中的WCF(4):发送和接收SOAP头