VMware {code} Community
jimmerman
Contributor
Contributor
Jump to solution

VimClient.FindViewEntities Returning null after some amount of time

Hi All,

I am hosting a C# webservice in IIS that makes calls against our vsphere instance to perform certain functions.  One basic call is to get a list of all VMs.  Every so often, the webservice will get in some state in which the call returns null (we always have many VMs up):

List<EntityViewBase> views = c.FindEntityViews(typeof(VirtualMachine), null, null, null);

if (views== null) throw new Exception("List of VMs from VimClient.FindEntityViews is null!");

At this point, I believe that this occurs if the webservice has not been restarted or recycled in some time.  Most likely, I am storing the VimClient incorrectly.

I am caching the VimClient connection in the following way.  My reason for doing so is to save time on calls (it takes roughly 20 seconds to call VimClient.Connect() upon each webservice call if I don't do this).

1) Upon webservice startup, I call

private static VimClient _vsphereClientConnection = null;

_vsphereClientConnection = new VimClient();

ServiceContent sc = _vsphereClientConnection.Connect(vsphereServerURL);

UserSession us = _vsphereClientConnection.Login(vsphereUsername, vspherePassword);

2) I then grab this instance of VimClient whenever I need to perform a function against the vSphere instance

public static VimClient VSphereClientConnection

{

get

{

Initialize.waitForInitialization(0);

return _vsphereClientConnection;

}

}

where Initialize.waitForInitialization() checks whether VimClient has finished connecting to vSphere.

Am I caching this connection improperly?  Is there a better way to do this/a way to do it at all?  Or perhaps I am making a wrong assumption about why FindEntityViews is returning null.

I was thinking that in the property VSphereClientConnection, I could detect whether the connection has timed out in some way and re-connect it, but that is not ideal because then the user would have to wait for initialization again.  I'm also not sure how to check whether the service is in a bad state without making a call that I know does not work in these 'bad' states.

Thanks,

Jason

0 Kudos
1 Solution

Accepted Solutions
stumpr
Virtuoso
Virtuoso
Jump to solution

You can call SessionManager.sessionIsActive() to validate a session.  You could also do some other data query.  Firewalls between you and vCenter can sometimes drop idle connections (vCenter has a configuration value vpxd.httpClientIdleTimeout to increase or decrease and it should be higher than your firewall idle connection drops).

You could also just cache the sessionID (which is basically presented in the cookie).  I'm not sure if the C# toolkit has the hooks to do this easily, however.  Perl does, and it works quite well in web servers.  The logic is get the sessionID, attempt to load the existing session, if unsuccessful, try new login with stored credentials.

Reuben Stump | http://www.virtuin.com | @ReubenStump

View solution in original post

0 Kudos
2 Replies
stumpr
Virtuoso
Virtuoso
Jump to solution

You can call SessionManager.sessionIsActive() to validate a session.  You could also do some other data query.  Firewalls between you and vCenter can sometimes drop idle connections (vCenter has a configuration value vpxd.httpClientIdleTimeout to increase or decrease and it should be higher than your firewall idle connection drops).

You could also just cache the sessionID (which is basically presented in the cookie).  I'm not sure if the C# toolkit has the hooks to do this easily, however.  Perl does, and it works quite well in web servers.  The logic is get the sessionID, attempt to load the existing session, if unsuccessful, try new login with stored credentials.

Reuben Stump | http://www.virtuin.com | @ReubenStump
0 Kudos
jimmerman
Contributor
Contributor
Jump to solution

Thanks!  That is certainly helpful.  The simplest solution for me for now is to just do a data query every 25 minutes to prevent the connection being dropped (I have to do a webservice query every x minutes anyways to keep the service alive as there is no mechanism in IIS 6 to permanently keep a webservice alive).  Works like a charm.  That being said, the c# toolkit dos allow for saving and loading sessions easily.

0 Kudos