C# 如何将大量数据传递给Web服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1040852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 06:46:52  来源:igfitidea点击:

How to pass large quantity of data to web service

c#asp.netweb-servicesclient-server

提问by Jared

I'm building a client-server (c#) application that uses a web services to synchronize the data.

我正在构建一个使用 Web 服务来同步数据的客户端-服务器 (c#) 应用程序。

Basically I am passing the XML of a DataSet back and forth. But depending on various parameters the dataset can actually be quite large. I'm compressing the XML using Gzip and want to pass that to the web server and the get the resulting compressed XML back.

基本上我来回传递数据集的 XML。但是根据各种参数,数据集实际上可能非常大。我正在使用 Gzip 压缩 XML,并希望将其传递给 Web 服务器并返回生成的压缩 XML。

What is the best way to pass potentially large chunks of data back and forth?

来回传递潜在的大量数据的最佳方法是什么?

Clarification: I guess I'm asking what format is the best to pass the data. JSON, SOAP, ordinary POST (I'm not very familiar with Web Services so I'm sure there's more that I'm not thinking of).

澄清:我想我在问什么格式最适合传递数据。JSON、SOAP、普通 POST(我对 Web 服务不是很熟悉,所以我确定还有更多我没有想到的)。

采纳答案by Michael Meadows

Best probably depends on a lot of factors. If by best you mean most performant, here are some points to consider:

最好可能取决于很多因素。如果最好是指性能最好,请考虑以下几点:

  • XML is not the best way at all
  • Binary serialization is far more efficient.
  • You may not have this option, however, if you need to concern yourself with interoperability. In that case, you may want to consider using a flat file or delimited format.
  • If neither of those are doable, then you might consider just sending what has changed instead of all of the data.
  • If that isn't an option, then compressing the XML is an... ok solution.
  • XML 根本就不是最好的方法
  • 二进制序列化效率更高。
  • 但是,如果您需要关注互操作性,您可能没有此选项。在这种情况下,您可能需要考虑使用平面文件或分隔格式。
  • 如果这些都不可行,那么您可以考虑只发送已更改的内容而不是所有数据。
  • 如果这不是一个选项,那么压缩 XML 是一个...好的解决方案。

I'd use XML as a last resort, since there's a lot of unnecessary metadata that is included in XML that will easily increase the size of your payload by more than 5X.

我将使用 XML 作为最后的手段,因为 XML 中包含许多不必要的元数据,这些元数据很容易将有效负载的大小增加 5 倍以上。

回答by Shiraz Bhaiji

If you use WCF for your web services you can use MTOM to send data over as a binary attachment. See:

如果您将 WCF 用于您的 Web 服务,您可以使用 MTOM 将数据作为二进制附件发送。看:

What is the best way to send large data from WCF service to Client?

从 WCF 服务向客户端发送大数据的最佳方式是什么?

回答by James

What you probably want to investigate is using MTOM. This allows you to send binary SOAP in a chunked manner. This is cool because chunking the data, coupled with an AJAX UI, will allow you to present a progress bar to the user that is uploading it.

您可能想要调查的是使用MTOM。这允许您以分块方式发送二进制 SOAP。这很酷,因为将数据分块,再加上 AJAX UI,将允许您向正在上传数据的用户显示进度条。

Hereis an example of using MTOM to send/receive data in a chunked manner. I hope this helps.

是使用 MTOM 以分块方式发送/接收数据的示例。我希望这有帮助。

回答by Russ Bradberry

You can try compressing the file and sending it as binary via webservice. also, don't forget to modify your upload limits.

您可以尝试压缩文件并通过网络服务将其作为二进制文件发送。另外,不要忘记修改您的上传限制。

how to send binary data via webservice: http://support.microsoft.com/kb/318425

如何通过网络服务发送二进制数据:http: //support.microsoft.com/kb/318425

回答by VolkerK

If all you want is to apply gzip compression to your xml data you might consider enabling transparent gzip compression at the http transport level (Accept-encoding: gzip, Content-Encoding: gzip) and let the iis handle it.
see http://weblogs.asp.net/owscott/archive/2004/01/12/IIS-Compression-in-IIS6.0.aspx#58745

如果您只想对 xml 数据应用 gzip 压缩,您可以考虑在 http 传输级别启用透明 gzip 压缩(Accept-encoding: gzip, Content-Encoding: gzip)并让 iis 处理它。
http://weblogs.asp.net/owscott/archive/2004/01/12/IIS-Compression-in-IIS6.0.aspx#58745

回答by Mark Schultheiss

For web services, you have multiple concepts and possibilities that you can consider.

对于 Web 服务,您可以考虑多种概念和可能性。

Do you need multiple clients managing the same data set? Concurrently?

您是否需要多个客户端管理相同的数据集?同时?

Do you have a disconnected set where it is managed on both ends or just one manages the other views/stores/reports?

您是否有一个断开连接的集,它在两端进行管理,还是只有一个管理其他视图/存储/报告?

Are you simply using the client to use as an interface to the data set, and if so, would it make sense to use a different UI (web) to manage different parts of the dataset independently as a web UI based application?

您是否只是将客户端用作数据集的接口,如果是这样,使用不同的 UI (Web) 作为基于 Web UI 的应用程序独立管理数据集的不同部分是否有意义?

Are you making incremental changes in a transactional type interaction? (One end sends the big dataset to client and transactional changes are returned based on that?)

您是否在事务类型交互中进行增量更改?(一端将大数据集发送给客户端,然后基于此返回事务更改?)

Difficult to recommend specifically without some thoughts about if/how you might dissect the dataset for more optimal management or knowing more of the specifics here.

如果不考虑是否/如何剖析数据集以进行更优化的管理或了解更多细节,则很难具体推荐。

If you HAVE to send the large data entity, then it just becomes a matter of which method you wish to use for your pipeline, its simply and upload/download and you manage the endpoints appropriately.

如果您必须发送大数据实体,那么您希望为管道使用哪种方法就变成了问题,它很简单,上传/下载,您可以适当地管理端点。

For some random thoughts:

对于一些随机的想法:

If you want to manipulate the data in the DOM, consider JSON, which is quite compatible with JavaScript.

如果要操作 DOM 中的数据,可以考虑 JSON,它与 JavaScript 非常兼容。

If you desire the XML dataset manipulation, you could place zip or some other compression if you need to pass the entire dataset, just wire it up on each end.

如果您需要 XML 数据集操作,您可以放置​​ zip 或其他一些压缩,如果您需要传递整个数据集,只需在每一端连接它。