无法使用模拟

时间:2017-08-31 10:05:09

标签: c# dll ibm-mq

我正在尝试使用在系统上创建的本地用户连接到IBM MQ队列管理器。

我使用的是Win server 2008 R2,IBM MQ客户端和服务器版本是8.0.0.6

要以本地用户身份运行代码,我已经实现了模拟。现在,当我冒充用户并尝试连接时,我得到以下连接错误。当我以不同的用户(本地用户)运行visual studio时,它可以正常工作,但不是在我使用模拟时。

以下是追踪:

 System.TypeInitializationException was unhandled
  HResult=-2146233036
  Message=The type initializer for 'IBM.WMQ.MQQueueManager' threw an exception.
  Source=amqmdnet
  TypeName=IBM.WMQ.MQQueueManager
  StackTrace:
       at IBM.WMQ.MQQueueManager..ctor(String queueManagerName, String Channel, String ConnName)
       at MQExample.MQTest.ConnectMQ(String strQueueManagerName, String strQueueName, String strChannelInfo) in D:\Adidas\MQExample\MQExample\MQExample\MQTest.cs:line 83
       at MQExample.Form1.button1_Click(Object sender, EventArgs e) in D:\Adidas\MQExample\MQExample\MQExample\Form1.cs:line 33
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at MQExample.Program.Main() in D:\Adidas\MQExample\MQExample\MQExample\Program.cs:line 19
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
       HResult=-2146232828
       Message=Exception has been thrown by the target of an invocation.
       Source=mscorlib
       StackTrace:
            at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
            at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
            at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
            at System.Activator.CreateInstance(Type type, Boolean nonPublic)
            at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, StackCrawlMark& stackMark)
            at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
            at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture)
            at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
            at IBM.WMQ.CommonServices.CreateCommonServices()
            at IBM.WMQ.CommonServices.TraceConstructor(String objectId, String sccsid)
            at IBM.WMQ.Nmqi.NmqiEnvironment..ctor(NmqiPropertyHandler nmqiPropertyHandler)
            at IBM.WMQ.Nmqi.NmqiFactory.GetInstance(NmqiPropertyHandler properties)
            at IBM.WMQ.MQQueueManager..cctor()
       InnerException: 
            HResult=-2146232828
            Message=Exception has been thrown by the target of an invocation.
            Source=amqmdnet
            StackTrace:
                 at IBM.WMQ.MQCommonServices..ctor()
            InnerException: 
                 HResult=-2146233088
                 Message=Failed to load mqe.dll from folder C:\Program Files\IBM\WebSphere MQ\bin\
                 Source=amqmdnet
                 StackTrace:
                      at IBM.WMQ.Nmqi.NativeManager.InitializeNativeApis(String mode)
                 InnerException: 

实现它的代码:

 using (new Tools.Impersonator("iibadmin", ".", "Test@1234"))
                    {
                        string userafter = WindowsIdentity.GetCurrent().Name;
                        MQQueueManager queueManager = null;
                        queueManager = new MQQueueManager(QueueManagerName, channelName, connectionName);

                        int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT | MQC.MQOO_INQUIRE | MQC.MQOO_FAIL_IF_QUIESCING;
                        queue = queueManager.AccessQueue(QueueName, openOptions);
                        int depth = queue.CurrentDepth;
                        queueMessage = new MQMessage();
                        queueMessage.Format = MQC.MQFMT_STRING;
                        queueGetMessageOptions = new MQGetMessageOptions();
                        queue.Get(queueMessage, queueGetMessageOptions);
                        strReturn =
                        queueMessage.ReadString(queueMessage.MessageLength);
                        queue.Close();

                        queueManager.Disconnect();
                    }

1 个答案:

答案 0 :(得分:0)

您的模拟用户可能没有MQ环境设置。

即。环境变量:

PATH
LIB
MQ_JAVA_DATA_PATH
MQ_JAVA_INSTALL_PATH
MQ_JAVA_LIB_PATH
MQ_JRE_PATH

如果您运行的是32位.NET,那么您需要指向32位MQ环境,就像64位一样明智。

即。 mqe.dll位于:

C:\Program Files\IBM\WebSphere MQ\bin\mqe.dll
C:\Program Files\IBM\WebSphere MQ\bin64\mqe.dll

因此,了解您正在运行的内容以及需要设置的MQ环境是个好主意。