【问题标题】:Wcf Service net.tcp calls failingWcf 服务 net.tcp 调用失败
【发布时间】:2015-04-22 14:47:52
【问题描述】:

我正在尝试设置一些测试来了解 WCF 服务的工作方式。对于 basicHttp、netTcp、netPipe 绑定,我有类似的代码调用带有这些绑定的 WCF 服务。

using (_log = new StreamWriter(@"C:\TestRunnerLog.txt", true))
{
    var methodCallInformation = new MethodCallInformation { Method = NetPipeServiceCall, TotalTimeTakenForAllMethodCalls = new TimeSpan(), TotalNumberOfTimesMethodCalled = 0, TimesErrored = 0 };

    for (var i = 0; i < 25000; i++)
    {
        try
        {
            var stopwatch = new Stopwatch();
            stopwatch.Start();
            using(var channelFactory = new ChannelFactory<IChatHub>(new NetTcpBinding { Security = new NetTcpSecurity { Mode = SecurityMode.None } }, "net.tcp://localhost/test/test.svc"))
            {
                var channel = channelFactory.CreateChannel();
                await channel.GetPeople();
            }
            stopwatch.Stop();
            return stopwatch.Elapsed;

            methodCallInformation.TotalTimeTakenForAllMethodCalls += stopwatch.Elapsed;
        }
        catch (Exception e)
        {
            methodCallInformation.TimesErrored++;
        }

        methodCallInformation.TotalNumberOfTimesMethodCalled++;
    }

    await _log.WriteLineAsync(string.Format("net.tcp:\tTimesCalled:{0}\tTotalTime:{1}\tAverageTime:{2}\tErrors:{3}", methodCallInformation.TotalNumberOfTimesMethodCalled, methodCallInformation.TotalTimeTakenForAllMethodCalls, new TimeSpan(methodCallInformation.TotalTimeTakenForAllMethodCalls.Ticks / methodCallInformation.TotalNumberOfTimesMethodCalled), methodCallInformation.TimesErrored));
}

我对我得到的结果感到困惑。我在本地运行所有迭代,并让服务和客户端都在我的机器上本地运行。根据我的研究,netPipe 应该是最快的,其次是 netTcp,然后是 basicHttp。我的代码随机化了这些调用,而不是像上面的示例那样,每个循环只为所有迭代调用一个绑定。

----------------------------------------------------------------------------------------------
http:       TimesCalled:24981   TotalTime:00:00:46.0452454  AverageTime:00:00:00.0018432    Errors:0
net.tcp:    TimesCalled:25168   TotalTime:00:00:16.7776697  AverageTime:00:00:00.0006666    Errors:17593
net.pipe:   TimesCalled:24786   TotalTime:00:00:50.5698790  AverageTime:00:00:00.0020402    Errors:0

我正试图弄清楚我做错了什么才能得到 net.tcp 的所有这些错误,以及为什么 net.pipe 的时间似乎如此缓慢。

这是我在服务上设置的绑定:

<bindings>
    <basicHttpBinding>
        <binding name="basicHttpDefaultBinding" maxReceivedMessageSize="2147483647" maxBufferPoolSize="2147483647" maxBufferSize="2147483647">
            <readerQuotas maxArrayLength="2147483647" maxStringContentLength="2147483647" maxDepth="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
            <security mode="None">
                <transport clientCredentialType="None" />
            </security>
        </binding>
    </basicHttpBinding>
    <netTcpBinding>
        <binding name="netTcpDefaultBinding" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
            <readerQuotas maxArrayLength="2147483647" maxStringContentLength="2147483647" maxDepth="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
            <security mode="None">
                <transport clientCredentialType="None" />
                <message clientCredentialType="None" />
            </security>
        </binding>
    </netTcpBinding>
    <netNamedPipeBinding>
        <binding name="netNamedPipeDefaultBinding" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
            <readerQuotas maxArrayLength="2147483647" maxStringContentLength="2147483647" maxDepth="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
            <security mode="None">
            </security>
        </binding>
    </netNamedPipeBinding>
</bindings>

任何帮助将不胜感激。

【问题讨论】:

  • 请提供反馈。为什么投反对票?
  • 你实际上并没有问过问题。
  • 对不起,如果不是很明显,但我在帖子中确实有这个:我试图找出我做错了什么来获得所有这些 net.tcp 错误以及为什么net.pipe 的时间似乎很慢。补充一点。我没有正确地创建连接或处理它们吗?为什么我会看到这种行为?

标签: c# wcf net.tcp


【解决方案1】:

您的 NetTcp 测试可能完成得更快,因为它在 ~25,000 次中抛出了 ~17,000 次异常。您或多或少只是测试应用程序抛出异常的速度。

在本地运行时,绑定之间的差异几乎无法察觉,并且环境因素可能足以影响您的结果。通过网络,结果可能会有所不同,主要是由于消息编码(二进制与文本,二进制更有效)。这有望回答您关于为什么命名管道与 NetTcp 相比“慢”的问题。

其次,要确定 NetTcp 失败的原因,您需要停止隐藏异常。

    catch (Exception e)
    {
        // you should also be reporting your exception somewhere here...
        methodCallInformation.TimesErrored++;   

    }

或者只是将 Visual Studio 设置为中断所有异常并在调试模式下运行。根据我的经验,推测为什么某事可能会产生错误通常不是解决问题的好方法。这是一种只有在没有其他可用选项时才应保留的方法。如果您提供异常详细信息,我们可能会提供更多见解。

【讨论】:

  • 感谢您的回复。我将尝试收集一些异常信息。我从少量迭代开始并不断增加它。 ~7500 次调用没有为 tcp 提供错误,但是一旦我开始 ~10000 是错误开始的时间,这就是我怀疑处理不当的原因。据我所知,这三个协议都使用 http 协议,并且 pipe 应该是单个机器上最快的,tcp 应该在网络上表现更好,而 http 在跨网络上表现更好。如果我将服务移到另一个盒子,我会看到性能下降是有道理的,但这是所有这些盒子的峰值吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-16
  • 2011-12-26
  • 2011-10-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多