繁体   English   中英

从 MSMQ Outqueue 获取消息计数时出现问题

[英]Issue while Getting message count from MSMQ Outqueue

我正在尝试获取传出队列中存在的消息计数。 我正在使用 C# 和 Powershell 来实现这一点。 我正在使用以下命令

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);

我通过授予所有权限尝试了此代码,但它给出了相同的例外。

通过在 Startup 标签中添加以下 useLegacyV2RuntimeActivationPolicy 解决了该问题。

<configuration>
 <startup useLegacyV2RuntimeActivationPolicy="true">
  <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6"/>
 </startup>
</configuration>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM