Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF Tracing in ASP.NET Core

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.

I.e. this document is useless: https://learn.microsoft.com/en-us/dotnet/framework/wcf/diagnostics/tracing/configuring-tracing

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

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

like image 494
Christian Findlay Avatar asked Aug 30 '18 06:08

Christian Findlay


People also ask

What is WCF tracing?

Windows Communication Foundation (WCF) uses the tracing mechanism defined in the System.Diagnostics namespace. In this tracing model, trace data is produced by trace sources that applications implement. Each source is identified by a name.

Is WCF supported in .NET Core?

NET Core and . NET 5 support calling WCF services, but won't offer server-side support for hosting WCF. There are two recommended paths for modernizing WCF apps: gRPC is built on modern technologies and has emerged as the most popular choice across the developer community for RPC apps.

How do I turn off tracing in WCF?

If you want to disable the trace source, you should use the logMessagesAtServiceLevel , logMalformedMessages , and logMessagesAtTransportLevel attributes of the messageLogging element instead. You should set all these attributes to false.

How to use WCF services in NET Core?

To use WCF services in .NET Core, you need to create a proxy client of the required service. Proxy is actually a contract equivalent of actual service and contains complete details of the Interface and method exposed by WCF service. One can also use the Channel factory technique to connect to the WCF service easily.

How do I enable WCF tracing in Visual Studio?

Enabling Tracing. To edit the configuration file of a WCF service project in Visual Studio, right click the application’s configuration file—either Web.config for Web-hosted applications, or Appname.exe.config for self-hosted application in Solution Explorer. Then choose the Edit WCF Configuration context menu item.

Can I use SOAP or WCF with ASP NET Core?

If you are looking to use SOAP or WCF with ASP.NET Core, you are not alone. It is one of the most searched for and requested features for .NET Core.

What is ASP corewcf?

CoreWCF is a subset of the functionality from WCF, but represents what we believe are the most used features, including: NetTcpBinding supports Certificate and Windows authentication Http bindings require authentication to be configured in ASP.NET Core Username, Certificate and Windows Authentication are supported


2 Answers

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

Client initialization:

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

Implementation of EndpointLoggingBehavior:

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)
        {
        }
    }

Implementation of 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.

like image 73
Petr Pokrovskiy Avatar answered Oct 18 '22 21:10

Petr Pokrovskiy


You'll use ETW Tracing for WCF on .NET Core

https://github.com/dotnet/wcf/blob/master/Documentation/HowToUseETW.md

In my experience, you have some limitations

  1. Tracing is on for All WCF Apps, rather than configuring for a single app through config file
  2. You cannot output Messages with ETW tracing
  3. SvcTraceViewer.exe doesn't work well for trace review, you'll need to move to PerfView.exe which may present a learning curve

Benefits of ETW

  1. You avoid the performance hit from classic forms of tracing
  2. No more config change to start/stop tracing
like image 43
Tony Avatar answered Oct 18 '22 22:10

Tony