Search code examples
asp.net.netasp.net-mvcpowershellstack-overflow

An unhandled exception of type 'System.StackOverflowException' occurred in VimService55.XmlSerializers.dll


I am working on an asp.net mvc-5 web application and i am using ap.net version 4.5.

Inside my web application I am executing some power-shell scripts to get some hardware info about some servers and VMs, and get back the results inside my code, as follows:

var shell = PowerShell.Create();
string PsCmd =       
    "add-pssnapin VMware.VimAutomation.Core; $vCenterServer = '" 
    + vCenterName.Trim() + "';$vCenterAdmin = '" + vCenterUsername.Trim() 
    + "' ;$vCenterPassword = '" + vCenterPassword + "';" 
    + System.Environment.NewLine;

PsCmd += "$VIServer = Connect-VIServer -Server $vCenterServer -User $vCenterAdmin -Password $vCenterPassword;" + System.Environment.NewLine;
PsCmd += "Get-VMHost " + System.Environment.NewLine;

shell.Commands.AddScript(PsCmd);

dynamic results = shell.Invoke(); 

var temp_result = results[0].BaseObject == null ? results[0] : results[0].BaseObject;
var otherIdentityInfo = temp_result.ExtensionData.Hardware.SystemInfo.OtherIdentifyingInfo;

now currently when i run this inside my Visual Studio 2012 professional , i will get the following exception :-

System.StackOverflowException was unhandled
An unhandled exception of type 'System.StackOverflowException' occurred in VimService55.XmlSerializers.dll

on

var otherIdentityInfo = temp_result.ExtensionData.Hardware.SystemInfo.OtherIdentifyingInfo;

So can anyone adivce on this? I know that in general a "StackOverflowException" exception is related to the fact that too many data exists inside the stack, but in my case I do not have control over this data as I am scanning VM server information. So can anyone advice on this please?

EDIT

I am not sure what is really raising the error (the debugger OR the code)? because when i try calling this code on the hosted application inside IIS (not inside Visual Studio) I will get null value for the otherIdentityInfo variable, rather than getting an exception. However, when i debug the code inside Visual Studio using Autos i will get the exception, so as @JmaesP mentioned the exception is being raised by the debugger, but not sure how i can debug this value to see why i am getting null??


Solution

  • A stack overflow exception from an XML serializer might indicate an issue with one of you serializable types. If the type declaration by itself is somewhat recursice, the default XML serializer will end up in inifite recursion. Consider this example:

    [Serializable()]
    public class Foo : IEnumerable<Foo>
    {
        public Foo()
        {
        }
    
        IEnumerator IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }
    
        public IEnumerator<Foo> GetEnumerator()
        {
            throw new NotImplementedException();
        }
    
        public void Add(Foo item)
        {
            throw new NotImplementedException();
        }
    }
    

    The default XML serializer first tries to figure out how to (de-)serialize an instance of Foo, then it tries to figure out how to (de-)serialize an IEnumerable<Foo>, and to do that it tries to figure out how to (de-)serialize a Foo — resulting in infinite recursion. Typically, a solution to this would to use a custom serializer as described here.

    However, in your case the serializer is provided by the third-party component. The exception likely occurs during the serialization/deserialization that is happening when objects are passed from the PowerShell session to your process. So what you could to is to change the object that is returned from the PowerShell script. Instead of returning a VMHost object (requiring to serialize the entire object tree of the VMHost object), you could just return the SystemInfo or OtherIdentifyingInfo.