So I am trying to put together some short tutorials about using VMWare ESX Web Service together with .NET. These tutorials  are meant to be specific and pointed for a single item.

So, on with the first posting for the ESX web Service API.

Before you dig into the the code below, this code references a Web Service proxy object called VMWareWS. You can create this by adding a web reference to your visual studio project with the following url “http://<your esx server>/sdk/vimService?wsdl”, then naming it VMWareWS.

 

VMWareWS.VimService _service = new VMWareTesting.VMWareWS.VimService();
_service.Url = @"https://your esx server/sdk";

System.Net.ServicePointManager.CertificatePolicy = new TrustAllCerts();

VMWareWS.ManagedObjectReference _object = new VMWareTesting.VMWareWS.ManagedObjectReference();
_object.type = "ServiceInstance";
_object.Value = "ServiceInstance";

VMWareWS.ServiceContent _serviceContent = _service.RetrieveServiceContent(_object);

_service.Login(_serviceContent.sessionManager, "root", "yourpassword", null);

Now, one thing i need to mention here is the following line

System.Net.ServicePointManager.CertificatePolicy = new TrustAllCerts();

This line references a custom class that allows all certificates from the esx server to be trusted. If you omit this line you will receive a certificate exception. Below is the reference class that you will need to create in your project as well. You can call it whatever you want just change the line above to reference your class name.

class TrustAllCerts : System.Net.ICertificatePolicy
    {
        public bool CheckValidationResult(System.Net.ServicePoint SP, System.Security.Cryptography.X509Certificates.X509Certificate X509Cert, System.Net.WebRequest WR, int Value)
        {
            return true;
        }
    }