从WCF服务器向Delphi客户端发送大数据 [英] Sending large data from WCF Server to Delphi Client

查看:62
本文介绍了从WCF服务器向Delphi客户端发送大数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个具有下载文件功能的WCF服务。这个WCF将被Delphi应用程序使用。

I need to create a WCF Service that will have a download file function. This WCF will be consumed by a Delphi application.

问题:将要下载的文件非常大,可能会在Delphi端引起内存问题。到目前为止,我有以下代码:

The problem: The files that will be downloaded are very large and may cause memory problems on Delphi side. So far, I have this code:

[OperationContract]
byte[] DownloadFile(string filePath);

但是此代码将导致客户端应用将所有数据保存在内存中,这可能是一个问题。

But this code will cause the client app to hold all data in memory which can be an issue.

我已经阅读到WCF能够流式传输数据,您可以在以下位置阅读:如何:启用流式传输

I have read that WCF is capable of streaming data as you can read at: How to: Enable Streaming

但是我对从MSDN截取的这段代码有疑问:

But I have a question regarding this piece of code cut from MSDN:

[OperationContract]
Stream GetStream(string data);

在客户端,我想传递 TFileStream 该功能。通过使用 TFileStream ,每个读取的字节将直接进入磁盘。但是该函数返回一个流,并且我想要的将无法实现,因为该流将不是该函数的参数。

On the client side I want to pass a TFileStream to the function. By using TFileStream every byte read will go directly to the disk. But the function RETURNS a stream and what I want will not be possible since the stream will not a parameter to the function.

如何从WCF服务中下载文件

How can I download a file from a WCF service directly to the disk?

推荐答案

我发现与其他人一起使用时,依靠WCF中的内置流功能(非.NET客户端是产生奇怪问题的重要来源...

I have found that relying on "built-in" streaming capability in WCF when working with other (non-.NET) clients is a big source for strange problems...

基本上,我们通过定义以下情形来解决这种情况:

Basically we solve this kind of scenario by defining:

[OperationContract]
string DownloadFile(string filePath);

该方法生成HTTP(S)url并将其返回...

The method generates a HTTP(S) url and returns it...

这样,任何支持HTTP的客户端都可以以健壮的方式处理数据...

This way any http-capable client can work with the data in a robust fashion...

请注意,这会使服务器成为服务器稍微复杂一点,因为您现在需要某种机制来生成URL(并在其上提供HTTP GET)(安全性,全局唯一性,仅在有限的时间内可用等)。

BEWARE that this makes the server a bit more complicated since you now need to have some mechanism to generate (and serve HTTP GET on) URLs (security, "globally" unique, only usable for a limited time etc.).

但是最大的好处是,只要有http支持,Delphi的任何客户端(移动或一些奇怪的嵌入式设备,或者您可能遇到的任何东西)都将能够实施此方案。 -客户端选项)。

BUT the big advantage is that any client out there (mobile or some strange embedded device or whatever you might encounter) will be able to implement this scheme as long as it has http-support available (Delphi has some very good http-client options).

这篇关于从WCF服务器向Delphi客户端发送大数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