This is an simple example of how to use .NET 2.0 to connect as a client to WebSphere MQ 7.
First, you need to setup a QueueManager, Channel, and Queue on a host and particular port. This may already be done for those lucky enough to have an MQ adminstrator.
Here is a screen shot of what this looks like in WebSphere’s MQ Explorer.
Second, you’ll need to add a reference to the amqmdnet.dll provided by WebSphere. If you installed WebSphere MQ server then you’ll have it already in the bin directory of your WebSphere installation.
The amqmdnet.dll that you reference also references a different dll, amqmdxcs.dll. You must include this in your project bin or wherever you choose to put these dlls.
If you are using the dll’s from WebSphere MQ 7, you’ll be fine. I’ve tested this connection example against both WebSphere 6 and WebSphere 7 (both using version 7′s dlls). However, if you decide to use version 6 dlls you’ll need more dlls than the two mentioned here. You may even have trouble (as I’ve had) using diferent minor versions of the version 6 dlls. So, if you can, use version 7 dlls.
Also note: There are some security features and some event features that are not included in the .NET dlls provided by WebSphere that are used in the java implementations.
Without further ado, the example code for a simple connection to WebSphere MQ using a .NET client:
using System; using IBM.WMQ; // Added reference to "C:\Program Files\ibm\WebSphereMQ\bin\amqmdnet.dll" using System.Collections; namespace TestWebsphereMQ { public class WMQTester { static void Main(string[] args) { // Setup some test data string queueManagerName = "QM_TEST"; string host = "127.0.0.1"; int port = 1415; string channel = "QM_TEST.SVRCONN"; WMQTester tester = new WMQTester(); tester.Connect(queueManagerName, host, port, channel); } public MQQueueManager Connect( string queueManagerName, string host, int port, string channel) { MQQueueManager queueManager; // Setup connection information Hashtable queueProperties = new Hashtable(); queueProperties[MQC.HOST_NAME_PROPERTY] = host; queueProperties[MQC.PORT_PROPERTY] = port; queueProperties[MQC.CHANNEL_PROPERTY] = channel; try { // Attempt the connection queueManager = new MQQueueManager(queueManagerName, queueProperties); Console.WriteLine("Connected Successfully"); } catch (MQException mexc) { // TODO: Setup other exception handling throw new Exception(mexc.Message + " ReasonCode: " + mexc.ReasonCode + "--- see list of Reason Codes at http://russsutton.com/developer/?p=45 " + mexc.StackTrace, mexc); } // For now, return the queueManager to use in reading/writing messages next return queueManager; } } }
Hope that helps! I banged my head on the wall a few times with the different versions of dlls and finding the lack of features in the WebSphere-provided .NET implementation that are readily available in the java implementations.
