I'm trying to deploy ovf-image through vSphere .Net API and encountered a problem during virtual disk upload. After importing OVF template, I start uploading *.vmdk file via HTTP POST, that seems to begin normally, but then fails with WebException "The request was aborted: The request was canceled". Percentage of loaded bytes differs from one vCenter to another - somewhere upload fails on 10%, somewhere on 90%, somewhere it completes successfully. Code implementing the upload:
private bool PrepareUpload(string url)
{
try
{
_fileSize =
new FileInfo(_vmdkPath).Length;
_fileStream =
File.OpenRead(_vmdkPath);Interlocked.Exchange(ref _bytesLoaded, 0);
_postRequest = (HttpWebRequest)WebRequest.Create(url);
_postRequest.KeepAlive =
true;
_postRequest.SendChunked =
true;
_postRequest.AllowWriteStreamBuffering =
false;
_postRequest.Method =
"POST";
_postRequest.ContentType =
"application/x-vnd.vmware-streamVmdk";
_postRequest.ContentLength = _fileSize;
_requestStream = _postRequest.GetRequestStream();
}
catch (Exception exeption)
{
if (_fileStream != null)
_fileStream.Close();
if (_requestStream != null)
_requestStream.Close();
LoadError = exeption;
return false;
}
return true;
}
private bool
RunUpload()
{
var block = new byte
[BlockSize];
try
{
while (Interlocked.Read(ref
_bytesLoaded) < _fileSize)
{
var
bytesRead = _fileStream.Read(block, 0, BlockSize);
_requestStream.Write(block, 0, bytesRead);
Interlocked.Add(ref
_bytesLoaded, bytesRead);
}
}
catch (Exception
ex)
{
LoadError = ex;
return false
;
}
finally
{
_fileStream.Close();
_requestStream.Close();
}
return true;
}
Any ideas, what can be wrong?