.NET 5/6 上 WCF 的 GetWebRequest 替代方案
我在 .NET FW 4.7 项目中有一个 WCF 客户端,我正在尝试将其升级到 .NET 5/6。 我已经使用 dotnet-svcutil 从 WSDL 获取新版本的 WCF 客户端,但我一直坚持迁移依赖于 GetWebRequest 的连接统计日志记录方法,如下所示:
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 .NET 5/6 WCF 客户端?看来 GetWebRequest 在 System.ServiceModel.ClientBase
上不可用
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
.NET 5 中的新增功能
Windows Communication Foundation (WCF) 的原始实现仅在 Windows 上受支持。但是,.NET Foundation 提供了一个可用的客户端端口。它是完全开源、跨平台的,并由 Microsoft 支持。下面列出了核心 NuGet 包:
您可以查看 .NET 5/6 中 GetWebRequest 的用法
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