Connecting to MQSeries with .NET

Damir Dobric Posts

Next talks:

 

    

Follow me on Twitter: #ddobric



 

 

Archives

By connecting to MQSeries withing a .NET application, first it has to be done is to install MQ Series client at the machine which will host the application.
To do that, you can obtain for example the trial version of WebSphere MQ. During installation first the prerequisites are checked. Unfortunately if you try to install the package at Windows Vista, no wonder, it will just fail, because the operative system is currently not supported by the setup. The good thing is that this requirement just can be ignored. Clearly that means, just install all other prerequisites and proceed with installation.

If you installed the Web Sphere at the local machine or somewhere in the windows environment with active directory infrastructure, there is a group named "MQM". The windows user who is running your .NET application has to be member of this group in order to be able to connect to MQ. In that case following code can be used to establish the connection and to put one simple message in the queue:

        [TestMethod]
        [Description("Changes the status of one single event."), Owner("ddobric")]
        public void MQ()
        {
            MQQueueManager queueManager = new MQQueueManager("QM_testmgr", "mychannel", "192.168.1.64");
            MQQueue queue = queueManager.AccessQueue("default", MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING);
            MQMessage queueMessage = new MQMessage();

            queueMessage.Format = MQC.MQFMT_STRING;
            queueMessage.MessageId = new byte[] { 0x01, 0x02, 0x03, 0x04};
            queueMessage.WriteString("Hello World");

            MQPutMessageOptions queuePutMessageOptions = new MQPutMessageOptions();
            queue.Put(queueMessage, queuePutMessageOptions);

        }

This code connects to the MQ manager "QM_testmgr" by using of channel "mychannel" hostet at the specified IP address. After executing you can see the message in the queue "default".
Assume you want now to connect to some remote machine. If you use this code the connection to the queueManager will fail with reason code 2035 = Not Authorized. To avoid this problem the MCA of the remote channel
has to be explicitly set. To do that, open the MQ Explorer go to channel properties and open the tab MCA. Then enter the name of the user who is authorized to connect. In the example bellow, I used the user "mqm".

Now the code in the .NET application has to be slightly changed as shown in the next example:

        [TestMethod]
        [Description("Changes the status of one single event."), Owner("ddobric")]
        public void MQ()
        {
            Hashtable props = new Hashtable();
            props.Add(MQC.HOST_NAME_PROPERTY, "sopmqseries");
            props.Add(MQC.CHANNEL_PROPERTY, m_ChannelName);
            props.Add(MQC.USER_ID_PROPERTY, "mqm");
            props.Add( MQC.PASSWORD_PROPERTY, "enter anything here." );

            MQQueueManager queueManager = new MQQueueManager(m_QueueManager, props);


            MQQueue
queue = queueManager.AccessQueue("default",
            MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING);
            MQMessage queueMessage = new MQMessage();

            queueMessage.Format = MQC.MQFMT_STRING;
            queueMessage.MessageId = new byte[] { 0x01, 0x02, 0x03, 0x04};
            queueMessage.WriteString("Hello World");

            MQPutMessageOptions queuePutMessageOptions = new MQPutMessageOptions();
            queue.Put(queueMessage, queuePutMessageOptions);

        }


At this point is important, that specified username "mqm" has to match the name set in the MCA-tab. Additionally it is interesting, that the password does not have to match user's password, but it has to be specified as property.
If password property is not specified at all, the initialization will crash with a "null reference error".

Last but not least. If you do not want to specify the username in your code, means you would like to use the first code example, there also one a little confusing possibility. Create some other user and put in the MQM group. The name of this user should be the same as the name of user who will run the .NET application. In this case if the name of interactive user (who is running the application) matches the name of the user member of MQM group at the remote system (this does not have to be necessarily windows system) you will not need the provide credential properties (MQC.USER_ID_PROPERTY and MQC.PASSWORD_PROPERTY) and MCA name may be empty.

By troubleshooting following very useful link contains the list of all reason codes. 


Posted Apr 13 2007, 01:31 AM by Damir Dobric

Comments

Andreas Erben's posts wrote Advanced WebSphere MQ connectivity in .NET apps, JMS without JVM
on 04-15-2007 4:31

Using WebSphere MQ (still MQSeries for most of us) and the related products abbreviated WPM and alike

rajiv wrote re: Connecting to MQSeries with .NET
on 06-05-2007 10:56

