我正在构建一个托管的 WCF 服务,允许客户端通过 HTTP 上传文件。服务逐块读取客户端的Stream
块。这适用于只需要一次迭代的小文件。但是当在一些块之后上传更大的文件时,我IOException
会An exception has been thrown when reading the stream.
在Stream.EndRead()
.
内部异常是The I/O operation has been aborted because of either a thread exit or an application request.
读取块的数量各不相同,但我无法弄清楚是什么导致了这种差异。它的工作时间从 300 毫秒到 550 毫秒不等,处理时间从 ~1MB 到 ~2MB。
有人有线索吗?
接口定义如下:
[ServiceContract]
public interface IServiceFileserver
{
[OperationContract]
UploadResponse UploadFile(UploadRequest uploadRequest);
// All status feedback related code is left out for simplicity
// [OperationContract]
// RunningTaskStatus GetProgress(Guid taskId);
}
[MessageContract]
public class UploadRequest
{
[MessageHeader()]
public string FileName { get; set; }
[MessageHeader()]
public long SizeInByte { get; set; }
[MessageBodyMember(Order = 1)]
public Stream Stream { get; set; }
}
[MessageContract]
public class UploadResponse
{
[MessageBodyMember()]
public Guid TaskId { get; set; }
}
这是服务实现:
const int bufferSize = 4 * 1024;
// This is called from the client side
public UploadResponse UploadFile(UploadRequest uploadRequest)
{
Guid taskId = Guid.NewGuid();
Stream stream = null;
try
{
stream = uploadRequest.Stream;
string filename = uploadRequest.FileName;
long sizeInBytes = uploadRequest.SizeInByte;
byte[] buffer = new byte[bufferSize];
stream.BeginRead(buffer, 0, bufferSize, ReadAsyncCallback, new AsyncHelper(buffer, stream, sizeInBytes));
}
catch (Exception ex)
{
if (stream != null)
stream.Close();
}
return new UploadResponse() { TaskId = taskId };
}
// Helper class for the async reading
public class AsyncHelper
{
public Byte[] ByteArray { get; set; }
public Stream SourceStream { get; set; }
public long TotalSizeInBytes { get; set; }
public long BytesRead { get; set; }
public AsyncHelper(Byte[] array, Stream sourceStream, long totalSizeInBytes)
{
this.ByteArray = array;
this.SourceStream = sourceStream;
this.TotalSizeInBytes = totalSizeInBytes;
this.BytesRead = 0;
}
}
// Internal reading of a chunk from the stream
private void ReadAsyncCallback(IAsyncResult ar)
{
AsyncHelper info = ar.AsyncState as AsyncHelper;
int amountRead = 0;
try
{
amountRead = info.SourceStream.EndRead(ar);
}
catch (IOException ex)
{
Trace.WriteLine(ex.Message);
info.SourceStream.Close();
return;
}
// Do something with the stream
info.BytesRead += amountRead;
Trace.WriteLine("info.BytesRead: " + info.BytesRead);
if (info.SourceStream.Position < info.TotalSizeInBytes)
{
try
{ // Read next chunk from stream
info.SourceStream.BeginRead(info.ByteArray, 0, info.ByteArray.Length, ReadAsyncCallback, info);
}
catch (IOException ex)
{
info.SourceStream.Close();
}
}
else
{
info.SourceStream.Close();
}
}
绑定定义如下:
BasicHttpBinding binding = new BasicHttpBinding();
binding.TransferMode = TransferMode.Streamed;
binding.MessageEncoding = WSMessageEncoding.Mtom;
binding.MaxReceivedMessageSize = 3 * 1024 * 1024;
binding.MaxBufferSize = 64 * 1024;
binding.CloseTimeout = new TimeSpan(0, 1, 0);
binding.OpenTimeout = new TimeSpan(0, 1, 0);
binding.ReceiveTimeout = new TimeSpan(0, 10, 0);
binding.SendTimeout = new TimeSpan(0, 1, 0);
binding.Security.Mode = BasicHttpSecurityMode.None;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;