我正在尝试将类库从 .NET Framework 4.7 转换为 .NET 8,该类库连接到您通过 WCF 进行通信的仪器。对服务器的所有调用都会慢得多(不仅仅是第一次调用)。
我的性能测试在 .NET Framework 4.7 中大约需要 3 秒(其中大部分是构建客户端代理),但在转换为 .NET 8 的项目中需要 35 秒。服务器和客户端在同一台机器上本地运行。
这是性能测试的代码:
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);
}
}
目标是客户端代理。
.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
每次调用服务器似乎有 2 秒的开销
这是创建客户端代理的代码:
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));
我已经尝试过以下建议:
将 Net Framework 4.7.1 升级到 Net 6 使 WCF 服务消耗变慢
https://justsimplycode.com/2019/11/02/disable-header-100-continue-in-net-core-wcf-client/
https://github.com/dotnet/wcf/issues/5002
https://github.com/dotnet/wcf/issues/5184
并将代码更改为:
ClientBase<GaiaServicePortTypeClient>.CacheSetting = CacheSetting.AlwaysOn;
GaiaServicePortTypeClient.CacheSetting = CacheSetting.AlwaysOn;
var binding = new BasicHttpBinding()
{
MaxReceivedMessageSize = 10 * 1024 * 1024,
ReaderQuotas = new XmlDictionaryReaderQuotas { MaxStringContentLength = 100000, },
UseDefaultWebProxy = false,
BypassProxyOnLocal = true,
};
server = new GaiaServicePortTypeClient(binding, new EndpointAddress(ServerUri));
server.Endpoint.EndpointBehaviors.Add(new HttpMessageHandlerBehaviour());
所有更改都没有产生任何影响。我也尝试过使用空的
BasicHttpBinding
和 UseDefaultWebProxy = true
- 没有什么区别。
代理代码是使用以下命令生成的:“svcutil /d:"$(ProjectDir)\service" %WSDLFILE% /language:C# /async /internal /namespace:*,Unisensor.Gaia.Webservices /noconfig”
更改为: dotnet-svcutil -d "$(ProjectDir)\service" %WSDLFILE% --outputFile GaiaService.cs --sync --internal --namespace "*,Unisensor.Gaia.Webservices"
我已经使用 Fiddler 检查了发送到服务的 XML,它似乎是相同的。服务器是用 C++ 编写的,无法更改。我正在运行新安装的 Windows 11,没有任何公司膨胀软件。
这似乎是 WCF 中的一个错误 - 我已经在 github 上提交了错误报告: https://github.com/dotnet/wcf/issues/5498 我找到了一个解决方法,该错误似乎与 DNS 有关 - 如果您传递服务器 IP 地址而不是服务器名称,则不会造成性能损失