2012-01-13 53 views
2

在我的客戶端調用服務之前,我發送到wcf服務之前添加一個令牌(guid)到請求。我已經實現了AfterReceiveRequest方法(來自IDispatchMessageInspector),並且希望驗證請求是否有效?

AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext) 

在此方法中,我想驗證請求頭是否包含客戶端中添加的令牌(guid)。所以我用這裏的這部分代碼:

Guid securityTokenId = request.Headers.GetHeader<Guid>("Token", "System"); 

如果在任何情況下,這個「令牌」的標題將我securityTokenIdGuid.Empty沒有找到?或者它返回了什麼?

回答

3

如果沒有包含該名稱/名稱空間的標頭,則在調用GetHeader時會發生異常(MessageHeaderException)。如果你不想處理異常,你可以先看看這個頭是否存在(使用FindHeader方法),那麼如果它返回一個非負值,就得到相應的頭。

public class StackOverflow_8854137 
{ 
    [ServiceContract] 
    public interface ITest 
    { 
     [OperationContract] 
     string Echo(string text); 
    } 
    public class Service : ITest 
    { 
     public string Echo(string text) 
     { 
      return text; 
     } 
    } 
    class MyBehavior : IEndpointBehavior, IDispatchMessageInspector 
    { 
     public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters) 
     { 
     } 

     public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime) 
     { 
     } 

     public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) 
     { 
      endpointDispatcher.DispatchRuntime.MessageInspectors.Add(this); 
     } 

     public void Validate(ServiceEndpoint endpoint) 
     { 
     } 

     public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext) 
     { 
      try 
      { 
       Guid tokenId = request.Headers.GetHeader<Guid>("Token", "System"); 
       Console.WriteLine("Token: {0}", tokenId); 
      } 
      catch (Exception e) 
      { 
       Console.WriteLine("{0}: {1}", e.GetType().FullName, e.Message); 
      } 

      return null; 
     } 

     public void BeforeSendReply(ref Message reply, object correlationState) 
     { 
     } 
    } 
    public static void Test() 
    { 
     string baseAddress = "http://" + Environment.MachineName + ":8000/Service"; 
     ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress)); 
     ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), ""); 
     endpoint.Behaviors.Add(new MyBehavior()); 
     host.Open(); 
     Console.WriteLine("Host opened"); 

     ChannelFactory<ITest> factory = new ChannelFactory<ITest>(new BasicHttpBinding(), new EndpointAddress(baseAddress)); 
     ITest proxy = factory.CreateChannel(); 
     Console.WriteLine(proxy.Echo("No token")); 

     using (new OperationContextScope((IContextChannel)proxy)) 
     { 
      OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader("Token", "System", Guid.NewGuid())); 
      Console.WriteLine(proxy.Echo("With token")); 
     } 

     ((IClientChannel)proxy).Close(); 
     factory.Close(); 

     Console.Write("Press ENTER to close the host"); 
     Console.ReadLine(); 
     host.Close(); 
    } 
} 
+0

是否實施IDisptachMessageInspector和端點行爲的變化對所有類型,如HTTP,TCP或綁定了NamedPipes支持的? – user1086046 2012-01-16 15:40:33

+0

卡洛斯,你可以請發佈web.config爲您的文章http://blogs.msdn.com/b/carlosfigueira/archive/2011/04/19/wcf-extensibility-message-inspectors.aspx 這是非常有益的。 – user1086046 2012-01-16 16:05:15

+0

Re:第一個問題:消息檢查員工作不管綁定。第二,該文章中的代碼是自託管的,並且所有服務設置均以代碼完成。我實際上(個人)不喜歡使用web.config,你可以在http://blogs.msdn.com/b/carlosfigueira/archive/2011/06/14/wcf-extensibility-servicehostfactory.aspx上看到更多關於它的信息。 。 – carlosfigueira 2012-01-17 03:50:53

0
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext) 
    { 
     var i = request.Headers.FindHeader(TOKEN_HEADER_NAME, TOKEN_HEADER_NAMESPACE); 
     string token; 
     if (i >= 0) 
     { 
      token = request.Headers.GetHeader<string>(i); 
     }else 
     { 
      token = Guid.NewGuid().ToString(); 
      request.Headers.Add(MessageHeader.CreateHeader(TOKEN_HEADER_NAME, TOKEN_HEADER_NAMESPACE, token)); 
     } 

     return token; 
    } 
相關問題