Hi,

I have gone through your entire article and i think it solves the real challenges.

I am trying to make a C# application which can talk to MQseries.

As per a IBM document, there are two ways of doing it,one using client connection and other is using server binding.

The IBM article further says that server binding is faster compared to client connection.

i couldn't understand why it is so in reality(is it bypassing TCP channel and creating its own native channel and port for itself) and what is the basic difference between clent connection and server Binding.

I understand that in this article, we are using client connection.

One more for server binding, do we need to configure something inside the MQclient like queue manager, or only the code will take care of everything.

Please help me in understanding the above things.

Damir Dobric wrote re: Connecting to MQSeries with .NET
on 06-05-2007 14:26

We implemented two different bindings for WCF, which interoperate with MQ. The first one is based on the native MQSeries API based on .NET (as described in this post) and second one is based on IBM’s XMS library. This library is the new one and not well tested. However it enables you to communicate with MQ based on “topics” and “queues”. Depending on what you are going to do, you should choose one of these scenarios.

Personally, I would recommend using custom WCF-binding. DAENET will probably (not sure) soon publish corresponding WCF-bindings as free download.

Hope this helps you.

Adanacp wrote re: Connecting to MQSeries with .NET
on 06-15-2007 5:04

Can you provide me some guidance on where to start implementing a custom WCF-binding for MQSeries ? (or any other custom binding not included in the framework for that matter)

Damir Dobric wrote re: Connecting to MQSeries with .NET
on 06-16-2007 16:12

This link contain some examples related to extending of WCF:
developers.de/.../WinDays-2007-session_3A00_-Extending-WCF.aspx.

I Satya Sekhar wrote re: Connecting to MQSeries with .NET
on 06-17-2007 15:35

Hi

 I would like start the basic .NET application to connect to MQSeries, Could you pls tell the

the pre-requests for this? ( that is softwares required etc)

I have knowledge about MQSeries on unix and windows using c/c++

email: isatyasekhar@in.ibm.com

           issekhar@gmail.com

Pls do the needful

Thanks and Regards

Satya

jinishans wrote re: Connecting to MQSeries with .NET
on 08-06-2007 23:37

Hi

Is this IBM MQ binding using MQ Classes for .NET. Can you give me the URL for the download, if so.

Regards

jinishans

Damir Dobric wrote re: Connecting to MQSeries with .NET
on 08-06-2007 23:55

Prerequisites connecting to MQSeries by using of .NET is at least MQSeries Client installation on the machine which will act (connect) as client. This one can be ovtained from IBM site as trial version.

Damir Dobric wrote re: Connecting to MQSeries with .NET
on 08-06-2007 23:59

We implemented two different bindings for WCF. First one is based on the native MQSeries API (installed with MQ Series client) . The second one enables queues and topics and is based on IBM’s XMS library.

Currently DAENET does not offer a free version of this binding.

Kshah wrote re: Connecting to MQSeries with .NET
on 10-12-2007 4:37

Hi Dominic,

I am running into issue for accessing QueueManager on remote machie.

I am making webservice call to connect to MQ.  For identity i am using web.config impersonation and it works great.

But as soon as I use same webservice as transactional webservice it stop working and it give me 2035.

Do you have ideas or solution to resovle it??

Padmacharan Bisoi wrote re: Connecting to MQSeries with .NET
on 01-15-2008 22:20

Damir, I have the Websphere installed on my machine. I am getting the Not Authorized error while trying to connect to Qmanager. Could u please in detail how to add my user id to have access to the queue on a remote machine.

Thanks...

Damir Dobric wrote re: Connecting to MQSeries with .NET
on 01-16-2008 1:05

Specified username "mqm" has to match the name set in the MCA-tab. See picture above.

If not so, the windows interactive user has to have permission in MQ.

Randy N. Villa wrote re: Connecting to MQSeries with .NET
on 03-20-2008 5:13

IBM has shipped a proof-of-concept technology in its alphaWorks site which enables WebSphere MQ v6.x to be used as a custom transport channel within the Windows® Communication Foundation framework, as available in the .NET Framework 3.

SongKai wrote re: Connecting to MQSeries with .NET
on 05-19-2008 4:47

How come my MQQueueManager is not support below overload ?

MQQueueManager queueManager = new MQQueueManager(m_QueueManager, props);

