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?
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.
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.
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.
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.
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.
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.
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
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.
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
Benefits of ETW
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With