尝试暂停/恢复传出消息队列时获取访问被拒绝错误

时间:2017-09-27 08:10:20

标签: vb.net msmq access denied

Imports System.Messaging
Imports System.Collections
Imports MSMQ
Imports System.IO
Imports System
Imports System.Messaging.MessageQueue
Imports System.Runtime.InteropServices

Public Class PauseOutMessages
    'Declare everything to be in the scope of all methods.
    Dim mgmt As New MSMQManagement
    Dim outqmgmt As MSMQOutgoingQueueManagement
    Dim q As New MSMQApplication
    Dim outgoingQueues As New ArrayList
    Dim myQueue As New MessageQueue("FormatName:DIRECT=OS:myMachine\Private$\myQueue", QueueAccessMode.ReceiveAndAdmin)

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        For Each queue In q.ActiveQueues
            If queue.IndexOf("DIRECT=") >= 0 Then
                outgoingQueues.Add(queue)
            End If
        Next

    End Sub

    Private Sub Pause_Click(sender As Object, e As EventArgs) Handles Pause.Click
        For Each queuePath In outgoingQueues
            mgmt.Init(FormatName:=queuePath)
            outqmgmt = mgmt.Pause()
        Next
    End Sub

    Private Sub Restart_Click(sender As Object, e As EventArgs) Handles Restart.Click
        For Each queuePath In outgoingQueues
            mgmt.Init(FormatName:=queuePath)
            outqmgmt = mgmt.Resume()
        Next
    End Sub

    Private Sub Send_Click(sender As Object, e As EventArgs) Handles Send.Click
        myQueue.Send("Test")
        For Each queue In q.ActiveQueues
            If queue.IndexOf("DIRECT=") >= 0 Then
                outgoingQueues.Add(queue)
            End If
        Next
    End Sub
End Class

这是我正在使用的代码,通过将测试消息发送到不存在的路径,它会卡在传出队列中,我希望能够调用MSMQOutgoingQueueManagement.Pause或.Resume能够启动和停止所有传出的队列。

但是我一直在mgmt.Pause()或mgmt.Resume()上收到错误,说Access被拒绝。我似乎无法找到一种方法来获取传出队列的属性,以便能够调整安全设置。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

解决!

原来我只需要以管理员的身份启动visual studio,然后就可以了。