Vijay wrote re: Connecting to MQSeries with .NET
on 06-04-2008 8:24

Is it better to connect to mq series using .NET with GUI or we have to write code? Which one is better and faster?

Damir Dobric wrote re: Connecting to MQSeries with .NET
on 06-04-2008 9:02

Hi Vijay,

I'm not sure what do you mean with GUI?

Vijay wrote re: Connecting to MQSeries with .NET
on 06-05-2008 11:25

Hi Damir,

               Which concept is used to connect to MQ-Series in .NET ex in Java we will use JMS and which namespace is used for MQ-series in .NET and I am not getting how to start coding can you suggest any PDF for implementing MQ-Series using .NET.

Damir Dobric wrote re: Connecting to MQSeries with .NET
on 06-05-2008 12:09

I would recommend you to take a look on this API: www.ibm.com/.../0509_phillips.html

I used this one to implement WCF-Channels which support JMS.

Vijay wrote re: Connecting to MQSeries with .NET
on 06-09-2008 5:06

send me the code to upload a file in windows application in .NET 1.1

rajiv wrote re: Connecting to MQSeries with .NET
on 06-18-2008 20:59

I have a strange problem.

I created a webservice in .net for put and get that

works perfectly as long as I run it on windows xp machine but breaks as soon as I publish the service

on windows 2003 server. The underlying websphere mq works as a windows form application does get messages back .

I cannot figure out why a web service running in IIS would break.

Damir Dobric wrote re: Connecting to MQSeries with .NET
on 06-18-2008 23:58

1. Take a look on Identity of the pool

2. Take a look on impersonation

This may help:

developers.de/.../user-impersonation-in-wcf.aspx

rajiv wrote re: Connecting to MQSeries with .NET
on 06-19-2008 0:46

I have the impersonation set in the webconfig (.net 2.0 framework) as well as programatically but it still fails.

As for the pool , I tried using W2k3 server credentiials as well as a valid user/password on websphere MQ server/AS400  but neither work.

Damir Dobric wrote re: Connecting to MQSeries with .NET
on 06-19-2008 9:21

If the pools's user name (with no impersonation) equlas  props.Add(MQC.USER_ID_PROPERTY, "mqm");

of the caller's user name (with impersonation) equals  props.Add(MQC.USER_ID_PROPERTY, "mqm");

the it should work,

Otherwise I would take a look on MQ security.

rajiv , St. Louis wrote re: Connecting to MQSeries with .NET
on 06-19-2008 17:36

Thanks Damir !

Setting 1. and 2.  got it working.

I had missed the DOMAIN\    before the username

in the Pool Identity .

Damir Dobric wrote re: Connecting to MQSeries with .NET
on 06-19-2008 17:41

That means that Web Service didn't work at all :)Cheers...

san1850 wrote re: Connecting to MQSeries with .NET
on 06-20-2008 22:51

I am currently using a webservice to put and get the mqmessage from my .net windows application.Everything is working fine but 1 in 100 times the host sends me blank message back.When we try it from local machine with client installed it never happens.It is killing the users as it blanks out the form and they need to enter all the values.Is it any setting we are missing or its instance creation of webservice is causing error.Thank you.

Damir Dobric wrote re: Connecting to MQSeries with .NET
on 06-21-2008 13:46

There many possible settings which could cause similar behavior. Be sure that you do not use session or if you use it, be sure that session relevant params are correctly set.

However, before you start to setup complex things, try to mock the MQ-layer from WCF-Service point of view. Write a class which does nothing to be sure that this behavior is caused by Web-Service and not anything behind it. If it is WS, check following params:

msdn.microsoft.com/.../ms731379.aspx

Vijay wrote re: Connecting to MQSeries with .NET
on 06-25-2008 5:11

Hi Damir,

           Can you send me the syntax of the Connection string for conncting with Web-Sphere from .NET

Vijay wrote re: Connecting to MQSeries with .NET
on 06-25-2008 7:27

Hi Damir,

             What does there Errors mean, when I am trying to connect with a Web-Sphere from .NET 1.1:

An unhandled exception of type 'System.EntryPointNotFoundException' occurred in amqmdnet.dll

Additional information: Unable to find an entry point named xcsDetachThread in DLL amqxcs2.dll.

An unhandled exception of type 'System.TypeInitializationException' occurred in amqmdnet.dll

