C# 从MSMQ输出队列获取消息计数时出现问题
我正在尝试获取传出队列中存在的邮件数。我使用C#with Powershell来实现这一点。 我正在使用下面的命令C# 从MSMQ输出队列获取消息计数时出现问题,c#,powershell,msmq,C#,Powershell,Msmq,我正在尝试获取传出队列中存在的邮件数。我使用C#with Powershell来实现这一点。 我正在使用下面的命令 Get-MsmqOutgoingQueue | Format-Table -Property MessageCount 此命令正在powershell命令提示符下成功执行。但当我从C#尝试这个时,它给出了以下异常: The type initializer for 'Microsoft.Msmq.PowerShell.Commands.Utilities' threw an ex
Get-MsmqOutgoingQueue | Format-Table -Property MessageCount
此命令正在powershell命令提示符下成功执行。但当我从C#尝试这个时,它给出了以下异常:
The type initializer for 'Microsoft.Msmq.PowerShell.Commands.Utilities' threw an exception.
下面是我用来执行此命令的C代码:
string scriptTest = "Get-MsmqOutgoingQueue | Format-Table -Property MessageCount";
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(scriptText);
pipeline.Commands.Add("Out-String");
Collection<PSObject> results = pipeline.Invoke();
runspace.Close();
string s = "";
foreach (PSObject obj in results)
{
s = obj.ToString();
}
Console.WriteLine(s);
string scriptTest=“Get-MsmqOutgoingQueue | Format Table-Property-MessageCount”;
Runspace Runspace=RunspaceFactory.CreateRunspace();
Open();
Pipeline Pipeline=runspace.CreatePipeline();
pipeline.Commands.AddScript(scriptText);
pipeline.Commands.Add(“输出字符串”);
收集结果=pipeline.Invoke();
runspace.Close();
字符串s=“”;
foreach(结果中的PSObject对象)
{
s=对象ToString();
}
控制台。写入线(s);
我通过授予所有权限尝试了这段代码,但它给出了相同的异常。通过在启动标记中添加以下useLegacyV2RuntimeActivationPolicy修复了此问题
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6"/>
</startup>
</configuration>
是否存在InnerException?选中InnerException并发现异常为:“混合模式程序集是根据运行时的版本“v2.0.50727”构建的,如果没有其他配置信息,则无法在4.0运行时中加载”通过修改App.Config将其修复。