2013-05-08 83 views
0

是否可以通過某種方式將標題信息(或查詢字符串)添加到wcf請求中? 我一直在瞎搞,像這樣的IWcfPolicy了一下:IWcfPolicy - 動態地添加郵件標題

var xmlObjectSerializer = new DataContractSerializer(typeof(string)); 

    var addressHeader = AddressHeader.CreateAddressHeader("client", "http://tempuri.org/", "someValue", xmlObjectSerializer); 

    var endpointAddress = new EndpointAddress(new Uri(url), new AddressHeader[] { addressHeader }); 

    invocation.ChannelHolder.ChannelFactory.Endpoint.Address = endpointAddress; 

    invocation.Proceed(); 

這不然而工作。任何幫助都會非常困難。

回答

1

確定,所以這裏是如何做到這一點:

using (var scope = new OperationContextScope((IContextChannel) (invocation.ChannelHolder.Channel))) 
       { 
        OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = new HttpRequestMessageProperty 
         () 
         { 
          Headers = 
           { 
            {"client", LicenseManager.Instance.GetCurrentLicense().LicenseKey} 
           } 
         }; 

        invocation.Proceed(); 
       } 

這個代碼進入應用IWcfPolicy實現的方法。 因爲此帖發現解決方案:how to add a custom header to every wcf call