ASP.NET Core中的WCF跟踪

编程入门 行业动态 更新时间:2024-10-25 00:25:51
本文介绍了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.

即该文档是无用的: docs.microsoft/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跟踪

本文发布于:2023-11-03 14:49:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555400.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:NET   ASP   WCF   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!