WCF - 大响应在我的代码返回和发送到客户端之间需要很长时间,同时 CPU 使用率为 100%

发布于 2024-11-30 06:53:15 字数 602 浏览 0 评论 0原文

情况是这样的:

  • 我有一个带有双工nettcp绑定的WCF主机
  • 我有一个WCF客户端,它请求一个包含字节数组的对象(大约2MB的图像)
  • 我使用实体框架从我的数据库是这样的:
    • id(整数)
    • 类型(字符串)
    • 数据(大约 2MB 的字节[])
  • 然后我将其返回给客户端(实际上是.NET wcf代码,它应该在其中被序列化然后发送给客户端),从那时起,我无法进一步调试,我的代码似乎在服务器中返回正常,但我的客户端一直等待 15 秒或更长时间,直到收到服务主机的回复。 我的 CPU 使用了 100% 的核心(由托管服务的应用程序使用),因此它在我返回和发送之间忙于执行某些操作。我确信这不是发送花了这么长时间,因为我的可靠会话开始要求重传,因为它没有收到任何东西,这使得情况变得更糟......所以我关闭了可靠会话,是什么导致服务只具有回复我的请求一次:),但是 2MB 数据需要 15 秒……

当我没有数据时(数据部分为空),它工作得很好。图像越大,花费的时间就越长...最奇怪的是它有时会非常快(1秒或类似的东西(但这不会发生太多,而且似乎是随机的)。这种情况发生在我测试过的所有电脑,所以与我的电脑无关。

The situation is like this:

  • I have a WCF host with a duplex nettcpbinding
  • I have a WCF client that asks for an object which contains a byte array (an image of approximately 2MB)
  • I get this object (server side) using the entity framework from my database and it is something like this:
    • id (int)
    • type (string)
    • data (byte[] of approx 2MB)
  • I then return it to the client (actually to the .NET wcf code, where it should get serialized and then sent to the client), and from then on I can't debug any further and my code seems to have returned fine in the server, but my client keeps waiting for 15 seconds or more until it gets a reply from the service host.
    My cpu uses 100% of a core (used by the application that hosts the service), so it's busy with doing something between my return and sending it. I'm sure it's not the sending that's taking so long, because my reliable session starts to ask for retransmissions, since it's not receiving anything, making it even worse... So i turned off the reliable session, what caused the service only having to reply once to my request :), but 15 bloody seconds for 2MB of data...

When i have no data (so null for the data part), it works fine. The larger the image gets, the longer it takes... And the strangest thing is that it sometimes goes very fast (1 second or something like that (but that doesn't happen much and it seems to be random). This happens on all computers I've tested, so nothing to do with my PC.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小兔几 2024-12-07 06:53:15

可以在本地重现吗?

如果是这样,我建议使用 asp.net mini profiler。我会在每个页面上使用它,即使是那些没有明显性能问题的页面。

http://code.google.com/p/mvc-mini-profiler/

我也发现了一篇不错的文章;
http://www.hanselman.com/blog/NuGetPackageOfTheWeek9ASPNETMiniProfilerFromStackExchangeRocksYourWorld.aspx

Can you reproduce it locally?

If so, I would suggest using asp.net mini profiler. I would use it on every single page, even ones that don't have obvious performance issues.

http://code.google.com/p/mvc-mini-profiler/

I found a nice article on it too;
http://www.hanselman.com/blog/NuGetPackageOfTheWeek9ASPNETMiniProfilerFromStackExchangeRocksYourWorld.aspx

烟酉 2024-12-07 06:53:15

那么 100% 的 CPU 都在服务器端,对吗?

您可以在 WCF 代码中添加其中一些内容,看看是否可以缩小导致问题的代码行范围。

DateTime Start = DateTime.Now;
DateTime End = DateTime.Now;
TimeSpan CallTime = End - Start;
Console.WriteLine("Call Time(MS): " + CallTime.Milliseconds.ToString());

没有看到任何代码,我猜它是数据库对象的 Serialize 方法。您可以实现自定义序列化接口,因为您实际上只对发送字节[]感兴趣。

So the 100% CPU is on the server side, correct?

You could sprinkle some of these in your WCF code to see if you can narrow down the line of code causing the issue.

DateTime Start = DateTime.Now;
DateTime End = DateTime.Now;
TimeSpan CallTime = End - Start;
Console.WriteLine("Call Time(MS): " + CallTime.Milliseconds.ToString());

Not seeing any code, I would guess it is the Serialize method of your database object. You could implement a custom serialization interface since you are really just interested in sending the byte[].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文