在某些条件下,ASMX Web 服务可以返回空白 XML 文档吗?
伪代码示例:
string data = GetDataFromDB();
if(data != null)
return XMLDocument;
else
break;
我想这样做的原因是在响应中没有数据的情况下降低吞吐量。
Pseudocode Example:
string data = GetDataFromDB();
if(data != null)
return XMLDocument;
else
break;
The reason I want to do this is to reduce throughput if there is no data in the response.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你可以做到的。但这并不意味着您应该这样做。如果你的服务的消费者期望得到一个返回值作为回报,那么就会产生一个奇怪的错误,让他们无法收到任何东西。这显然违背了最小惊喜原则。
另一方面,如果您是编写使用 Web 服务的代码的人,请随意做您想做的事!
编辑:
如果您想减少带宽使用,您可以考虑不使用 SOAP xml 序列化程序,而可能使用 SOAP 二进制序列化程序(或者/并且您可以为您的服务启用内容压缩)。如果您想要一个比 SOAP 更简洁(并且消耗更少带宽)的跨平台 Web 服务,您还可以使用普通的旧 xml/json。在这种情况下,WCF Web API是一个非常好的平台。
You can do it. But that does not mean you should. If the consumers of your service expects a return value in return, it will create a strange bug for them to not receive anything. It clearly goes against the principle of least surprise.
On the other hand, if you are the one making the code consuming the web service, feel free to do as you wish!
Edit:
if you want to reduce bandwidth usage, you may consider not using the SOAP xml serializer and perhaps instead using SOAP binary serializer (or/and you could enable content compression for your service). If you want a cross platform web service that's less verbose than SOAP (and less bandwidth consuming), you could also use plain old xml/json. In this case, WCF Web API is a very good platform.
好吧,您可以返回 null,或者如果您不喜欢这样,则返回一个新对象。
您的实际代码将如下所示:
考虑在 SOAP 中,
null
被序列化为长度非 0 的 XML 片段Well, you could either return null, or if you don't like that, then return a new object.
Your actual code would be something like this:
Consider that in SOAP, a
null
is serialized into an XML fragment which has a non 0 length是的,这是可能的。
我在 Web 应用程序中有一个 Web 服务,如果服务已按请求完成并且没有要返回的数据,则该服务会返回空响应。不过,我确实确保服务返回带数据的“200 - OK”状态和不带数据的“204 - 无内容”状态。
使用 HttpStatusCode 枚举,因此您的代码具有
HttpStatusCode.OK
或HttpStatusCode.NoContent
而不是 200 或 204。这使得后来的编码人员一眼就能看出意图,而不必知道 204 的含义没有内容。Yes, it is possible.
I have a webservice within a WebApp that returns an empty response if the service has done as requested and they was no data to return. I did however, ensure the the serivce returns a "200 - OK" status with data and a "204 - No content" status with no data.
Use the HttpStatusCode enum, so your code has
HttpStatusCode.OK
orHttpStatusCode.NoContent
and not 200 or 204. This allows later coders to see intent at a glance and not have to know that 204 means No Content.