WFP all TextBlocks not updating - c#

So my code was working just fine. But now, even though I can confirm the variables and strings are there through a Clipboard copy, the textblocks are not updating via TextBlock.Text. Did I turn something off? Every single one stopped doing it at the same time.
`private void actionPing_Click(object sender, RoutedEventArgs e)
{
try
{
Ping myPing = new Ping();
PingReply reply = myPing.Send(HostNameIPTyped.Text.ToString(), 500);
if (reply != null)
{
string tripTime = reply.RoundtripTime.ToString();
if (tripTime == "0")
{
PingStatus1.Foreground = new System.Windows.Media.SolidColorBrush((Color)ColorConverter.ConvertFromString("#660000"));
PingStatus1.Text = "Device not found"; <---NONE OF THESE ARE UPDATING ACROSS THE ENTIRE PROGRAM
}
else
{
PingStatus1.Foreground = new System.Windows.Media.SolidColorBrush((Color)ColorConverter.ConvertFromString("#17b05c"));
PingStatus1.Text = "Ping Successful, " + reply.RoundtripTime.ToString() + "ms roundtrip";
//// LET'S GET THE IP AND HOSTNAME OF WHATEVER THE HELL YOU PUT IN
////IPADDRESS
try
{
System.Net.IPAddress ip = System.Net.Dns.GetHostEntry(HostNameIPTyped.Text.ToString()).AddressList.Where(o => o.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).First();
ipAddress = ip.ToString();
}
catch (Exception ex)
{ }
////HOSTNAME
try
{
System.Net.IPHostEntry hostEntry = System.Net.Dns.GetHostEntry(ipAddress);
machineName = hostEntry.HostName;
string linkBuild = "http://" + machineName;
}
catch (Exception ex) { }
PingStatus1.Text += " " + machineName;
}
}
}
catch
{
{
PingStatus1.Foreground = new System.Windows.Media.SolidColorBrush((Color)ColorConverter.ConvertFromString("#660000"));
PingStatus1.Text = "Device not found";
}
}
}`

It was updating the fields. I had made an adjustment to the size of the grid which flattened all of the textblock fields to the left side. Once I drug them out they were no longer auto-sized.
Thanks for the help!

Related

TcpClient hangs

