Tuesday, November 2, 2010

WCF or WSDL or SOAP Request and Response XML Messages

Guys !!!!!!!This is The best WCF custom Behavior for SOAP end point MessageVieweInspector


Using this we can test your WCF or WSDL or SOAP services in C#.NET 

Provides a custom behavior that allows a client to capture messages and log
 them or assist in debugging messages.





Steps1: Open VS2010/2008 select class file copy and paste following code 
Step 2: Add your  WCF OR WSDL service into your project 





namespace WsdlTest
{
    // <summary>
    /// Provides a custom behavior that allows a client to capture messages and log
    /// them or assist in debugging messages.
    /// </summary>
    public class MessageViewerInspector : IEndpointBehavior, IClientMessageInspector
    {


        #region Properties
        public string RequestMessage { get; set; }
        public string ResponseMessage { get; set; }
        #endregion


        #region IEndpointBehavior Members
        public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
        {


        }


        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
            // adds our inspector to the runtime
            clientRuntime.MessageInspectors.Add(this);
        }


        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
        {


        }


        public void Validate(ServiceEndpoint endpoint)
        {


        }
        #endregion


        #region IClientMessageInspector Members
        void IClientMessageInspector.AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
        {
            this.ResponseMessage = reply.ToString();
        }


        object IClientMessageInspector.BeforeSendRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel)
        {
            this.RequestMessage = request.ToString();
            return null;
        }
        #endregion
    }
}


Step 3:Addd following code client side then test your Request and response 


//Create Proxy refrence
var proxy = new ServiceReference2.CustomerHelperClient();
//Create refrence your class
MessageViewerInspector inspector = new MessageViewerInspector();
//Add end point Behavior
proxy.Endpoint.Behaviors.Add(inspector); 
//Display into multi textbox or list box or Save into xml file for implimentation for ur feature 

string request = inspector.RequestMessage;
string response= inspector.ResponseMessage;



No comments:

Post a Comment