使用網上的例子
使用WCF頭資訊可以用於在調用Service的同時傳遞Header中繼資料
用戶端:
ChannelFactory<IOrderProcessor> channelFactory = new ChannelFactory<IOrderProcessor>("defaultEndpoint");
IOrderProcessor orderProcessor = channelFactory.CreateChannel();
//使用通道建立Scope
using (OperationContextScope scope = new OperationContextScope(orderProcessor as IContextChannel))
{
MessageHeader<string> mh = new MessageHeader<string>("abcde");
MessageHeader header = mh.GetUntypedHeader("AuthKey", http://www.cjb.com/);//設定名稱和命名空間
OperationContext.Current.OutgoingMessageHeaders.Add(header);
orderProcessor.Function();//調用服務時發送的資料Header為在該OperationContextScope中設定的OperationContext
}
服務端: //OperationContext.Current 返回當前建立串連的OperationContext
if (OperationContext.Current != null)
{
authKey = OperationContext.Current.IncomingMessageHeaders.GetHeader<string>("AuthKey", http://www.cjb.com);
}
使用網上的例子
使用WCF頭資訊可以用於在調用Service的同時傳遞Header中繼資料
用戶端:
ChannelFactory<IOrderProcessor> channelFactory = new ChannelFactory<IOrderProcessor>("defaultEndpoint");
IOrderProcessor orderProcessor = channelFactory.CreateChannel();
//使用通道建立Scope
using (OperationContextScope scope = new OperationContextScope(orderProcessor as IContextChannel))
{
MessageHeader<string> mh = new MessageHeader<string>("abcde");
MessageHeader header = mh.GetUntypedHeader("AuthKey", http://www.cjb.com/);//設定名稱和命名空間
OperationContext.Current.OutgoingMessageHeaders.Add(header);
orderProcessor.Function();//調用服務時發送的資料Header為在該OperationContextScope中設定的OperationContext
}
服務端: //OperationContext.Current 返回當前建立串連的OperationContext
if (OperationContext.Current != null)
{
authKey = OperationContext.Current.IncomingMessageHeaders.GetHeader<string>("AuthKey", http://www.cjb.com);
}