I am working on a simple Client to send data to a server at my office. I tested the code locally on my computer using a server called TCPServer, I connect, send data, receive reply, disconnect, send again, rinse and repeat, it all works perfectly, but when I connect to office and do the same thing it connects fine, I can connect/disconnect forever, but when I send data it just hangs. Nothing is received in the log at the office. I can't send a single byte there.
Seems like a firewall issue doesn't it.
But I can run an older program I wrote years ago in Delphi (pascal), and it connects and sends the same data over without issue, same port, everything, so the problem is not a firewall issue. Thoughts on this? Here is the basic code layout.
Connect Button
Disconnect Button
Send Button
At the top of the class I declare the TcpClient Variable
public TcpClient m_client = new TcpClient();
In the _Click for Connect Button and Disconnect Button I have code to connect to server and set some indicators etc
private void ConnectButton_Click(object sender, EventArgs e)
{
string address = FixIP(IPAddressMaskedTextBox.Text);
int Port = Convert.ToInt32(PortNumberMaskedTextBox.Text);
Control control = (Control)sender;
String name = control.Name;
try
{
switch (name)
{
case ("ConnectButton"):
//Connect to server
connect(address, Port);
if (m_client.Connected)
{
SingleConnectionRichTextBox.Clear();
ConnectedLightButton.BackColor = Color.Lime;
SingleConnectionRichTextBox.Text += "Connected at IP " + address + " and Port " + Port.ToString() + "\r\n";
}
break;
case ("DisconnectButton"):
if (m_client.Connected)
{
SingleConnectionRichTextBox.Text += "Connection Terminated\r\n";
ConnectedLightButton.BackColor = Color.Red;
m_client.Client.Disconnect(false);
m_client = new TcpClient();
}
break;
}
}
catch (Exception err)
{
MessageBox.Show(err.ToString());
}
}
public void connect(string address, int port)
{
try
{
if (!m_client.Connected)
{
ConnectedLightButton.BackColor = Color.Yellow;
SingleConnectionRichTextBox.Text += "Attempting to Connect...\r\n";
m_client.Connect(address, port);
}
else
{
SingleConnectionRichTextBox.Text += "Connection Failed...\r\n";
ConnectedLightButton.BackColor = Color.Red;
throw new Exception("Connect: Already connected\r\n");
}
}
catch (Exception err)
{
MessageBox.Show(err.ToString());
}
}
The Send button has it's own event, mostly because when connecting to office it can take a minute for sockets to be created etc.
private void SendButton_Click(object sender, EventArgs e)
{
try
{
if (m_client.Connected)
{
string completeString = "";
for (int cnt = 0; cnt < SingleSendRichTextBox.Lines.Count() - 1; cnt++)
{
string aLine = Regex.Replace(SingleSendRichTextBox.Lines[cnt], #"\e\[(\d+;)*(\d+)?[ABCDHJKfmsu]", "");
if (cnt == 0)
{
//First line gets Start Block, plus a CR on end
aLine = (char)0x0B + aLine + (char)0x0D;
}
else if (cnt == (SingleSendRichTextBox.Lines.Count() -1))
{
//Last line gets CR + End Block + CR on end
aLine += (char)0x0D + (char)0x1C + (char)0x0D;
}
else
{
//All middle lines get CR on end
aLine += (char)0x0D;
}
//MessageBox.Show("Sending line# " + cnt.ToString() + " = " + aLine);
completeString += aLine;
}
Byte[] data = Encoding.ASCII.GetBytes(completeString);
WriteBytes(data);
ReadAllBytes();
}
else
{
MessageBox.Show("Nothing is connected currently...");
}
}
catch (Exception err)
{
MessageBox.Show(err.ToString());
}
}
public void WriteBytes(Byte[] data)
{
try
{
if ((m_client.Connected)&&(data.Count() > 0))
{
// Get access to network stream
NetworkStream stm = m_client.GetStream();
stm.Write(data, 0, data.Length);
stm.Flush();
//MessageBox.Show("Data Sent, length = " + data.Length.ToString());
}
else
{
MessageBox.Show("Either we are not connected, or there is no data to send!!");
}
}
catch (Exception err)
{
MessageBox.Show(err.ToString());
}
}
public void ReadAllBytes()
{
try
{
// Buffer to store the response bytes.
Byte[] readdata = new Byte[256];
// String to store the response ASCII representation.
String responseData = String.Empty;
NetworkStream stm = m_client.GetStream();
// Read the first batch of the TcpServer response bytes.
Int32 bytes = stm.Read(readdata, 0, readdata.Length);
responseData = Encoding.ASCII.GetString(readdata, 0, bytes);
SingleReplyRichTextBox.Text += responseData + "\r\n";
}
catch (Exception err)
{
MessageBox.Show(err.ToString());
}
}
Does anything in here raise a red flag that is obvious? I tried Stream VS NetworkStream. I tried turning off the Reply listener. I took this code and combined it all into one function, and moved the TcpClient creation over to a different Class as static so I could create it in each function, but while all of these worked fine locally, nothing works connecting to office. It won't send a single byte. I set m_client as static at the top too, works fine locally, not to office.
Is GetStream failing maybe? or its sending the data on a different socket?
use a Task and cancellation token to cancel the task. Don't init a "new" TCPclient. In the background the TCPclient will be not closed (socket-timeout of .net)
Use try catches to see the exception and add the log to the conversation to make it more clear pls

Error Code -201 when using ZKemKeeper.DLL

I am creating a C# WinForm using ZKemKeeper.DLL to fetch data from a Biometric Device. But when I try connecting my App to the Device it always gives Error Code -201
Any Idea what could be causing this? I've read a guide document for ZKemKeeper.DLL but it doesn't list -201 as an error code. Any help would be much appreciated. Thank you.
private void btnConnect_Click(object sender, EventArgs e)
{
try
{
IsConnected = TimeKeeper.Connect_Net(txtIP.Text, 4370);
if (IsConnected == true)
{
MessageBox.Show("Device Connected Successfully.");
}
else
{
TimeKeeper.GetLastError(ref ErrorCode);
MessageBox.Show("Device Not Found. Error Code : " + ErrorCode.ToString(), "Error");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
protected void btnConnect_Click(object sender, EventArgs e)
{
try
{
//this.Cursor = Cursors.WaitCursor;
//ShowStatusBar(string.Empty, true);
if (IsDeviceConnected)
{
IsDeviceConnected = false;
//this.Cursor = Cursors.Default;
return;
}
string ipAddress = txtIPAddress.Text.Trim();
string port = txtPort.Text.Trim();
if (ipAddress == string.Empty || port == string.Empty)
throw new Exception("The Device IP Address and Port is mandotory !!");
int portNumber = 4370;
if (!int.TryParse(port, out portNumber))
throw new Exception("Not a valid port number");
bool isValidIpA = UniversalStatic.ValidateIP(ipAddress);
if (!isValidIpA)
throw new Exception("The Device IP is invalid !!");
isValidIpA = UniversalStatic.PingTheDevice(ipAddress);
if (!isValidIpA)
throw new Exception("The device at " + ipAddress + ":" + port + " did not respond!!");
objZkeeper = new ZkemClient(RaiseDeviceEvent);
IsDeviceConnected = objZkeeper.Connect_Net(ipAddress, portNumber);
if (IsDeviceConnected)
{
string deviceInfo = manipulator.FetchDeviceInfo(objZkeeper, int.Parse(txtMachineNumber.Text.Trim()));
//lblDeviceInfo.Text = deviceInfo;
lblMessage.Text = deviceInfo + "is Connected";
}
}
catch (Exception ex)
{
throw(ex);
}
//this.Cursor = Cursors.Default;
}

How to fix VS2013: Error: type or namespace 'Windows' could not be found?

I want to create a service for Wifi Direct. If I try to add Reference, I don't see core->windows option in VS2013. I have updated the winSDK.
How do I add the Windows.Devices.WifiDirect api ?
you can use
public sealed class WiFiDirectDevice : IDisposable
this is a sample code to handle connections
Windows.Devices.WiFiDirect.WiFiDirectDevice wfdDevice;
private async System.Threading.Tasks.Task<String> Connect(string deviceId)
{
string result = "";
try
{
// No device Id specified.
if (String.IsNullOrEmpty(deviceId)) { return "Please specify a Wi- Fi Direct device Id."; }
// Connect to the selected Wi-Fi Direct device.
wfdDevice = await Windows.Devices.WiFiDirect.WiFiDirectDevice.FromIdAsync(deviceId);
if (wfdDevice == null)
{
result = "Connection to " + deviceId + " failed.";
}
// Register for connection status change notification.
wfdDevice.ConnectionStatusChanged += new TypedEventHandler<Windows.Devices.WiFiDirect.WiFiDirectDevice, object>(OnConnectionChanged);
// Get the EndpointPair information.
var EndpointPairCollection = wfdDevice.GetConnectionEndpointPairs();
if (EndpointPairCollection.Count > 0)
{
var endpointPair = EndpointPairCollection[0];
result = "Local IP address " + endpointPair.LocalHostName.ToString() +
" connected to remote IP address " + endpointPair.RemoteHostName.ToString();
}
else
{
result = "Connection to " + deviceId + " failed.";
}
}
catch (Exception err)
{
// Handle error.
result = "Error occurred: " + err.Message;
}
return result;
}
private void OnConnectionChanged(object sender, object arg)
{
Windows.Devices.WiFiDirect.WiFiDirectConnectionStatus status =
(Windows.Devices.WiFiDirect.WiFiDirectConnectionStatus)arg;
if (status == Windows.Devices.WiFiDirect.WiFiDirectConnectionStatus.Connected)
{
// Connection successful.
}
else
{
// Disconnected.
Disconnect();
}
}
private void Disconnect()
{
if (wfdDevice != null)
{
wfdDevice.Dispose();
}
}

Can a ListViewItem Tag property contain a Socket object

On my server side I have set up a single thread code that creates a new Socket object every time a client connects. Then I pass whatever I get from the client along with the socket that is connected to a packet handler and do the calculations there. In my main form I have a listview that I populate via entity framework, and whenever a packet from a registered computer connects I update the listview. So my question is can I from the packet handler class pass the socket object to the tag property of my listview when I update it?
My server side code:
private void ReceivedCallback(IAsyncResult result)
{
Socket clientSocket = result.AsyncState as Socket;
SocketError ER;
try
{
int bufferSize = clientSocket.EndReceive(result, out ER);
if (ER == SocketError.Success)
{
byte[] packet = new byte[bufferSize];
Array.Copy(_buffer, packet, packet.Length);
Console.WriteLine("Handling packet from IP:" + clientSocket.RemoteEndPoint.ToString());
//Handle packet stuff here.
PacketHandler.Handle(packet, clientSocket);
_buffer = new byte[61144];
clientSocket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, ReceivedCallback, clientSocket);
//clientSocket.BeginReceive(new byte[] { 0 }, 0, 0, 0, ReceivedCallback, clientSocket);
}
else
{
Console.WriteLine("No bytes received, we're closing the connection.");
clientSocket.Close();
}
}catch(SocketException ex)
{
Console.WriteLine("We caught a socket exception:" + ex.Message);
clientSocket.Close();
}
}
And my packet handler class:
public static void Handle(byte[] packet, Socket clientSocket)
{
if (clientSocket.Connected)
{
if (packet.Length > 0)
{
IPEndPoint RemoteIP = (IPEndPoint)clientSocket.RemoteEndPoint;
ushort packetLength = BitConverter.ToUInt16(packet, 0);
ushort packetType = BitConverter.ToUInt16(packet, 2);
ushort packetID = BitConverter.ToUInt16(packet, 4);
Console.WriteLine("We received a packet of Type: {0}, ID: {1} FROM {2}", packetType, packetID, RemoteIP.ToString());
if (packetType == 1)
{
switch (packetID)
{
case 1://Check if computer is registered in the database
CheckRegisteredRequest request1 = new CheckRegisteredRequest(packet);
Console.WriteLine("We received (Case 1): " + request1.Text);
string Response = "";
bool found = false;
ServerDbContext database = new ServerDbContext();
foreach (computers pcs in database.computers)
{
if (pcs.name == request1.Text.Split(',')[0])
{
found = true;
if (pcs.computer_ip == request1.Text.Split(',')[1])
{
//We found a computer with that name and ip address
Response = "true";
CheckRegisteredResponse resp1 = new CheckRegisteredResponse(Response);
clientSocket.Send(resp1.Data);
computers registeredPC;
var name = request1.Text.Split(',')[0];
using (var ctx = new ServerDbContext())
{
registeredPC = ctx.computers.Where(c => c.name == name).FirstOrDefault<computers>();
}
if (registeredPC != null)
{
registeredPC.networkStatus = "online";
}
using (var ctx = new ServerDbContext())
{
ctx.Entry(registeredPC).State = System.Data.Entity.EntityState.Modified;
ctx.SaveChanges();
}
addNewLog("Computer: " + request1.Text.Split(',')[0] + " came online - IP: " + request1.Text.Split(',')[1]);
RaiseFeedback("PC: " + request1.Text.Split(',')[0] + " came online - IP: " + request1.Text.Split(',')[1]);
break;
}
else
{
//We found a computer with that name but a different ip address, update it
var name = request1.Text.Split(',')[0];
var registeredPC = new computers();
using (var ctx = new ServerDbContext())
{
registeredPC = ctx.computers.Where(c => c.name == name).FirstOrDefault<computers>();
}
if (registeredPC != null)
{
var ip = request1.Text.Split(',')[1];
registeredPC.computer_ip = ip;
registeredPC.networkStatus = "online";
}
using (var ctx = new ServerDbContext())
{
ctx.Entry(registeredPC).State = System.Data.Entity.EntityState.Modified;
ctx.SaveChanges();
}
Response = "true";
CheckRegisteredResponse resp1 = new CheckRegisteredResponse(Response);
clientSocket.Send(resp1.Data);
addNewLog("Computer: " + request1.Text.Split(',')[0] + " came online - IP: " + request1.Text.Split(',')[1]);
RaiseFeedback("PC: " + request1.Text.Split(',')[0] + " came online - IP: " + request1.Text.Split(',')[1]);
break;
}
}
}
if (!found)
{
//There is no computer with that name in the database so send false
Response = "false";
CheckRegisteredResponse resp1 = new CheckRegisteredResponse(Response);
clientSocket.Send(resp1.Data);
}
break;... and so on....
So I've tried via a handler:
this is my custom event handler Args:
public class TextArgs : EventArgs
{
#region Fields
private string szMessage;
private Socket _sockets123;
#endregion Fields
#region ConstructorsH
public TextArgs(string TextMessage,Socket sock)
{
if (sock != null)
{
_sockets123 = sock;
szMessage = TextMessage;
}
else
{
szMessage = TextMessage;
}
}
#endregion Constructors
#region Properties
public string Message
{
get { return szMessage; }
set { szMessage = value; }
}
public Socket _socket
{
get { return _sockets123; }
set { _sockets123 = value; }
}
#endregion Properties
}
This is how i define that handler at the PacketHandler class:
public static event LogsEventHandler Feedback;
private static void RaiseFeedback(string p, Socket sock)
{
LogsEventHandler handler = Feedback;
if (handler != null)
{
handler(null,new TextArgs(p,sock));
}
}
And whenever a computer registers or connects I do the following:
RaiseFeedback("PC: " + request1.Text.Split(',')[0] + " came online - IP: " + request1.Text.Split(',')[1], clientSocket);
or
RaiseFeedback("PC: " + request1.Text.Split(',')[0] + " came online - IP: " + request1.Text.Split(',')[1], null);
And the event fires of these two methods in my main form:
private void OnFeedbackReceived(object sender, TextArgs e)
{
Invoke((MethodInvoker)delegate
{
UpdateComputers();
UpdateGUI(e.Message,e._socket);
}
);
}
public void UpdateGUI(string s, Socket sock)
{
LogBox.Text += s;
LogBox.AppendText(Environment.NewLine);
using (var context = new ServerDbContext())
{
var PCInfo = context.viewUsersInfo;
dataGridView1.DataSource = PCInfo.ToList();
}
if (sock != null)
{
ListViewItem item = ComputersList.FindItemWithText(s.Split(':')[1].ToString());
item.Tag = sock;
}
}
The Question: Yes, you can. Control.Tag is of type object and can hold pretty much anything you choose.
So you can write
Socket socket = someSocket;
ListViewItem item = listView1.FindItemWithText(someText);
if (item != null) item.Tag = socket; else Console.WriteLine(someText + " not found!);
And retrieve it as:
if (item.Tag != null) someSocket = item.Tag AS socket;
if (someSocket != null) ..
It is up to you to watch out for the success of the cast when retrieving it but also if the Socket still is alive and well..
The problem: The stackoverflow in your code is due to an erreanous, short-circuited property, which you have fixed by now. In general, you only need to write explicit getter and setters if they actually do more than just gettin and setting.
They could log out out test data, update other, dependent properties, do checks or conversiones or do other stuff.
But if none of it is needed, simply don't create the private fields and write the automatic getter and setters:
public Socket MySocket {get; set;}
Also note that the naming convention asks you to capitalize proprety names!

Websphere MQ - Keeping message context and identity when moving from one queue to another in .NET

I have been tasked with writing a custom application in C#.NET to move messages from one queue to another, while keeping all of the context and identity information the same as the original message.
I have tried to decipher the online docs, and have tried every combination of MQC.MQOO_PASS_ALL_CONTEXT and MQOO_SAVE_ALL_CONTEXT in opening queues, and MQC.MQPMO_PASS_ALL_CONTEXT in put calls, to no avail. I have tried setting the MQPutMessageOptions.ContextReference to both the source and the
destination queues, but I continue to get exceptions, either MQRC_CONTEXT_HANDLE_ERROR or MQRC_OPTIONS_ERROR, and I cannot find enough information in the online docs to determine the problem.
I am using runtime version v2.0.50727, Version 7.5.0.1 of the amqmdnet.dll. If anyone knows the correct open and/or put message option settings I need to use, I would appreciate the leg up. Thanks.
***** SECOND UPDATE *****
Here is a very simple gui program to test the classes, and I get the same results:
public partial class frmMain : Form
{
// Simple test - all data hard-coded
string sourceQStr = "PETE.TQ1";
string targetQStr = "PETE.TQ2";
string targetMsgIdStr = "414D5120514D4942584330352020202028ED0155202EB302";
string connName = "localhost";
string connPort = "1414";
MQQueueManager sourceManager;
MQQueueManager targetManager;
MQQueue sourceQueue;
MQQueue targetQueue;
MQMessage msg = new MQMessage();
MQGetMessageOptions gmo = new MQGetMessageOptions();
MQPutMessageOptions pmo = new MQPutMessageOptions();
public frmMain()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void txtPassword_Validating(object sender, CancelEventArgs e)
{
if (txtPassword.Text.Trim() != string.Empty)
{
if (LoginUser())
btnTest.Focus();
else
txtUserId.Focus();
}
}
public void LogoutUser()
{
txtUserId.Text = "";
txtPassword.Text = "";
btnTest.Enabled = false;
}
public bool LoginUser()
{
bool OK = ValidateUserAndPassword();
if (OK)
{
btnTest.Enabled = true;
}
else LogoutUser();
return OK;
}
private bool ValidateUserAndPassword()
{
if ((txtUserId.Text.Trim() == string.Empty) || (txtPassword.Text.Trim() == string.Empty))
return false;
try
{
bool _passwordValid = false;
ContextOptions options = ContextOptions.SimpleBind | ContextOptions.SecureSocketLayer;
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, "UP"))
{
_passwordValid = pc.ValidateCredentials
(txtUserId.Text.Trim(), txtPassword.Text.Trim(), options);
}
if (!_passwordValid)
MessageBox.Show("Invalid username / password", "Invalid",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return _passwordValid;
}
catch (PrincipalServerDownException pex)
{
string msg = pex.Message.Insert(pex.Message.IndexOf("server"), "Authentication ");
MessageBox.Show(msg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
private void btnTest_Click(object sender, EventArgs e)
{
try
{
SetupObjects();
sourceQueue.Get(msg, gmo);
targetQueue.Put(msg, pmo);
sourceManager.Commit();
targetManager.Commit();
MessageBox.Show("Test appears successful - verify with MQ Explorer","OK",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
catch (Exception ex) // MQRC_CONTEXT_HANDLE_ERROR is always thrown
{
MessageBox.Show("Error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
sourceManager.Backout();
targetManager.Backout();
}
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
/************************************** Utiility methods *****************************************/
private void SetupObjects()
{
// set up objects
string connectName = connName + "(" + connPort + ")";
int ConnOptions = MQC.MQCNO_HANDLE_SHARE_BLOCK;
sourceManager = new MQQueueManager("", ConnOptions, "CHANNEL1", connectName);
targetManager = new MQQueueManager("", ConnOptions, "CHANNEL1", connectName);
MQEnvironment.UserId = txtUserId.Text.Trim();
MQEnvironment.Password = txtPassword.Text.Trim();
int options =
MQC.MQOO_INPUT_SHARED + MQC.MQOO_FAIL_IF_QUIESCING +
MQC.MQOO_SAVE_ALL_CONTEXT + MQC.MQOO_INQUIRE;
sourceQueue = sourceManager.AccessQueue
(sourceQStr, options, sourceManager.Name, null, txtUserId.Text.Trim());
options =
MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING +
MQC.MQOO_PASS_ALL_CONTEXT + MQC.MQOO_INQUIRE;
targetQueue = targetManager.AccessQueue
(targetQStr, options, targetManager.Name, null, txtUserId.Text.Trim());
int i = 0;
try
{
i = sourceQueue.CurrentDepth;
}
catch (Exception ex)
{
MessageBox.Show("Exception: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
gmo.Options = MQC.MQGMO_COMPLETE_MSG + MQC.MQGMO_FAIL_IF_QUIESCING +
MQC.MQGMO_NO_WAIT + MQC.MQGMO_SYNCPOINT;
pmo.Options = MQC.MQPMO_PASS_ALL_CONTEXT +
MQC.MQPMO_SYNCPOINT + MQC.MQPMO_SYNC_RESPONSE;
pmo.ContextReference = sourceQueue;
msg.MessageId = StringToByteArray(targetMsgIdStr);
}
public byte[] StringToByteArray(String hex)
{
hex = hex.Trim();
int NumberChars = hex.Length;
if ((NumberChars % 2) != 0)
{
hex = "0" + hex;
NumberChars++;
}
byte[] bytes = new byte[NumberChars / 2];
for (int i = 0; i < NumberChars; i += 2)
bytes[i / 2] = StringToByte(hex.Substring(i, 2));
return bytes;
}
public byte StringToByte(string hexDigits)
{
if (hexDigits.Length != 2)
return 0;
int high = hexValue(hexDigits[0]);
int low = hexValue(hexDigits[1]);
return (byte)(((high << 4) & 240) | (low & 15));
}
public int hexValue(char c)
{
int retval = 0;
if (c > '9')
retval = ((int)c) - 55;
else
retval = ((int)c) - 48;
return retval;
}
}
The GUI simply prompts for a username and password (which are validated with an LDAP call) and then enables a "test" button which runs the btnTest_Click method. I still get the same exception (MQRC_CONTEXT_HANDLE_ERROR) even thought I have used all the open and get message options specified.
Adding to Morag's answer, here is snippet for moving messages with context. I am also using a SYNC_POINT to ensure the message is removed from source queue after the message is successfully put to the destination queue.
/// <summary>
/// Moves messages from one queue to another with context
/// </summary>
public void moveMessagesWithContext()
{
MQQueueManager qmSource = null;
MQQueueManager qmDestination = null;
MQQueue qSource = null;
MQQueue qDestination = null;
Hashtable htSource = null;
Hashtable htDestination = null;
try
{
htSource = new Hashtable();
htDestination = new Hashtable();
htSource.Add(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_MANAGED);
htSource.Add(MQC.HOST_NAME_PROPERTY, "localhost");
htSource.Add(MQC.PORT_PROPERTY, 2020);
htSource.Add(MQC.CHANNEL_PROPERTY, "A_QM_SVRCONN");
qmSource = new MQQueueManager("A_QM", htSource);
qSource = qmSource.AccessQueue("Q_SOURCE", MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_FAIL_IF_QUIESCING | MQC.MQOO_SAVE_ALL_CONTEXT);
htDestination.Add(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_MANAGED);
htDestination.Add(MQC.HOST_NAME_PROPERTY, "localhost");
htDestination.Add(MQC.PORT_PROPERTY, 3030);
htDestination.Add(MQC.CHANNEL_PROPERTY, "B_QM_SVRCONN");
qmDestination = new MQQueueManager("B_QM", htDestination);
qDestination = qmDestination.AccessQueue("Q_DESTINATION", MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING | MQC.MQOO_PASS_ALL_CONTEXT);
MQMessage msgSource = new MQMessage();
MQGetMessageOptions gmo = new MQGetMessageOptions();
gmo.Options |= MQC.MQGMO_SYNCPOINT;
qSource.Get(msgSource,gmo);
if (msgSource != null)
{
MQMessage msgDestination = new MQMessage();
MQPutMessageOptions pmo = new MQPutMessageOptions();
pmo.ContextReference = qSource;
qDestination.Put(msgSource, pmo);
qmSource.Commit();
}
}
catch (MQException mqEx)
{
qmSource.Backout();
Console.WriteLine(mqEx);
}
catch (Exception otherEx)
{
Console.WriteLine(otherEx);
}
}
The correct options to use to move messages whilst retaining all the context information within them are as follows.
When opening the queue to get messages from, use MQOO_SAVE_ALL_CONTEXT. The context will be saved at get time in the object handle that represents this opened queue.
When opening the queue to put messages to, use MQOO_PASS_ALL_CONTEXT. Then when putting the message, use MQPMO_PASS_ALL_CONTEXT and provide the object handle that contains the context.
Receiving return code MQRC_CONTEXT_HANDLE_ERROR means that you didn't use MQOO_SAVE_ALL_CONTEXT on the first open but then tried to use the object handle on the put. It could also mean that it's just not a correct reference to the first queue. In the MQ API it's an object handle (MQHOBJ) whereas in .Net it's the MQQueue (see MQPutMessageOptions .NET class).
Receiving return code MQRC_OPTIONS_ERROR means that you mixed and matched the options too much.

Categories