Hi
We are using the VI SDK 2.5 from Windows XP SP3 to connect to vCenter to QueryPerf to get the perf data.
We use a specific user account to connect to the vCenter.
In certains situations, we notice that there are some 'idle' sessions left hanging around. I see them listed in the vCenter's Administration/Management/Sessions console. I think these 'idle sessions' end up when the app is connected to vCenter via the API and attempts a 'logout' and the logout fails for some reason. It is also possible, I think, when the application terminates abnormally. I noticed a case when the 'idle' session is terminated after 30 mins of inactivity.
Is there anyway to make the API use the same session or inform it not to create a new session if one already exists for the connecting user? Something similar to how a SQL Server would do 'connection pooling'?
I appreciate any input in this matter.
Regards
I hope my blog can help you to understand the session management:
http://www.doublecloud.org/2010/02/the-mythical-sessions-in-vsphere-and-vi/
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)
I hope my blog can help you to understand the session management:
http://www.doublecloud.org/2010/02/the-mythical-sessions-in-vsphere-and-vi/
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)
Thank you Steve. Your blog was extremely useful in finding a solution.
For those who is looking for a sample code, here is what I have... that I adapted from your blog and also referring to sample code from Schley Andrew Kutz at -
private void ConnectToServer()
{
svcRef = new ManagedObjectReference();
svcRef.type = "ServiceInstance";
svcRef.Value = "ServiceInstance";
vservice = new VimService();
vservice.Url = "https://" + textBox2.Text+"/sdk";
vservice.CookieContainer = new System.Net.CookieContainer();
bool attemptLogin = true;
try
{
System.Net.Cookie savedSession = LoadSessionState(VcenterSessionFilePathName);
if (savedSession != null)
{
vservice.CookieContainer.Add(savedSession);
sic = vservice.RetrieveServiceContent(svcRef);
if (GetCurrentUserSession() != null)
{
// The last session is still valid - but probably inactive.
// And, we successfully 'rehydrated' the saved session state.
// Any calls into vcenter will use the last session.
attemptLogin = false;
}
}
}
catch (Exception ex)
{
}
if (attemptLogin)
{
vservice.CookieContainer = new System.Net.CookieContainer();
sic = vservice.RetrieveServiceContent(svcRef);
vservice.Login(sic.sessionManager, textBox3.Text, textBox4.Text, null);
SaveSessionState(VcenterSessionFilePathName);
}
}
private System.Net.Cookie LoadSessionState(string pathToSessionFile)
{
if (File.Exists(pathToSessionFile) == false)
{
return null;
}
System.Net.Cookie retVal = null;
BinaryFormatter bf = new BinaryFormatter();
{
using (FileStream s = File.OpenRead(pathToSessionFile))
{
retVal = (System.Net.Cookie)bf.Deserialize(s);
s.Close();
}
}
return retVal;
}
private void SaveSessionState(string pathToSessionFile)
{
System.Net.CookieCollection cc = vservice.CookieContainer.GetCookies(new Uri(vservice.Url));
if (cc.Count <= 0 || cc[0].Name.Equals("vmware_soap_session", StringComparison.InvariantCultureIgnoreCase) == false)
{
return;
}
BinaryFormatter bf = new BinaryFormatter();
{
using (FileStream s = File.Open(pathToSessionFile, FileMode.Create))
{
bf.Serialize(s, cc[0]);
s.Close();
}
}
}
private UserSession GetCurrentUserSession()
{
int ret = -1;
// First define what data we want back from the filter
// by creating a PropertySpec
PropertySpec pSpec = new PropertySpec();
pSpec.all = false;
pSpec.type = sic.sessionManager.type;
pSpec.pathSet = new String[] { "currentSession" };
ObjectSpec oSpec = new ObjectSpec();
oSpec.obj = sic.sessionManager;
oSpec.skip = false;
// Now put the whole thing together in the PropertyFilterSpec
PropertyFilterSpec pfSpec = new PropertyFilterSpec();
pfSpec.propSet = new PropertySpec[] ;
pfSpec.objectSet = new ObjectSpec[] ;
ManagedObjectReference pcRef = sic.propertyCollector;
ObjectContent[http://] ocary = vservice.RetrieveProperties(pcRef, new PropertyFilterSpec[|http://] ocary = vservice.RetrieveProperties(pcRef, new PropertyFilterSpec[] );
if (ocary != null)
{
if (ocary.Length > 0)
{
ObjectContent oc = (ObjectContent)ocary[0];
if (oc.propSet != null)
{
if (oc.propSet.Length > 0)
{
if (oc.propSet[0].name.Equals("currentSession"))
{
UserSession us = (UserSession)oc.propSet[0].val;
return us;
}
}
}
}
}
return null;
}