104 lines
3.1 KiB
C#
104 lines
3.1 KiB
C#
using System;
|
|
using System.Collections.ObjectModel;
|
|
using System.Diagnostics;
|
|
using System.ServiceModel;
|
|
using System.ServiceModel.Channels;
|
|
using System.ServiceModel.Configuration;
|
|
using System.ServiceModel.Description;
|
|
using System.ServiceModel.Dispatcher;
|
|
|
|
namespace Kreta.BusinessLogic.Classes
|
|
{
|
|
public class WcfMessageLogger : IDispatchMessageInspector, IClientMessageInspector, IServiceBehavior, IEndpointBehavior
|
|
{
|
|
#region IDispatchMessageInspector
|
|
|
|
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
|
|
{
|
|
Trace.WriteLine($"WCF request:{Environment.NewLine}{request}");
|
|
return null;
|
|
}
|
|
|
|
public void BeforeSendReply(ref Message reply, object correlationState)
|
|
{
|
|
Trace.WriteLine($"WCF response:{Environment.NewLine}{reply}");
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region IClientMessageInspector
|
|
|
|
public object BeforeSendRequest(ref Message request, IClientChannel channel)
|
|
{
|
|
Trace.WriteLine($"WCF request:{Environment.NewLine}{request}");
|
|
return null;
|
|
}
|
|
|
|
public void AfterReceiveReply(ref Message reply, object correlationState)
|
|
{
|
|
Trace.WriteLine($"WCF response:{Environment.NewLine}{reply}");
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region IServiceBehavior
|
|
|
|
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
|
|
{
|
|
foreach (ChannelDispatcher dispatcher in serviceHostBase.ChannelDispatchers)
|
|
{
|
|
foreach (var endpoint in dispatcher.Endpoints)
|
|
{
|
|
endpoint.DispatchRuntime.MessageInspectors.Add(new WcfMessageLogger());
|
|
}
|
|
}
|
|
}
|
|
|
|
public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
|
|
{
|
|
}
|
|
|
|
public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
|
|
{
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region IEndpointBehavior
|
|
|
|
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
|
|
{
|
|
clientRuntime.ClientMessageInspectors.Add(new WcfMessageLogger());
|
|
}
|
|
|
|
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
|
|
{
|
|
}
|
|
|
|
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
|
|
{
|
|
}
|
|
|
|
public void Validate(ServiceEndpoint endpoint)
|
|
{
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
|
|
public class WcfMessageLoggerExtension : BehaviorExtensionElement
|
|
{
|
|
protected override object CreateBehavior()
|
|
{
|
|
return new WcfMessageLogger();
|
|
}
|
|
|
|
public override Type BehaviorType
|
|
{
|
|
get
|
|
{
|
|
return typeof(WcfMessageLogger);
|
|
}
|
|
}
|
|
}
|
|
}
|