GetWebRequest alternative for WCF on .NET 5/6

340 views Asked by At

I've got a WCF client in a .NET FW 4.7 project, which I'm trying to upgrade to .NET 5/6. I've used dotnet-svcutil to do get the new version of WCF client from the WSDL, but I'm stuck on migrating the connection stats logging method which relies on GetWebRequest, and looks like this:

public static void Filter(HttpWebRequest wrq, WsAuthClientConfig config)
{

    // set keep alive 
    wrq.KeepAlive = config.KeepAlive;

    // manage connection (whatever it does)
    if (config.ManageConnection)
    {
        wrq.ServicePoint.ConnectionLeaseTimeout = config.KeepAlive ? 180000 : 0;
        wrq.ServicePoint.MaxIdleTime = 120000;
        wrq.ServicePoint.SetTcpKeepAlive(true, 120000, 20000);
    }

    // service point status vars 
    var to = wrq.ServicePoint.ConnectionLeaseTimeout;
    var idle = wrq.ServicePoint.IdleSince;
    var mit = wrq.ServicePoint.MaxIdleTime;
    var addr = wrq.ServicePoint.Address;
    var conns = wrq.ServicePoint.CurrentConnections;
    var limit = wrq.ServicePoint.ConnectionLimit;
    var nagle = wrq.ServicePoint.UseNagleAlgorithm;
    var ver = wrq.ProtocolVersion;
    var expect100 = wrq.ServicePoint.Expect100Continue;

    // log debug status
    if (Logger.GetGlobalLevelThreshold() == LoggingLevel.Debug)
    {
        Logger.Debug("svcpt: {8}, keepalive: {7}, lease_to: {0}, idle_since: {1}, max_idle: {2}, addr: {3}, conns: {4}, limit: {5}, ver: {6}, nagle: {9}, expect100: {10}",
            to, idle, mit, addr, conns, limit, ver, wrq.KeepAlive, wrq.ServicePoint.GetHashCode(), nagle, expect100);
    }

    // log if all stack availalbe connections are used
    if (limit > 4 && (limit - conns) < 3)
    {
        Logger.Warning("Connection pool near exhausted: svcpt: {0}, keepalive: {1}, conns: {2}, limit: {3}, address: {4}",
            wrq.ServicePoint.GetHashCode(), wrq.KeepAlive, conns, limit, addr);
    }            
}

How does one get the connection stats in the .NET 5/6 WCF client? It seems that GetWebRequest is not available on System.ServiceModel.ClientBase

1

There are 1 answers

2
Lan Huang On

What's new in .NET 5
The original implementation of Windows Communication Foundation (WCF) was only supported on Windows. However, there is a client port available from the .NET Foundation. It is entirely open source, cross platform, and supported by Microsoft. The core NuGet packages are listed below:
You can take a look at the usage of GetWebRequest in .NET 5/6