IClientMessageInspector.BeforeSendRequest Method
.NET Framework 4
Enables inspection or modification of a message before a request message is sent to a service.
Assembly: System.ServiceModel (in System.ServiceModel.dll)
Parameters
- request
- Type: System.ServiceModel.Channels.Message%
The message to be sent to the service.
- channel
- Type: System.ServiceModel.IClientChannel
The client object channel.
Return Value
Type: System.ObjectThe object that is returned as the correlationState argument of the AfterReceiveReply method. This is null if no correlation state is used.
The best practice is to make this a System.Guid to ensure that no two correlationState objects are the same.
The following code example shows an implementation that writes strings to the console when the implementation is called.
#region IClientMessageInspector Members public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState) { Console.WriteLine("IClientMessageInspector.AfterReceiveReply called."); Console.WriteLine("Message: {0}", reply.ToString()); } public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel) { Console.WriteLine("IClientMessageInspector.BeforeSendRequest called."); return null; }
The following code example shows how to use an System.ServiceModel.Description.IEndpointBehavior to insert the client message inspector in the client endpoint.
#region IEndpointBehavior Members public void AddBindingParameters( ServiceEndpoint endpoint, BindingParameterCollection bindingParameters ) { return; } public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime) { clientRuntime.MessageInspectors.Add(new Inspector()); foreach (ClientOperation op in clientRuntime.Operations) op.ParameterInspectors.Add(new Inspector()); } public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) { endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new Inspector()); foreach (DispatchOperation op in endpointDispatcher.DispatchRuntime.Operations) op.ParameterInspectors.Add(new Inspector()); } public void Validate(ServiceEndpoint endpoint){ return; }
Finally, the following code example shows how to modify the client configuration file to use the endpoint behavior with a particular endpoint.
<client>
<endpoint
address="http://localhost:8080/SampleService"
behaviorConfiguration="clientInspectorsAdded"
binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_ISampleService"
contract="ISampleService"
name="WSHttpBinding_ISampleService"
>
</endpoint>
</client>
<behaviors>
<endpointBehaviors>
<behavior name="clientInspectorsAdded">
<clientInterceptors />
</behavior>
</endpointBehaviors>
</behaviors>
<extensions>
<behaviorExtensions>
<add
name="clientInterceptors"
type="Microsoft.WCF.Documentation.InspectorInserter, HostApplication, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"
/>
</behaviorExtensions>
</extensions>
<client>
<endpoint
address="http://localhost:8080/SampleService"
behaviorConfiguration="clientInspectorsAdded"
binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_ISampleService"
contract="ISampleService"
name="WSHttpBinding_ISampleService"
>
</endpoint>
</client>
<behaviors>
<endpointBehaviors>
<behavior name="clientInspectorsAdded">
<clientInterceptors />
</behavior>
</endpointBehaviors>
</behaviors>
<extensions>
<behaviorExtensions>
<add
name="clientInterceptors"
type="Microsoft.WCF.Documentation.InspectorInserter, HostApplication, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"
/>
</behaviorExtensions>
</extensions>
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.