2011-08-22 126 views
3

我有一個自定義頭存儲在一個「字符串」變量,我需要從我的WCF客戶端與「字符串」類型變量頭中的傳出SOAP請求頭。根據研究,我發現實施MessageHeaders.WriteHeaderContents可以工作,但此方法只接受XmlDictionaryWriterXmlWriter類型作爲輸入。我有一個字符串輸入。如何在C#中編寫代碼...需要示例實現MessageHeaders.WriteHeaderContents

回答

4

消息頭是一個SOAP概念,SOAP請求是XML文檔,所以您真的需要一些XML-ness。但對於您的場景,實際上並不需要覆蓋MessageHeaders,您可以使用檢查器並簡單替換此處的標題,如下面的示例所示(轉換爲XML將由MessageHeader類完成)。

public class StackOverflow_7141998 
{ 
    [MessageContract] 
    public class MyMC 
    { 
     [MessageHeader(Name = "MyHeader", Namespace = "http://my.namespace.com")] 
     public string HeaderValue { get; set; } 
     [MessageBodyMember(Name = "MyBody", Namespace = "http://my.namespace.com")] 
     public string BodyValue { get; set; } 
    } 
    [ServiceContract] 
    public interface ITest 
    { 
     [OperationContract] 
     void Process(MyMC mc); 
    } 
    public class Service : ITest 
    { 
     public void Process(MyMC mc) 
     { 
      Console.WriteLine("Header value: {0}", mc.HeaderValue); 
     } 
    } 
    public class MyInspector : IEndpointBehavior, IClientMessageInspector 
    { 
     public string NewHeaderValue { get; set; } 

     public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters) 
     { 
     } 

     public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime) 
     { 
      clientRuntime.MessageInspectors.Add(this); 
     } 

     public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) 
     { 
     } 

     public void Validate(ServiceEndpoint endpoint) 
     { 
     } 

     public void AfterReceiveReply(ref Message reply, object correlationState) 
     { 
     } 

     public object BeforeSendRequest(ref Message request, IClientChannel channel) 
     { 
      int originalIndex = request.Headers.FindHeader("MyHeader", "http://my.namespace.com"); 
      if (originalIndex >= 0) 
      { 
       request.Headers.Insert(originalIndex, MessageHeader.CreateHeader("MyHeader", "http://my.namespace.com", this.NewHeaderValue)); 
       request.Headers.RemoveAt(originalIndex + 1); 
      } 

      return null; 
     } 
    } 
    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 WSHttpBinding(), ""); 
     host.Open(); 
     Console.WriteLine("Host opened"); 

     ChannelFactory<ITest> factory = new ChannelFactory<ITest>(new WSHttpBinding(), new EndpointAddress(baseAddress)); 
     MyInspector inspector = new MyInspector { NewHeaderValue = "Modified header value" }; 
     factory.Endpoint.Behaviors.Add(inspector); 
     ITest proxy = factory.CreateChannel(); 

     proxy.Process(new MyMC { HeaderValue = "Original header value", BodyValue = "The body" }); 

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

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