VMware {code} Community
vmcts
Contributor
Contributor

Unable to perform operation when .Net applications is deployed on IIS

Hi,

I am working on vmware vi sdk to perform little funtionalities such as getting inventory details, power-on,power-off,create VM etc. in C# .Net framework. Now i had the functionalities available which i want to provide it to the front end team. So in order to achieve that i wrote web-services in C# .Net framework for different operation(power-on and power-off) and executed the service and the services are worked fine.

Now the problem comes here when i deployed these services to the IIS(Interenet Information server) but i am not able to use these services through IIS. Infact i am not getting any error also. My concern is the same service if i try to run through Microsoft Visual Studio i am getting the output. But as soon as i deploy the service on IIS , the service did not work. First i thought this could be a problem with IIS configuration. But when i created the simple "Hello World" web-service and deployed in IIS, the front-end team was able to acccess. So i concluded that there is no problem in IIS configuration.

So i am just thinking now the IIS is not able to validate the vmware certificate policy which is populated during the login to the esx server. Please provide your inputs. can anyone tell me how should i go about it.

Regards

Pardeep Singh

0 Kudos
4 Replies
ssbangal
Contributor
Contributor

Hi Pardeep,

I just tried the same and i could get it working. It has got to do with the certificates. Please try adding the following code into your page load and create a function copied below code snippet. Hopefully it should work. BTW, you need to include using System.Net.Security & using System.Security.Cryptography.X509Certificates;

Add in page load: System.Net.ServicePointManager.ServerCertificateValidationCallback = TrustAllCertificateCallback; (this will accept all the certificates)

Function:

public static bool TrustAllCertificateCallback(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors errors)

{

return true;

}

Regards,

Sudhir

0 Kudos
vmcts
Contributor
Contributor

Thanks ssbangal. It worked for me as well. Smiley Happy

Regards

Pardeep Singh

0 Kudos
vmcts
Contributor
Contributor

Hi Sudhir ,

I am working on vmware vi sdk to perform certain functionality. I am using clonevm api to clone vm in virtual center. The clone api works fine if i clone in the same ESXServer but if try to clone from one ESX Server to other ESX Server i got "operation timed out" error for System.Net.WebException however the clone works fine in this case also. The same problem i found during migrating the vm from one ESX Server to other. Please help me out to overcome the "operation timed out ", code for clone and waitforvalue are as follows: Please find the attchment also.

public bool CloneVM(string vmType, string srcName, string clonedName, string dstName, string datastore)

