c# - 从 WCF 返回大字节数组 - 内存问题

标签 c# .net wcf memory large-object-heap

我需要从 WCF 服务器返回一个大字节数组。

问题是——为了返回这样一个数组——我需要创建一个——当创建这样一个数组时——它会自动转到大对象堆——这意味着当服务有压力时——我得到一个内存使用和管理中的真正问题。

我考虑过使用非托管内存来避免使用大型托管字节数组 - 但仍然 - 如何从 WCF 服务返回这样的数组?

是否有任何方法可以从不包括实际创建托管字节数组的 WCF 服务返回字节“流”?我知道 WCF 本身使用 BufferManager - 所以如果它只是读取我的非托管内存并在发送之前使用其缓冲区管理来存储它 - 我希望不会有问题。

最佳答案

您可以使用 WCF 的 Streaming Mode .从该页面:

  1. To stream data, the OperationContract for the service must satisfy two requirements:

    a. The parameter that holds the data to be streamed must be the only parameter in the method. For example, if the input message is the one to be streamed, the operation must have exactly one input parameter. Similarly, if the output message is to be streamed, the operation must have either exactly one output parameter or a return value.

    b. At least one of the types of the parameter and return value must be either Stream, Message, or IXmlSerializable.

  2. Streaming must be enabled on the binding. You set a TransferMode property, which can take one of the following values:

    a. Buffered,

    b. Streamed, which enables streaming communication in both directions.

    c. StreamedRequest, which enables streaming the request only.

    d. StreamedResponse, which enables streaming the response only.

该页面上也有一些不错的示例,包括如何写回自定义流(位于底部)。

有关编码、流媒体和 session 的更多背景信息,以及使用流媒体时的一些安全注意事项,请参阅 this MSDN page .

关于c# - 从 WCF 返回大字节数组 - 内存问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13476708/

相关文章:

c# - 字典到 ListView TwoWay 绑定(bind) - 可能吗?

c# - App.Config 连接字符串

c# - 从 WebApi 返回 JsonResult 不工作 ASP.NET

android - 在 android 应用程序中使用 .net 服务?

javascript - 如何将从 jquery mobile 表单输入的查询 SQL 发布到 WCF 服务并在 WCF 服务上运行该查询?

c# - 以线程安全的方式添加到 Parallel.ForEach 循环中的列表

c# - 在不重新启动程序的情况下更新列表框?

C# : Cancelling MySqlCommand using CancellationToken giving NULLReferenceException

c# - 无法让 netTcpBinding 请求显示在 Fiddler 中

c# - 在 silverlight 中使用 WCF CloseAsync