是否有内置方法来确定 WCF 响应的大小?
在客户端获得 Web 请求的完整有效负载之前,我们希望首先向其发送对其将获得的响应大小的测量。如果响应太大,客户端将向用户显示一条消息,让他们选择中止操作。
我们可以编写一些自定义代码来在服务器上预加载响应,确定大小,然后将其传递给客户端,但如果有其他方法可以做到这一点,我们宁愿不这样做。
有谁知道 WCF 是否有任何棘手的方法来做到这一点?或者有没有免费的第三方工具可以完成此任务?
谢谢。
Before a client gets the full payload of the web request, we'd like to first send it a measurement of the size of the response it will get. If the response will be too large, the client will present a message to the user giving them the option to abort the operation.
We can write some custom code to preload the response on the server, determine the size, and then pass it on to the client, but we'd rather not if there's another way to do it.
Does anyone know if WCF has any tricky way to do this? Or are there any free third party tools out there that will accomplish this?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不认为 WCF 或 .NET 框架中有什么“棘手”的东西可以做到这一点,真的。你要传回给客户端什么?一个类的实例?
您可以做的是运行查询,或者无论如何获取响应,然后将其序列化到内存流中并查看它有多大。这不会是一个完全准确的大小 - SOAP 消息有一些开销,如 SOAP 信封和标头等 - 但它可以为您提供一个大概的数字,表明您是否要返回几百个字节,或者一个几兆字节。
问题是:这可能需要在服务器上花费一段时间来组装/查询,然后再实际“测量”。另外,您几乎必须进行两次调用 - 一次调用“MeasureResult”,返回 Int 或 Long 或其他值,然后第二次调用“GetResult”以实际获取结果。因此,您将花费两次精力来组装消息......
我真的没有一个好的答案给您,但也许您只需要找出其他方法来允许客户端在需要时中止呼叫太长了。或者找到一种方法来更快地找出响应有多大的指标(无需获取响应本身的所有详细信息)。
I don't think there's anything "tricky" in WCF or the .NET framework to do this, really. What are you passing back to the client? An instance of a class?
What you could do is run the query or however you fetch the response, and then serialize that into a memory stream and see how big it gets. This won't be a totally accurate size - the SOAP messages has some overhead to it, like SOAP envelope and headers and stuff - but it can give you a ballpark figure of whether you're about to return a few hundred bytes, or a couple megabytes.
Trouble is: this might take a while on the server just to assemble / query, and then to actually "measure", too. Plus you'd almost have to have two calls - one to "MeasureResult" which returns an Int or Long or something, and then a second call "GetResult" to actually get the results. So you'll incur that effort to assemble the message twice....
I don't really have a good answer for you, but maybe you just need to figure out some other way to allow the client to abort a call if it takes too long. Or find a way to more quickly figure out an indicator as to how large the response will be (without getting all the details of the response itself).