{

//string vcVersion = "2.5.0";

ManagedObjectReference vmFolderRef = new ManagedObjectReference();

ManagedObjectReference HSFolderRef = new ManagedObjectReference();

ManagedObjectReference[] DSPoolRef = null;

ManagedObjectReference resourcePoolRootRef = new ManagedObjectReference();

ManagedObjectReference datacenterRef = new ManagedObjectReference();

if (_ServiceContent == null)

{

_bConnection = false;

//Connect();

}

Debug.Assert(_ServiceContent != null);

if (_bVirtualCenter == true)

{

//MOR for searching datacenter in inventory

ManagedObjectReference searchIndex = new ManagedObjectReference();

searchIndex = _ServiceContent.searchIndex;

ManagedObjectReference dcmor = GetFirstDecendentMoRef(null, "Datacenter");

ObjectContent[] dcName = GetObjectProperties(dcmor, new string[http://communities.vmware.com/community-document-picker.jspa?communityID=&subject=%5DdcName%3DGetObjectProperties%28dcmor%2Cnew+string%5B] { "name" });

//System.Console.WriteLine(dcName[0].propSet[0].val);

_DatacenterName = (string)dcName[0].propSet[0].val;

datacenterRef = VS.FindByInventoryPath(searchIndex, _DatacenterName);

if (datacenterRef == null)

{

System.Console.WriteLine("The specified datacenter is not found");

return false;

}

// Find the virtual machine and Host folder for this datacenter.

vmFolderRef = (ManagedObjectReference)getObjectProperty(datacenterRef, "vmFolder");

HSFolderRef = (ManagedObjectReference)getObjectProperty(datacenterRef, "hostFolder");

DSPoolRef = (ManagedObjectReference[])getObjectProperty(datacenterRef, "datastore");

}

// Find the virtual machine list from the virtual machine folder found above.

ManagedObjectReference[] vmList = (ManagedObjectReference[http://communities.vmware.com/community-document-picker.jspa?communityID=&subject=%5DvmList%3D+%28ManagedObjectReference%5B])getObjectProperty(vmFolderRef, "childEntity");

ManagedObjectReference[] hsList = (ManagedObjectReference[http://communities.vmware.com/community-document-picker.jspa?communityID=&subject=%5DhsList%3D+%28ManagedObjectReference%5B])getObjectProperty(HSFolderRef, "childEntity");

//Searching for the template/virtual machine

ManagedObjectReference templateRef = null;

//for (int i = 0; i < vmList.Length; i++)

for (int i = 0; i < vmList.Length; i++)

{

if (vmList.type == "VirtualMachine")

+

{

Object[] vmProps = getProperties(vmList[i], new String[http://communities.vmware.com/community-document-picker.jspa?communityID=&subject=i%5D%2CnewString%5B] { "config.name", "config.template" });

if (((String)vmProps[0]).Equals(clonedName))

{

System.Console.WriteLine("Destination Already Present!");

return false;

}

}

}

for (int i = 0; i < vmList.Length; i++)

{

if (vmList+.type == "VirtualMachine")

{

Object[] vmProps = getProperties(vmList[i], new String[http://communities.vmware.com/community-document-picker.jspa?communityID=&subject=i%5D%2CnewString%5B] { "config.name", "config.template" });

if (vmProps[0].Equals(srcName))

{

templateRef = vmLis

+

if (String.Compare(_vcVersion, vcVersion) < 0)

{

ObjectContent[] runtime = GetObjectProperties(templateRef, new string[http://communities.vmware.com/community-document-picker.jspa?communityID=&subject=%5Druntime%3DGetObjectProperties%28templateRef%2Cnew+string%5B] { "runtime" });

VirtualMachineRuntimeInfo stateinfo = ((VirtualMachineRuntimeInfo)runtime[0].propSet[0].val);

string testPower = stateinfo.powerState.ToString();

if (testPower.Equals("poweredOn"))

{

System.Console.WriteLine("Can't perform Clone Opeartion in PowerON State");

return false;

}

}

}

}

}

//Searching for the Host

ManagedObjectReference hosRef = null;

for (int i = 0; i < hsList.Length; i++)

{

if (hsList+.type == "Compu

++

{

Object[] hsProps = getProperties(hsList[i], new String[http://communities.vmware.com/community-document-picker.jspa?communityID=&subject=i%5D%2CnewString%5B] { "name" });

if (((String)hsProps[0]).Equals(dstName))

{

ho

+++
}


}


}


////Searching for the Datastore


ManagedObjectReference dosRef = null;


for (int i = 0; i < DSPoolRef.Length; i++)


{

if (DSPoolRef<<br />++++

{

Object[] dsProps = getProperties(DSPoolRef[i], new String[http://communities.vmware.com/community-document-picker.jspa?communityID=&subject=i%5D%2CnewString%5B] { "summary.name" });

if (((String)dsProps[0]).Equals(datastore))

+++++

}

}

}

//Create VirtualMachineCloneSpec & VirtualMachineRelocateSpec

VirtualMachineCloneSpec cloneSpec = new VirtualMachineCloneSpec();

VirtualMachineRelocateSpec relocSpec = new VirtualMachineRelocateSpec();

VirtualMachineConfigInfo vmConfigInfo = (VirtualMachineConfigInfo)getObjectProperty(templateRef, "config");

//if (vmConfigInfo.template.Equals(true))

//{

VirtualMachineRuntimeInfo vmRuntimeInfo = (VirtualMachineRuntimeInfo)getObjectProperty(templateRef, "runtime");

// getting the Managed Object Reference of host of the src template

ManagedObjectReference hostRef = vmRuntimeInfo.host;

if (hostRef == null)

{

System.Console.WriteLine("Host not found for the template");

}

else

{

// Find the first cluster compute resource in this datacenter.

ManagedObjectReference[] crList = (ManagedObjectReference[http://communities.vmware.com/community-document-picker.jspa?communityID=&subject=%5DcrList%3D+%28ManagedObjectReference%5B])getObjectProperty(HSFolderRef, "childEntity");

ManagedObjectReference computeResourceRef = null;

for (int i = 0; i &lt; crList.Length; i++)

{

Object[] hsProps1 = getProperties(crList[i], new String[http://communities.vmware.com/community-document-picker.jspa?communityID=&subject=i%5D%2CnewString%5B] { "name" });

if (crList+.type.CompareTo("ComputeResource") == 0 && ((

++++++
+++++++

}

}

resourcePoolRootRef = (ManagedObjectReference)getObjectProperty(computeResourceRef, "resourcePool");

if (resourcePoolRootRef == null)

{

System.Console.WriteLine("Resource pool not found for the template");

return false;

}

else

{

// Assigning the root resource pool of host of the src template as pool

relocSpec.pool = resourcePoolRootRef;

relocSpec.datastore = dosRef;

}

}

//}

cloneSpec.location = relocSpec;

cloneSpec.powerOn = false;

if (vmType.Equals("clone"))

{

cloneSpec.template = false;

}

else if (vmType.Equals("temp"))

{

cloneSpec.template = true;

}

// Create VirtualMachineConfigSpec

VirtualMachineConfigSpec configSpec = new VirtualMachineConfigSpec();

cloneSpec.config = configSpec;

System.Console.WriteLine("Launching clone task to create a clone: " + clonedName);

// Performing CloneVM_Task

ManagedObjectReference cloneTask = ServiceContent.Service.CloneVMTask(templateRef, vmFolderRef, clonedName, cloneSpec);

// If we get a valid task reference, monitor the task for success or failure and report task completion or failure.

if (cloneTask != null)

{

Console.WriteLine("Got Valid Task Reference");

object[] result =

WaitForValues(cloneTask, new string[] { "info.state", "info.error" },

new string[] { "state" }, // info has a property - state for state of the task

new object[][http://communities.vmware.com/community-document-picker.jspa?communityID=&subject=%5D%5B] { new object[] { TaskInfoState.success, TaskInfoState.error} });

// Wait till the task completes.

if (result[0].Equals(TaskInfoState.success))

{

Console.WriteLine("Successfully Cloned VM ");

return true;

}

else

{

Console.WriteLine("Failed to Cloned VM ");

return false;

}

}

return true;

}

public object[] WaitForValues(

ManagedObjectReference objmor, string[] filterProps,

string[] endWaitProps, object[][] expectedVals

)

{

m_Log.TraceEvent(TraceEventType.Verbose, (int)LogTraceEventType.Verbose, "Start CESXServerProxy::WaitForValues()");

// version string is initially null

string version = "";

object[] endVals = new object[http://endWaitProps.Length|http://endWaitProps.Length];

object[] filterVals = new object[http://filterProps.Length|http://filterProps.Length];

PropertyFilterSpec spec = new PropertyFilterSpec();

spec.objectSet = new ObjectSpec[] { new ObjectSpec() };

spec.objectSet[0].obj = objmor;

spec.propSet = new PropertySpec[] { new PropertySpec() };

spec.propSet[0].pathSet = filterProps;

spec.propSet[0].type = objmor.type;

spec.objectSet[0].selectSet = null;

spec.objectSet[0].skip = false;

spec.objectSet[0].skipSpecified = true;

ManagedObjectReference filterSpecRef =

_ServiceContent.Service.CreateFilter(

_ServiceContent.Service.PropertyCollector, spec, true

);

bool reached = false;

UpdateSet updateset = null;

//updateset=null;

PropertyFilterUpdate[] filtupary = null;

PropertyFilterUpdate filtup = null;

ObjectUpdate[] objupary = null;

ObjectUpdate objup = null;

PropertyChange[] propchgary = null;

PropertyChange propchg = null;

Thread.Sleep(360000);

while (!reached)

{

updateset =

_ServiceContent.Service.WaitForUpdates(

_ServiceContent.Service.PropertyCollector, version

);

version = updateset.version;

if (updateset == null || updateset.filterSet == null)

{

continue;

}

// Make this code more general purpose when PropCol changes later.

filtupary = updateset.filterSet;

filtup = null;

for (int fi = 0; fi &lt; filtupary.Length; fi++)

{

filtup = filtupary[fi];

objupary = filtup.objectSet;

objup = null;

propchgary = null;

for (int oi = 0; oi &lt; objupary.Length; oi++)

{

objup = objupary[oi];

// TODO: Handle all "kind"s of updates.

if (objup.kind == ObjectUpdateKind.modify ||

objup.kind == ObjectUpdateKind.enter ||

objup.kind == ObjectUpdateKind.leave

)

{

propchgary = objup.changeSet;

for (int ci = 0; ci &lt; propchgary.Length; ci++)

{

propchg = propchgary[ci];

UpdateValues(endWaitProps, endVals, propchg);

UpdateValues(filterProps, filterVals, propchg);

}

}

}

}

object expctdval = null;

// Check if the expected values have been reached and exit the loop if done.

// Also exit the WaitForUpdates loop if this is the case.

for (int chgi = 0; chgi &lt; endVals.Length && !reached; chgi++)

{

for (int vali = 0; vali &lt; expectedVals[chgi].Length && !reached; vali++)

{

expctdval = expectedVals[chgi][vali];

reached = expctdval.Equals(endVals[chgi]) || reached;

}

}

}

// Destroy the filter when we are done.

_ServiceContent.Service.DestroyPropertyFilter(filterSpecRef);

m_Log.TraceEvent(TraceEventType.Verbose, (int)LogTraceEventType.Verbose, "Finish CESXServerProxy::WaitForValues()

0 Kudos
rkamal
VMware Employee
VMware Employee

Try Setting the VimService.Timeout to a max value.

0 Kudos