我正在开发一个 asp.net mvc-5 Web 应用程序,并且使用的是 ap.net 版本 4.5。
在我的 Web 应用程序中,我正在执行一些 power-shell 脚本来获取有关某些服务器和虚拟机的一些硬件信息,并在我的代码中返回结果,如下所示:
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;
现在,当我在 Visual Studio 2012 Professional 中运行此命令时,我将收到以下异常:-
System.StackOverflowException was unhandled
An unhandled exception of type 'System.StackOverflowException' occurred in VimService55.XmlSerializers.dll
上
var otherIdentityInfo = temp_result.ExtensionData.Hardware.SystemInfo.OtherIdentifyingInfo;
有人可以对此提出建议吗?我知道,一般来说,“StackOverflowException”异常与堆栈内存在太多数据有关,但就我而言,当我扫描虚拟机服务器信息时,我无法控制这些数据。那么有人可以就此提出建议吗?
编辑
我不确定真正引发错误的是什么(调试器或代码)?因为当我尝试在 IIS 内(而不是 Visual Studio 内)的托管应用程序上调用此代码时,我将获得 otherIdentityInfo
变量的空值,而不是获得异常。但是,当我使用 Autos 调试 Visual Studio 内的代码时,我会收到异常,因此正如 @JmaesP 提到的异常是由调试器引发的,但不确定如何调试这个值以查看为什么我得到 null?
最佳答案
来自 XML 序列化程序的堆栈溢出异常可能表明其中一种可序列化类型存在问题。如果类型声明本身有点递归,则默认的 XML 序列化程序将导致无限递归。考虑这个例子:
[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();
}
}
默认的 XML 序列化程序首先尝试找出如何(反)序列化 Foo
的实例。 ,然后它尝试找出如何(反)序列化 IEnumerable<Foo>
,并为此尝试找出如何(反)序列化 Foo
- 导致无限递归。通常,解决此问题的方法是使用自定义序列化器,如here所述。 。
但是,在您的情况下,序列化器是由第三方组件提供的。当对象从 PowerShell session 传递到进程时发生的序列化/反序列化过程中可能会发生异常。因此,您可以更改从 PowerShell 脚本返回的对象。而不是返回 VMHost
对象(需要序列化 VMHost
对象的整个对象树),您可以只返回 SystemInfo
或OtherIdentifyingInfo
.
关于asp.net - VimService55.XmlSerializers.dll 中发生类型 'System.StackOverflowException' 的未处理异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40810098/