クライアントが HTTP 経由でファイルをアップロードできるようにする、ホストされた WCF サービスを構築しています。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;