VMware {code} Community
daniellynn
Enthusiast
Enthusiast

Importing OVF with C#

I'm importing an OVF and everything goes great if I don't try to import a vmdk with it. If I do, I get the following error:

System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.Exception: Cannot open writestream to server: The underlying connection was closed: Unable to connect to the remote server.

You'd think that's pretty self-explanatory, but from the same machine, I can connect to the server that it's trying to upload to via https successfully. There is a cert complaint when I open it in a browser, but that should be a different error in C#. I've included below the exact code I'm running into this problem with. (this is the section of code that pushes the file up anyway)

String absoluteFile = ovfi.path;
urlToPost = thisDevice.url;

myAPI.service.HttpNfcLeaseProgress(myLease, 15);

try
{
    HttpWebRequest request = null;
    Uri uri = new Uri(urlToPost);
    request = (HttpWebRequest)WebRequest.Create(uri);
    request.Method = "POST";
    request.ContentType = "application / x - vnd.vmware - streamVmdk";
    request.ContentLength = ovfi.size;
    try
    {
        using (System.IO.Stream writeStream = request.GetRequestStream())
        {
            byte[] bytes = new byte[0];
            try
            {
                bytes = readVMDKFile(@"C:\path\to\my\file\78698-258-disk1.vmdk");
            }
            catch (Exception e)
            {
                throw new Exception("Error Reading File: " + e.Message);
            }
            try
            {
                writeStream.Write(bytes, 0, bytes.Length);
            }
            catch (Exception e)
            {
                throw new Exception("Error Writing to stream: " + e.Message);
            }
        }
    }
    catch (Exception e)
    {
        throw new Exception("Cannot open writestream to server: " + e.Message);
    }
}
catch (Exception e)
{
    throw new Exception(e.Message);
}

Reply
0 Kudos
3 Replies
Steve_Jin
Expert
Expert

daniellynn,

Not a direct answer, but I have written two Java samples which you can port to C#.

Good luck!

Steve JIN

Author of VMware VI and vSphere SDK (Prentice Hall)

Creator of open source vSphere (VI) Java API(Tutorial, Testimonials, Download, Samples, Forum)

Blog: DoubleCloud.ORG ( Top 10 Best Practices,[Object Model|http://www.doublecloud.org/2010/02/object-model-of-vmware-vsphere-api-a-big-picture-in-2-minutes/],[Common Mistakes|http://www.doublecloud.org/2010/01/31/common-mistakes-using-vmware-vi-and-vsphere-sdk/], Tiny REST API, Cloud Application Architecture)

Steve JIN Author of VMware VI and vSphere SDK; Creator of open source VI Java API (http://vijava.sf.net); Blogger at http://www.doublecloud.org
daniellynn
Enthusiast
Enthusiast

Thank you. I actually used those as the basis for my c# app.

Luckily, I've been able to move past that problem and a few more since. It didn't like specifying file size for some reason when opening the write stream and then I was filling up the buffer too fast and I had to tell it not to buffer the writes, but we've gotten past that. I'm left with 2 odd issues now, one c#-specific and one not.

1) In C#, it doesn't look like there's a way to check the state of the license. As a temporary measure, I just put in a 10-second sleep, by which time the httpnfclicense state is always "Ready", but it'd obviously be better to be able to actually pull that.

2) I get a around half way through a 700 MB VMDK (when I tested, this occurred at 325mb, 331mb, and 361mb) and the task cancels of its own accord. My first thought was that the license was expiring because in the API reference, it says you have to use HttpNfcLicenseProgress to update it now and then or it'll expire, but I put in an update to update the % every MB, which happens a little more than once per second, and that didn't help. Did you or anyone else out there run into such a problem or do you know any other reason it might cancel itself. I just want to also point out the distinction that, when other errors occurred, the program failed out but the task continued and I would have to manually cancel the task. When this happens, the task in vSphere client changes to the state "The task was canceled by a user."

Thanks for any input.

Reply
0 Kudos
greentim
Contributor
Contributor

Hi

did you have a chance to find solution?

if yes, could you please, to share this?

Thanks for advance

Reply
0 Kudos