WCF学习笔记二:服务端Log

问题提出:

通过这几天对WCF的学习,准备使用WCF实现 服务端Log

参考资料:

服务站: 使用自定义行为扩展 WCF

总结WCF中截获消息的几种方式

总结WCF中截获消息的几种方式(二)

聚焦WCF行为的扩展

总结WCF中截获消息的几种方式中的几种方法进行分析。

1  路由截获法:

  根据总结WCF中截获消息的几种方式文中优缺点总结,不适合。

2  自定义Binding法:

  因为与Binding关联,使Binding的替换实现起来比较复杂,所以不打算使用。

3  实现IMessageInspector接口法:

  步骤1、编写MessageInspector类,实现IDispatchMessageInspector接口。

代码
public class MessageInspector : IDispatchMessageInspector
{
#region IDispatchMessageInspector Members

public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
{
Console.WriteLine(request.ToString());
return null;
}

public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
{

}

#endregion
}

  步骤2、将MessageInspector注入到EndpointDispatcher.DispatchRuntime.MessageInspectors中。

    有两种实现方法:

      a、编写MessageServiceBehavior类,实现IServiceBehavior接口。

代码
public class MessageServiceBehavior : IServiceBehavior
{
#region IServiceBehavior Members

public void AddBindingParameters(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{

}

public void ApplyDispatchBehavior(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
{
foreach (ChannelDispatcher cDispatcher in serviceHostBase.ChannelDispatchers)
foreach (EndpointDispatcher endpointDispatcher in cDispatcher.Endpoints)
endpointDispatcher.DispatchRuntime.MessageInspectors.Add(
new MessageInspector());

}

public void Validate(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
{

}

#endregion
}

      b、编写MessageEndpointBehavior类,实现IEndpointBehavior接口。

代码
public class MessageEndpointBehavior:IServiceBehavior, IEndpointBehavior
{
#region IServiceBehavior Members

public void AddBindingParameters(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
}

public void ApplyDispatchBehavior(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
{
foreach (ServiceEndpoint endpoint in serviceHostBase.Description.Endpoints)
{
endpoint.Behaviors.Add(
this);
}
}

public void Validate(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
{
}

#endregion

#region IEndpointBehavior Members

public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
}

public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
{
}

public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
{
endpointDispatcher.DispatchRuntime.MessageInspectors.Add(
new MessageInspector());
}

public void Validate(ServiceEndpoint endpoint)
{
}

#endregion
}

  其实结果是一样的:都是将MessageInspector添加到服务端每个EndpointDispatcher(Host.ChannelDispatchers.Endpoints中)的MessageInspectors(EndpointDispatcher.DispatchRuntime.MessageInspectors)中。

4  跟踪诊断法:

  自定义CustomTraceListener,继承自TraceListener抽象类。

代码
public class CustomTraceListener : TraceListener
{
public override void Write(string message)
{
Console.Write(message);
}

public override void WriteLine(string message)
{
Console.WriteLine(message);
}
}

  配置参考:配置消息日志记录

 

代码下载:https://files.cnblogs.com/xujiaoxiang/Fly_Wcf_MessageLog.zip

原文地址:https://www.cnblogs.com/xujiaoxiang/p/1750040.html