wcf - 直接写入 HttpResponse 时 WCF REST 失败网络错误
问题描述
我正在创建一个 WCF REST Web 服务,用于下载大文件(可能超过 2GB)。这些文件是不存储在硬盘驱动器上的 csv 文件,而是在发出请求时在运行时形成的。正因为如此,我选择将 csv 文件直接写入HttpResponse
避免任何不会容纳超过 2GB 数据的中间容器。这是我写的一个测试代码,它符合我的描述:
[ServiceContract]
public interface IDownloadService
{
[OperationContract]
[WebInvoke(
Method = "GET",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "")]
void Download();
}
public class DownloadService : IDownloadService
{
public void Download()
{
var response = HttpContext.Current.Response;
response.Clear();
response.ContentType = "application/csv";
response.AddHeader("Content-Disposition", "attachment; filename=myfile.csv");
//This is a small test csv file, it will be replaced with a big file,
//that will be formed in runtime and written piece by piece into Response.OutputStream
response.BinaryWrite(System.Text.Encoding.UTF8.GetBytes("1,2,3"));
response.Flush();
response.Close();
response.End();
}
}
这是我的Web.config
,以防万一:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.6.1" />
<httpRuntime targetFramework="4.6.1"/>
</system.web>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="RestWcf.DownloadService">
<endpoint address="" binding="webHttpBinding" behaviorConfiguration="web" contract="RestWcf.IDownloadService"/>
</service>
</services>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
</configuration>
现在这是有趣的部分,当我从 调用这个 webservice 方法时Google Chrome
,首先它开始下载文件,在下载 Chrome 结束时给我错误Failed - Network error
,当我尝试调用它时,Postman
我收到Could not get any response
消息但没有生成响应,最后当我尝试从Fiddler
我收到 504 响应中调用它,并提供更多信息ReadResponse() failed: The server did not return a complete response for this request. Server returned 384 bytes.
关于这里发生了什么以及如何解决这种奇怪行为的任何想法?
解决方案
推荐阅读
- java - tomcat WEB-INF上的jar名称列表
- javascript - 使用变换 api 的 3d 幻觉效果
- docker - Rabbitmq - 端口 5672 上的 Docker 连接被拒绝
- java - 在给定的数组段中查找最小数
- php - php数组进入更新sql
- javascript - 用于重用函数接口的 JSDoc
- python - 在 Amazon ElasticBean Stalk 上运行 Flask 应用程序时出错
- android - 使用警报管理器设置多个警报
- docker - 导入后无法运行 docker gitlab 容器
- r - 使用 ggplot2 为组自定义颜色