This is a hacked-up copy of work done by the Application Insights team in the Application Insights SDK Lab repository. The original work enabled AppInsights instrumentation of non-HTTP WCF request and dependency calls on both clients and servers in .NET Framework. I was not involved with any of that work, have no affiliation with that team, and have never worked for Microsoft.
This repository is an attempt to port that instrumentation over to .NET 6.0 and later. As little code as possible is being changed, but a number of features are disabled for the foreseeable future--most notably any WCF Server monitoring. Only WCF Clients using the System.ServiceModel libraries (not the CoreWCF libraries) can be monitored.
This assumes you already have Application Insights installed and working in your project. If you do not have Application Insights installed, review this guide from Microsoft for an ASP.NET Core application or this guide for all other apps.
- Install the package
WcfApplicationInsights.ServiceModel - When configuring your client's
ChannelFactory, add a reference to theWcfApplicationInsightsnamespace - Then inject the configured App Insights
TelemetryClient - Instantiate a new
ClientTelemetryEndpointBehaviorand pass in theTelemetryClient - Add the new
ClientTelemetryEndpointBehaviorto the channel factory's endpoint behaviors
A complete example is:
var telemetryClient = serviceProvider.GetRequiredService<TelemetryClient>();
var netTcpBinding = new NetTcpBinding()
{
Security = new NetTcpSecurity
{
Transport = new TcpTransportSecurity
{
ClientCredentialType = TcpClientCredentialType.None
},
Mode = SecurityMode.Transport
}
};
var address = new EndpointAddress("<your service address>");
var channelFactory = new ChannelFactory<IEchoService>(netTcpBinding, address);
channelFactory.Endpoint.EndpointBehaviors.Add(new ClientTelemetryEndpointBehavior(telemetryClient));Once the ClientTelemetryEndpointBehavior is added, all new connections and WCF service calls should be automatically logged to your Application Insights resource.
- Autoinstrumentation
- The existing autoinstrumentation relied on the
Microsoft.AI.Agent.Interceptpackage, which was not ported to .NET Core. It appears to use bytecode analysis to find and intercept certain calls, which would be challenging and probably undesirable to reimplement. - However, the
System.ServiceModelimplementations in .NET Core and onwards do appear to implementEventSourcemethods that could be listened for.
- The existing autoinstrumentation relied on the
- Server Support
- Client WCF libraries were ported over to .NET Core early on, but server libraries were not, so that
System.ServiceModelonly contains classes necessary for building clients. The CoreWCF project later implemented server support, but had to reimplement many of the same classes already inSystem.ServiceModel. Consequently supporting non-Framework WCF servers will require changing all the namespace references to point at theCoreWCFversions instead, as well as uncommenting and fixing up all the disabled serverside code - This can probably be done with careful use of conditional compilation, but needs work
- Client WCF libraries were ported over to .NET Core early on, but server libraries were not, so that