Additional information: The type initializer for "IBM.WMQ.MQTrace" threw an exception.

Damir Dobric wrote re: Connecting to MQSeries with .NET
on 06-25-2008 9:16

Connecting example:

MQQueueManager queueManager = new MQQueueManager("QM_testmgr", "mychannel", "192.168.1.64");

           MQQueue queue = queueManager.AccessQueue("default", MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING);

Damir Dobric wrote re: Connecting to MQSeries with .NET
on 06-25-2008 9:17

The exception problem could be caused by incorrect installation of MQ-client.

Vijay wrote re: Connecting to MQSeries with .NET
on 08-05-2008 11:44

How to count all the messages in MQ-Series and get display that particular Message????

Armen Merikyan wrote re: Connecting to MQSeries with .NET
on 09-25-2008 1:57

How can i get queue count so that i can process each msg

Deven wrote re: Connecting to MQSeries with .NET
on 07-05-2010 14:59

Hi,

I am trying to connect to MQ with the help of AMQCLCHL.TAB file.

Please let me know how to connect to MQ With XMS in C#.

Thanks in advance..

-deven

Arvind wrote re: Connecting to MQSeries with .NET
on 11-18-2010 9:41

Hi  Damir ,

I am using .net 1.1 to connect to MQ server. I have installed the client on which application is running.

Everything works fine unless I change the default port in my code. I am not able to change the port number using the .net code.

MQEnvironment.Port = 1000;

No matter what port Number I give here it always trying to Connect default port(1414). I have checked my entire code I am not setting the port to 1414.

Here is Log:

Remote host 'ServerName (XXX.XX.XXX.16) (1414)' not available, retry later.  

The attempt to allocate a conversation using TCP/IP to host 'ServerName (XXX.XX.XXX.16) (1414) was not successful.  However the error may be a transitory one and it may be possible to successfully allocate a TCP/IP conversation later.  

Try the connection again later. If the failure persists, record the error values and contact your systems

administrator. The return code from TCP/IP is 10061 (X'274D'). The reason for the failure may be that this host cannot reach the destination host. It may also be possible that the listening program at host 'ServerName (XXX.XX.XXX.16) (1414)' was not running.  If this is the case, perform the relevant operations to start the TCP/IP listening program, and try again.

Arvind C wrote re: Connecting to MQSeries with .NET
on 11-18-2010 9:49

Hi  Damir ,

I am using .net 1.1 to connect to MQ server. I have installed the client on which application is running.

Everything works fine unless I change the default port in my code. I am not able to change the port number using the .net code.

MQEnvironment.Port = 1000;

No matter what port Number I give here it always trying to Connect default port(1414). I have checked my entire code I am not setting the port to 1414.

Here is Log:

Remote host 'ServerName (XXX.XX.XXX.16) (1414)' not available, retry later.  

The attempt to allocate a conversation using TCP/IP to host 'ServerName (XXX.XX.XXX.16) (1414) was not successful.  However the error may be a transitory one and it may be possible to successfully allocate a TCP/IP conversation later.  

Arvind wrote re: Connecting to MQSeries with .NET
on 11-30-2010 8:19

Problem resolved after using newer version of “amqmdnet.dll” (1.0.0.3)  and creating the MQmanager object using Hash Table as shown in this article.

The file cound be found in MQ client installation directory.

Vish wrote re: Connecting to MQSeries with .NET
on 02-03-2011 5:29

I have tried in fail to create a server connection from .net code. According to the api on the QueueManager constructor only the name of queue manager is required and when I do that it throws a 2058 error while creating it. Any ideas ?

Nagaraj wrote re: Connecting to MQSeries with .NET
on 02-03-2012 10:32

Scenario :

Just supply the IP Address, port and queue name in the configuration URI

uri="wmq://123.123.123.123:2414/XYZ.CHANNEL?queue=M.X.IN?replyTo=M.X.REPLY"

Note: No Queue manager name is specified the Uri

Question:

How to access the default queue manger in c# code, when the user doesn't know about the queue manager name.

Would be helpful if you can point out how to connect using c#.

IBM MQSeries Accessing Issue from .NET | Search RounD wrote IBM MQSeries Accessing Issue from .NET | Search RounD
on 03-07-2014 0:31

Pingback from  IBM MQSeries Accessing Issue from .NET | Search RounD

developers.de is a .Net Community Blog powered by daenet GmbH.