Where are Endpoint.Behaviors and clientRuntime.MessageInspectors in .Net 5.0?

Viewed 974

With all version of .Net until the 4.8, it was possible to easily catch all ingoing and outgoing traffic for SOAP services consumed with WCF for logging purpose, using IEndpointBehavior / IClientMessageInspector, as described here for example : https://docs.microsoft.com/en-us/dotnet/api/system.servicemodel.dispatcher.iclientmessageinspector.beforesendrequest?view=netframework-4.8

I would like to know how I can restore this, or achieve the same purpose as before in .Net 5.0. I need to trace all SOAP exchanges in a log file, previously I just have to do :

public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
{
    _logger.Debug("Reply received: " + Environment.NewLine + "{0}", reply.ToString());
}
public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel)
{
    _logger.Debug("Sending request: " + Environment.NewLine + "{0}", request.ToString());
    return null;
}

To have it working, you need clientRuntime.MessageInspectors, and client.Endpoint.Behaviors to register the classes.

Despite I have registered nuget packages available for System.ServiceModel, and especially System.ServiceModel.Primitives.dll these properties seems to not exist anymore.

1 Answers

Actually, these features are still existing and this seem to work like previously. The properties have just slightly changed.

client.Endpoint.Behaviors.Add(new LoggerEndpointBehavior(_logger)); became client.Endpoint.EndpointBehaviors.Add(new LoggerEndpointBehavior(_logger));

And

clientRuntime.MessageInspectors.Add(new LoggerClientMessageLoggerInspector(_logger)); became clientRuntime.ClientMessageInspectors.Add(new LoggerClientMessageLoggerInspector(_logger));

Everything else is like in the example in the first link. I'm quite surprised to not have seen these suggestions by Intellisense when I was migrating, but it works now !

Related