Мне удалось зарегистрировать сообщения, используя IClientMessageInspector
, прикрепленное через настраиваемое поведение. Есть некоторая документация для инспекторов сообщений на MSDN , но она все еще неясна (и несколько устарела, netstandard-2.0
Поверхность API немного отличается.
Мне пришлось перейти от использования ChannelFactory<T>
к использованию фактического сгенерированного прокси-класса. Рабочий код (сокращенно для краткости):
var clientChannel = new GeneratedProxyClient(
new BasicHttpBinding { SendTimeout = TimeSpan.FromMilliseconds(timeout) },
new EndpointAddress(new Uri("http://actual-service-address"));
clientChannel.Endpoint.EndpointBehaviors.Add(new LoggingBehaviour());
Классы обслуживания:
// needed to bind the inspector to the client channel
// other methods are empty
internal class LoggingBehaviour : IEndpointBehavior
{
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
clientRuntime.ClientMessageInspectors.Add(new LoggingClientMessageInspector());
}
}
internal class LoggingClientMessageInspector : IClientMessageInspector
{
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
var correlationId = Guid.NewGuid();
this.SaveLog(ref request, correlationId, "RQ");
return correlationId;
}
public void AfterReceiveReply(ref Message reply, object correlationState)
{
var correlationId = (Guid)correlationState;
this.SaveLog(ref reply, correlationId, "RS");
}
private void SaveLog(ref Message request, Guid correlationId, string suffix)
{
var outputPath = GetSavePath(suffix, correlationId, someOtherData);
using (var buffer = request.CreateBufferedCopy(int.MaxValue))
{
var directoryName = Path.GetDirectoryName(outputPath);
if (directoryName != null)
{
Directory.CreateDirectory(directoryName);
}
using (var stream = File.OpenWrite(outputPath))
{
using (var message = buffer.CreateMessage())
{
using (var writer = XmlWriter.Create(stream))
{
message.WriteMessage(writer);
}
}
}
request = buffer.CreateMessage();
}
}
}