通过OperationContext添加消息头信息
通过往WCF消息头中添加自定义信息,可以用于各种用途,比如可以用于传递AuthKey来判断调用是否合法。
客户端:
using (OperationContextScope scope = new OperationContextScope(iContextChannel))
{
MessageHeader<string> mh = new MessageHeader<string>("abcde");
MessageHeader header = mh.GetUntypedHeader("AuthKey", http://www.cjb.com/);
OperationContext.Current.OutgoingMessageHeaders.Add(header);
return func();
}
服务端:{
MessageHeader<string> mh = new MessageHeader<string>("abcde");
MessageHeader header = mh.GetUntypedHeader("AuthKey", http://www.cjb.com/);
OperationContext.Current.OutgoingMessageHeaders.Add(header);
return func();
}
string authKey = string.Empty;
if (OperationContext.Current != null)
{
authKey = OperationContext.Current.IncomingMessageHeaders.GetHeader<string>("AuthKey", http://www.cjb.com);
}
if (OperationContext.Current != null)
{
authKey = OperationContext.Current.IncomingMessageHeaders.GetHeader<string>("AuthKey", http://www.cjb.com);
}