.net - 如何以编程方式设置私有(private) MSMQ 队列的权限设置以进行远程访问?

标签 .net ironpython msmq message-queue

作为我正在开发的应用程序的一部分,我尝试利用 Microsoft 消息队列功能,但在权限设置方面遇到了问题。

我想创建一个队列并向服务器计算机上的队列发送消息,并通过本地网络从客户端计算机访问它。两台计算机(均为 Windows 7 Home Premium)位于同一 LAN 网络上,并且能够相互 ping 通。我会知道创建的队列的确切路径,所以我想私有(private)队列是可以的。

我的代码基于 this example并使用 IronPython 访问 MessageQueue class来自 System.Messaging 命名空间。该示例中的发送和接收设置用于创建一个队列并在一个控制台中向其发送一条消息:

>>> localQueue = MessageQueue.Create('.\\private$\\testQueue')
>>> localQueue.FormatName
'DIRECT=OS:<clientMachineName>\\private$\\testQueue'
>>> localQueue.Send('hello from other console')

然后通过以下代码访问队列并在另一个控制台中查看它:

>>> SemiRemoteQueue = MessageQueue('FormatName:Direct=OS:<clientMachineName>\\private$\\testQueue')
>>> SemiRemoteQueue.Peek()
<System.Messaging.Message object at 0x000000000000002F [System.Messaging.Message
]>

当我在服务器计算机上创建新队列时,我似乎能够与客户端计算机建立连接,但无法从中获取任何消息数据。从客户端查看在服务器上创建的队列,会出现以下“访问被拒绝”错误消息:

>>> ReallyRemoteQueue = MessageQueue('FormatName:Direct=OS:<serverMachineName>\\private$\\remoteTestQueue')
>>> ReallyRemoteQueue.Peek()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
EnvironmentError: System.Messaging.MessageQueueException (0x80004005): Access to
 Message Queuing system is denied.
   at System.Messaging.MessageQueue.MQCacheableInfo.get_ReadHandle()
   at System.Messaging.MessageQueue.StaleSafeReceiveMessage(UInt32 timeout, Int3
2 action, MQPROPS properties, NativeOverlapped* overlapped, ReceiveCallback rece
iveCallback, CursorHandle cursorHandle, IntPtr transaction)
   at System.Messaging.MessageQueue.ReceiveCurrent(TimeSpan timeout, Int32 actio
n, CursorHandle cursor, MessagePropertyFilter filter, MessageQueueTransaction in
ternalTransaction, MessageQueueTransactionType transactionType)
   at System.Messaging.MessageQueue.Peek()
   at Microsoft.Scripting.Interpreter.FuncCallInstruction`2.Run(InterpretedFrame
 frame)
   at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
   at Microsoft.Scripting.Interpreter.LightLambda.Run3[T0,T1,T2,TRet](T0 arg0, T
1 arg1, T2 arg2)
   at IronPython.Compiler.Ast.CallExpression.Invoke0Instruction.Run(InterpretedF
rame frame)
   at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
   at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 a
rg1)
   at IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx)
   at IronPython.Compiler.PythonScriptCode.Run(Scope scope)
   at IronPython.Hosting.PythonCommandLine.<>c__DisplayClass1.<RunOneInteraction
>b__0()

我发现应该可以访问远程私有(private) MSMQ 队列,但不知道如何设置正确的权限。我已在防火墙设置中允许 MSMQ,甚至尝试关闭防火墙。找到了一些有关将权限设置应用于队列文件的讨论,但这没有帮助,因为我没有具有适用于它们的安全设置的队列文件。我还没有按照建议尝试完全访问“匿名登录”的选项,但最终希望以编程方式(在代码中)设置权限,而不是在文件的上下文菜单中。我应该使用 MessageQueing 类 (http://msdn.microsoft.com/en-us/library/dd48yz36(v=vs.80)) 中的 SetPermissions 方法吗?如何指定其参数?

感谢您的建议!

最佳答案

localQueue.SetPermissions("Everyone", MessageQueueAccessRights.FullControl, AccessControlEntryType.Allow);

“Everyone”可以是任何域帐户,例如“Domain\MyUserName”

关于.net - 如何以编程方式设置私有(private) MSMQ 队列的权限设置以进行远程访问?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11094055/

相关文章:

.net - 重新打开 ServiceHost 时出现 WCF "Instance already exists in CounterSet"错误

c# - 使用 IronPython 针对 .NET 程序集进行编码的问题,特别是在 app.config 方面

python - 为什么 id( )'s result on IronPython equal to id()' s 在 Python 上没有结果?

Python - 从不同包中的一个包创建类的对象

c# - 当 MSMQ 服务器重新启动时,远程 MSMQ 连接显然丢失问题

.net - 如何使用 Entity Framework 实现跨 DBMS 插入忽略?

.net - 基于字符串值在 .NET 中创建方法调用

c# - C# 垃圾收集器如何找到唯一引用是内部指针的对象?

c# - 控制台应用程序相互通信的推荐方式是什么?

msmq - 通过脚本在 Microsoft 集群中创建私有(private) MSMQ 队列