【发布时间】:2011-05-04 14:32:59
【问题描述】:
我正在尝试使用 WCF REST 将文件流式传输到服务器。当我在控制台上托管应用程序时,流式传输文件。 IE。当我在循环中发送字节(读取要发送的文件)并在服务器端保留一个调试器时,该服务过去常常被每个循环命中。但是现在我已经在 IIS 6 上托管了该服务,只有在我关闭流时才会触发该服务。这是一些 IIS6 问题还是我做错了什么?
以下是 web.config:
<system.web>
<compilation debug="true" targetFramework="4.0" />
<pages validateRequest="false" />
<httpRuntime maxRequestLength="102400" executionTimeout="3600" requestValidationMode="2.0" requestPathInvalidCharacters="" />
</system.web>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<bindings>
<webHttpBinding>
<binding name="streamWebHttpBinding" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" receiveTimeout="01:00:00" sendTimeout="01:00:00" transferMode="Buffered" />
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="FileUpload.FileUploadBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="RestBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="FileUpload.UploadData" behaviorConfiguration="FileUpload.FileUploadBehavior" >
<endpoint behaviorConfiguration="RestBehavior" address="" contract="FileUpload.IUpload" binding="webHttpBinding" bindingConfiguration="streamWebHttpBinding" />
</service>
</services>
</system.serviceModel>
请帮忙
编辑:
放置客户端代码:
HttpWebRequest req = GetWebRequest("asyncfileupload", fileName);
// 64 KB buffer
byte[] buf = new byte[0x10000];
Stream st = req.GetRequestStream();
int bytesRead;
using (FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read))
{
bytesRead = fs.Read(buf, 0, buf.Length);
while (bytesRead > 0)
{
st.Write(buf, 0, bytesRead);
bytesRead = fs.Read(buf, 0, buf.Length);
}
st.Close();
}
错误发生在代码“st.Write(buf, 0, bytesRead);”处它说 - 请求被中止:请求被取消。约 2 分钟后
【问题讨论】:
-
也许尝试在流上调用 Flush() 作为快速修复?
-
我的代码在使用 Stream.Write 发送流中的字节时出错。它给出错误 - 请求被中止:请求被取消。约 2 分钟后
-
是在你调用flush之后吗?您能否发布您的错误以及该错误附近的几行代码?
-
用客户端代码编辑了代码和我出错的地方