为什么在使用Powershell方法时,MSMQ上的计数消息返回0



我正在开发一个在 IIS 上运行的 WCF 服务,我需要计算 MSMQ 中每个专用队列的消息 i = 。最快的方法似乎是Powershell方法。

基准测试在这里:

http://www.codeproject.com/Articles/346575/Message-Queue-Counting-Comparisions)

在Visual Studio 2012上进行调试时,它工作得很好,但是当部署在我的本地IIS 7.5服务器上时,它返回0。

这是我正在使用的方法:

private int GetPowerShellCount()
{
    return GetPowerShellCount(".\private$pingpong", Environment.MachineName, "", "");
}
private  int GetPowerShellCount(string queuePath, string machine,string username, string password)
{
    var path = string.Format(@"\{0}rootCIMv2", machine);
    ManagementScope scope;
    if (string.IsNullOrEmpty(username))
    {
            scope = new ManagementScope(path);
    }
    else
    {
        var options = new ConnectionOptions {Username = username, Password = password};
        scope = new ManagementScope(path, options);
    }
    scope.Connect();
    if (queuePath.StartsWith(".\")) queuePath=queuePath.Replace(".\",string.Format("{0}\",machine));
    string queryString = String.Format("SELECT * FROM Win32_PerfFormattedData_msmq_MSMQQueue");
    var query = new ObjectQuery(queryString);
    var searcher = new ManagementObjectSearcher(scope, query);
    IEnumerable<int> messageCountEnumerable =
        from ManagementObject queue in searcher.Get()
        select (int)(UInt64)queue.GetPropertyValue("MessagesInQueue");
    //IEnumerable<string> messageCountEnumerable =
    //  from ManagementObject queue in searcher.Get()
    //  select (string)queue.GetPropertyValue("Name");
    var x = messageCountEnumerable.First();
    return x;
}

请注意,我没有使用用户/传递参数,所以它都是本地的(WCF 服务和 MSMQ 在同一台机器上)。

为什么它在部署到 IIS 时返回 0?你认为我应该尝试什么?

您的问题可能与 IIS 特定行为有关。

看看Tom Hollander的博客:MSMQ,WCF和IIS:让他们玩得漂亮(第1部分)

通常,消息队列可以随心所欲地调用。然而 在 IIS 7 中承载启用 MSMQ 的服务时,队列 名称必须与服务的 .svc 文件的 URI 匹配。在此示例中 我们将在名为 MsmqService 的应用程序中托管该服务 使用名为 MsmqService.svc 的 .svc 文件,因此队列必须 称为MsmqService/MsmqService.svc

最新更新