简体   繁体   中英

Issue while Getting message count from MSMQ Outqueue

I'm trying to get the count of messages present in outgoing queue. I'm using C# with Powershell to achieve this. I'm using below command

Get-MsmqOutgoingQueue | Format-Table -Property MessageCount

This command is getting executed successfully on powershell command prompt. But when I try this from C#, it is giving following exception:

The type initializer for 'Microsoft.Msmq.PowerShell.Commands.Utilities' threw an exception.

Below is the C# code that I'm using to execute this command:

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

I tried this code by giving all permission but it's giving same exception.

Fixed the issue by adding following useLegacyV2RuntimeActivationPolicy in Startup tag.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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