VimService55.XmlSerializers.dll中发生未处理的"System.StackOverflowException"类型异常

joh*_* Gu 6 .net asp.net stack-overflow asp.net-mvc powershell

我正在使用asp.net mvc-5 Web应用程序,我正在使用ap.net 4.5版.

在我的Web应用程序中,我正在执行一些power-shell脚本以获取有关某些服务器和VM的硬件信息,并在我的代码中获取结果,如下所示:

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;
Run Code Online (Sandbox Code Playgroud)

现在,当我在我的Visual Studio 2012专业版中运行时,我将得到以下异常: -

System.StackOverflowException was unhandled
An unhandled exception of type 'System.StackOverflowException' occurred in VimService55.XmlSerializers.dll
Run Code Online (Sandbox Code Playgroud)

var otherIdentityInfo = temp_result.ExtensionData.Hardware.SystemInfo.OtherIdentifyingInfo;
Run Code Online (Sandbox Code Playgroud)

任何人都可以这样做吗?我知道一般来说,"StackOverflowException"异常与堆栈中存在太多数据这一事实有关,但在我的情况下,由于我正在扫描VM服务器信息,因此无法控制此数据.所以有人可以就此提出建议吗?

编辑

我不确定是什么引起了错误(调试器或代码)?因为当我尝试在IIS内部的托管应用程序上调用此代码(不在Visual Studio中)时,我将获得otherIdentityInfo变量的空值,而不是获得异常.但是,当我使用Autos在Visual Studio中调试代码时,我将获得异常,因为@JmaesP提到调试器正在引发异常,但不知道我如何调试此值以查看为什么我得到null?

Dir*_*mar 3

XML 序列化程序的堆栈溢出异常可能表明其中一种可序列化类型存在问题。如果类型声明本身有点递归,则默认的 XML 序列化程序将导致无限递归。考虑这个例子:

\n\n
[Serializable()]\npublic class Foo : IEnumerable<Foo>\n{\n    public Foo()\n    {\n    }\n\n    IEnumerator IEnumerable.GetEnumerator()\n    {\n        throw new NotImplementedException();\n    }\n\n    public IEnumerator<Foo> GetEnumerator()\n    {\n        throw new NotImplementedException();\n    }\n\n    public void Add(Foo item)\n    {\n        throw new NotImplementedException();\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

默认的 XML 序列化程序首先尝试找出如何(反)序列化 的实例Foo,然后尝试找出如何(反)序列化 一个IEnumerable<Foo>,然后尝试找出如何(反)序列化序列化Foo\xe2\x80\x94 导致无限递归。通常,解决此问题的方法是使用自定义序列化器,如此处所述

\n\n

但是,在您的情况下,序列化器是由第三方组件提供的。当对象从 PowerShell 会话传递到进程时发生的序列化/反序列化过程中可能会发生异常。因此,您可以更改从 PowerShell 脚本返回的对象。您可以只返回or ,而不是返回VMHost对象(需要序列化对象的整个对象树VMHost)。SystemInfoOtherIdentifyingInfo

\n