Cercar en aquest blog

8/1/13

WCF: Modificar el comportament del client del servei afegint capçaleres

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using System.ServiceModel;

namespace Test
{
    public class WcfClientInspector : IClientMessageInspector, IEndpointBehavior
    {

        public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
        {
            
        }

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

        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
        {
            
        }

        public void Validate(ServiceEndpoint endpoint)
        {
            
        }

        public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
        {
            
        }

        public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
        {
            MessageHeader header = new MessageHeader("Consoletest");
            request.Headers.Add(header.GetUntypedHeader("ApplicationName", Constants.Namespace));
            MessageHeader header1 = new MessageHeader("MyPC");
            request.Headers.Add(header1.GetUntypedHeader("ApplicationClientComputer", Constants.Namespace));
            MessageHeader header2 = new MessageHeader("ROLUser");
            request.Headers.Add(header2.GetUntypedHeader("ApplicationUserRol", Constants.Namespace));
            return null;
        }
    }
}

...



_factory.Endpoint.Behaviors.Add(endp)