Das Lesen von SOAP Message Headern für jede Art von Nachrichten können für die Problemdiagnose hilfreich sein. Der folgende Code ermöglicht dieses.

Um den Code zum Laufen zu bringen führe folgende Schritte aus:

  1. Copy und paste den Code ins Visual Studio 2010.
  2. Füge eine Referenz zu folgenden Assemblies hinzu:
    1. System.ServiceModel.dll
    2. System.Runtime.Serialization.dll.

Original Post: http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/fa3f4c55-5835-43f2-892f-737b4bccb598

using System; 
using System.Collections.Generic; 
using System.ServiceModel; 
using System.ServiceModel.Description; 
using System.ServiceModel.Dispatcher; 
    
public class MessageInspectorSample 
    static void Main() 
    
        // This runs the whole show! 
        Run();         
    
    
    [ServiceContract] // A very simple service contract. Echo and Add. 
    public interface ITest 
    
        [OperationContract] 
        string Echo(string text); 
        [OperationContract] 
        int Add(int x, int y); 
    
    
    public class Service : ITest 
    
            
        public string Echo(string text) 
        
            return text; 
        
    
        public int Add(int x, int y) 
        
            return x + y; 
        
    
    
    public class MyInspector : IClientMessageInspector, IEndpointBehavior 
    
           
        // These lists store the messages for later retrieval. 
        public List<string> sentMessages = new List<string>(); 
        public List<string> receivedMessages = new List<string>(); 
             
        #region IClientMessageInspector Members 
        public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState) 
        
            // Save the reply to the list.  
            receivedMessages.Add(reply.ToString()); 
        
    
        public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel) 
        
            // Save the request to the list. 
            sentMessages.Add(request.ToString()); 
            return null
        
    
        #endregion 
    
        #region IEndpointBehavior Members 
        public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters) 
        {} 
    
        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime) 
        
            // Add this implementation to the inspectors. 
            clientRuntime.MessageInspectors.Add(this); 
        
    
        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) 
        {        } 
    
        public void Validate(ServiceEndpoint endpoint) 
        {        } 
        #endregion 
    
    
    public static void Run() 
    
        string baseAddress = "http://" + Environment.MachineName + ":8000/Service"
        ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress)); 
        ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), ""); 
        host.Open(); 
        Console.WriteLine("Host opened at " + baseAddress); 
    
        MyInspector inspector = new MyInspector(); 
        ChannelFactory<ITest> factory = new ChannelFactory<ITest>(new BasicHttpBinding(), new EndpointAddress(baseAddress)); 
        factory.Endpoint.Behaviors.Add(inspector); 
        ITest proxy = factory.CreateChannel(); 
        proxy.Echo("Hello world"); 
        proxy.Add(123, 456); 
        ((IClientChannel)proxy).Close(); 
        factory.Close(); 
        host.Close(); 
        Console.WriteLine("Sent messages:"); 
        foreach (string sent in inspector.sentMessages) 
        
            Console.WriteLine(sent); 
            Console.WriteLine(); 
        
    
        Console.WriteLine("*******************************************"); 
    
        Console.WriteLine("Received messages:"); 
    
        foreach (string received in inspector.receivedMessages) 
        
            Console.WriteLine(received); 
            Console.WriteLine(); 
        
        Console.ReadLine(); 
    
}

+++++++++++++++++++++++++++++++++++++

Um Komplexität hinzuzufügen, benutze ein anderes Binding, wi WSHttpBinding, das andere Sicherheitsanforderungen bietet.


Siehe auch


Andere Sprachen

Dieser Artikel ist auch in den folgenden Sprachen verfügbar:

English (en-US)