ASP.NET Core中的WCF跟踪 [英] WCF Tracing in ASP.NET Core

查看:62
本文介绍了ASP.NET Core中的WCF跟踪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们曾经使用过ASP.NET上的WCF,最近切换到了ASP.NET Core上的WCF.这很难实现,因为ASP.Net Core不支持WCF.一方面,整个web.config XML配置模型已转储到ASP.NET Core中,因此我们无法在此处配置WCF跟踪.

We used to use WCF over ASP.NET and recently switched to WCF over ASP.NET Core. This was quite hard to implement because ASP.Net Core doesn't support WCF out of the box. For one thing, the whole web.config XML configuration model has been dumped in ASP.NET Core so we cannot configure WCF tracing there.

即该文档是无用的: https://docs.microsoft.com/en-us/dotnet/framework/wcf/diagnostics/tracing/configuring-tracing

我们不得不通过在WCF和端口80之间放置一个http代理来入侵ASP.NETCore.WCF实际上在另一个端口上运行.

We had to hack ASP.NET Core by putting a http proxy between WCF and port 80. WCF is actually running on another port.

问题是,如果ASP.NET Core不关注web.config,如何启用WCF跟踪?

The question is, how do we enable WCF tracing if ASP.NET Core doesn't pay attention to the web.config?

推荐答案

在客户端跟踪的情况下,我将自定义端点行为( IEndpointBehavior )与自定义消息日志记录检查器( IClientMessageInspector )以获取输入和输出消息.

In case of client side tracing I used custom endpoint behaviour (IEndpointBehavior) with custom message logging inspector (IClientMessageInspector) to get input and output messages.

客户端初始化:

_serviceClient = new MyCustomServiceClient();
_serviceClient.Endpoint.Address = new System.ServiceModel.EndpointAddress(_configParams.ServiceUri);
_serviceClient.Endpoint.EndpointBehaviors.Add(new EndpointLoggingBehavior("MyCustomService"));

EndpointLogging行为的实现:

public class EndpointLoggingBehavior : IEndpointBehavior
    {
        public EndpointLoggingBehavior(string serviceName)
        {
            _serviceName = serviceName;
        }

        private readonly string _serviceName;

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

        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
            clientRuntime.ClientMessageInspectors.Add(new MessageLoggingInspector(_serviceName));
        }

        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
        {
        }

        public void Validate(ServiceEndpoint endpoint)
        {
        }
    }

MessageLoggingInspector 的实现:

public class MessageLoggingInspector : IClientMessageInspector
    {
        private readonly string _serviceName;
        public MessageLoggingInspector(string serviceName)
        {
            _serviceName = serviceName;
        }
        public void AfterReceiveReply(ref Message reply, object correlationState)
        {
            // copying message to buffer to avoid accidental corruption
            var buffer = reply.CreateBufferedCopy(int.MaxValue);
            reply = buffer.CreateMessage();
            // creating copy
            var copy = buffer.CreateMessage();
            //getting full input message
            var fullInputMessage = copy.ToString();

        }
        public object BeforeSendRequest(ref Message request, IClientChannel channel)
        {
            // copying message to buffer to avoid accidental corruption
            var buffer = request.CreateBufferedCopy(int.MaxValue);
            request = buffer.CreateMessage();
            // creating copy
            var copy = buffer.CreateMessage();
            //getting full output message
            var fullOutputMessage = copy.ToString();
            return null;
        }
    }

然后,当然,您需要将这些消息写入任何存储.

Then, of course, you will need to write these messages to any storage.

这篇关于ASP.NET Core中的WCF跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