.NET 8调用WCF服务性能远逊于.NET Framework 4.7问题求助
基于.NET Framework 4.7的WCF类库迁移至.NET 8后性能暴跌问题
将依赖WCF连接仪器设备的.NET Framework 4.7类库迁移至.NET 8后,所有WCF服务调用性能出现大幅下降(非首次调用独有问题):本地同机运行时,.NET Framework 4.7下性能测试耗时约3秒,.NET 8下耗时长达35秒,每次服务调用存在约2秒的额外开销。
性能测试代码
public void PerformanceTest() { var stopWatch = Stopwatch.StartNew(); Console.WriteLine("Start: " + stopWatch.ElapsedMilliseconds); using (var target = MakeGaiaNetworkInstrument()) { Console.WriteLine("After constructor: " + stopWatch.ElapsedMilliseconds); Assert.AreEqual(true, target.Lock(), "Lock failed"); var testParamStringName = "Parameter Name"; var testValueString = "Value"; target.SetParameter(testParamStringName, testValueString); Console.WriteLine("Before loop: " + stopWatch.ElapsedMilliseconds); for (var repetition = 0; repetition < 10; repetition++) { Assert.AreEqual(testValueString, target.GetParameter(testParamStringName)); } Assert.AreEqual(true, target.Unlock()); Console.WriteLine("After loop: " + stopWatch.ElapsedMilliseconds); } }
注:target为WCF客户端代理实例。
测试输出对比
.NET Framework 4.7测试输出
Start: 0 After constructor: 3310 Before loop: 3321 After loop: 3347
.NET 8测试输出
Start: 0 After constructor: 8508 Before loop: 12609 After loop: 35268 Added measurement for each method in the new code: Start: 0 After constructor: 6567 Gaia Lock took: 2045 Gaia SetParameter took: 2043 Before loop: 10659 Gaia GetParameter took: 2030 Gaia GetParameter took: 2024 Gaia GetParameter took: 2047 Gaia GetParameter took: 2025 Gaia GetParameter took: 2030 Gaia GetParameter took: 2043 Gaia GetParameter took: 2052 Gaia GetParameter took: 2063 Gaia GetParameter took: 2042 Gaia GetParameter took: 2047 Gaia Unlock took: 2043 After loop: 33112
WCF客户端代理创建代码
var binding = new BasicHttpBinding { MaxReceivedMessageSize = 10 * 1024 * 1024, ReaderQuotas = new XmlDictionaryReaderQuotas { MaxStringContentLength = 100000, }, UseDefaultWebProxy = false, BypassProxyOnLocal = true, }; this.server = new GaiaServicePortTypeClient(binding, new EndpointAddress(this.ServerUri));
已尝试的优化措施
- 修改缓存设置、添加消息处理行为,无任何效果
- 使用空
BasicHttpBinding配置,性能未改善 - 将
UseDefaultWebProxy设置为true,无性能提升 - 代理代码从原
svcutil工具生成改为dotnet-svcutil生成,通过Fiddler验证发送至服务端的XML请求完全一致
服务端为C++编写,无法修改;运行环境为全新安装的Windows 11。
内容的提问来源于stack exchange,提问作者user24146667
相关产品推荐
相关产品推荐

