How to submit a message into Message Queue?

B

Bishoy George

I have an application that get data and write xml file from this data.
The application should put the data as a message in the Message Queuing.
Every time I encounter an error saying: "The queue does not exist or you do
not have sufficient permissions to perform the operation".

This is designer code:
---------------------

namespace ReceiveOrder
{
partial class ReceiveOrder
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.Status = new System.Windows.Forms.Label();
this.WelcomeMessage = new System.Windows.Forms.Label();
this.ProductID = new System.Windows.Forms.TextBox();
this.Quantity = new System.Windows.Forms.TextBox();
this.EmailAddress = new System.Windows.Forms.TextBox();
this.Checkout = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 26);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(55, 13);
this.label1.TabIndex = 0;
this.label1.Text = "ProductID";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(12, 66);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(49, 13);
this.label2.TabIndex = 1;
this.label2.Text = "Quantity";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(12, 106);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(73, 13);
this.label3.TabIndex = 2;
this.label3.Text = "Email Address";
//
// Status
//
this.Status.AutoSize = true;
this.Status.Location = new System.Drawing.Point(12, 196);
this.Status.Name = "Status";
this.Status.Size = new System.Drawing.Size(0, 13);
this.Status.TabIndex = 3;
//
// WelcomeMessage
//
this.WelcomeMessage.AutoSize = true;
this.WelcomeMessage.Location = new System.Drawing.Point(12,
236);
this.WelcomeMessage.Name = "WelcomeMessage";
this.WelcomeMessage.Size = new System.Drawing.Size(0, 13);
this.WelcomeMessage.TabIndex = 4;
//
// ProductID
//
this.ProductID.Location = new System.Drawing.Point(88, 22);
this.ProductID.Name = "ProductID";
this.ProductID.Size = new System.Drawing.Size(243, 20);
this.ProductID.TabIndex = 5;
//
// Quantity
//
this.Quantity.Location = new System.Drawing.Point(88, 62);
this.Quantity.Name = "Quantity";
this.Quantity.Size = new System.Drawing.Size(243, 20);
this.Quantity.TabIndex = 6;
//
// EmailAddress
//
this.EmailAddress.Location = new System.Drawing.Point(88, 102);
this.EmailAddress.Name = "EmailAddress";
this.EmailAddress.Size = new System.Drawing.Size(243, 20);
this.EmailAddress.TabIndex = 7;
//
// Checkout
//
this.Checkout.Location = new System.Drawing.Point(88, 141);
this.Checkout.Name = "Checkout";
this.Checkout.Size = new System.Drawing.Size(75, 23);
this.Checkout.TabIndex = 8;
this.Checkout.Text = "Checkout!";
this.Checkout.UseVisualStyleBackColor = true;
this.Checkout.Click += new
System.EventHandler(this.Checkout_Click);
//
// ReceiveOrder
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(343, 197);
this.Controls.Add(this.Checkout);
this.Controls.Add(this.EmailAddress);
this.Controls.Add(this.Quantity);
this.Controls.Add(this.ProductID);
this.Controls.Add(this.WelcomeMessage);
this.Controls.Add(this.Status);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "ReceiveOrder";
this.Text = "Receive Order";
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label Status;
private System.Windows.Forms.Label WelcomeMessage;
private System.Windows.Forms.TextBox ProductID;
private System.Windows.Forms.TextBox Quantity;
private System.Windows.Forms.TextBox EmailAddress;
private System.Windows.Forms.Button Checkout;
}
}


-----------------------------------------------------------------------------------------


This is my code:
---------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Messaging;
using System.Globalization;

namespace ReceiveOrder
{
public partial class ReceiveOrder : Form
{

AccessControlList acl;
MessageQueue queue;

public ReceiveOrder()
{
InitializeComponent();

MakeAccessControlList();
}

private void MakeAccessControlList()
{
//Set Permissions on queue with AccessControlList
//Grant permissions to multiple users at the same time

acl = new AccessControlList();

AccessControlEntry ace = new AccessControlEntry();
ace.Trustee = new Trustee(@"bishoylaptop\bishoy");
ace.EntryType = AccessControlEntryType.Deny;
ace.GenericAccessRights = GenericAccessRights.Write;
ace.StandardAccessRights = StandardAccessRights.Write;

acl.Add(ace);
}

private void SetQueuePermissions()
{
queue.SetPermissions(acl);

//Set Permissions on queue with MessageQueueAccessControlEntry
MessageQueueAccessControlEntry msmqace = new
MessageQueueAccessControlEntry(new Trustee("Everyone"),
MessageQueueAccessRights.WriteMessage, AccessControlEntryType.Allow);
queue.SetPermissions(msmqace);

//Set permissions on queue with those last 2 overloads.
queue.SetPermissions(@"bishoylaptop\bishoy",
MessageQueueAccessRights.ReceiveMessage);
queue.SetPermissions(@"bishoylaptop\bishoy",
MessageQueueAccessRights.WriteMessage, AccessControlEntryType.Allow);

}

private void SendMessage(StringWriter sw, DateTime dt)
{
// Send the XML document to the message queue.
System.Messaging.Message msg = new
System.Messaging.Message(sw.ToString(), new ActiveXMessageFormatter());
queue = new MessageQueue(@".\Private$\MyOrderQueue");

SetQueuePermissions();

if (queue.Transactional) // Gets a value that indicates
whether the queue accepts only transactions
{
MessageQueueTransaction trans1 = new
MessageQueueTransaction();
trans1.Begin();
queue.Send(msg, "Order[" + dt + "]", trans1);
trans1.Commit();
}
else
{
queue.Send(msg, "Order[" + dt + "]");

}

sw.Close();

// Confirm that the order was received.
this.Status.Text = "Thank you, your order was received at " + dt
+ ".";
}

private void Checkout_Click(object sender, EventArgs e)
{
// Create an XmlWriter with an XmlWriterSettings object that
specifies indentation and new lines for attributes.
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = "\t";
settings.NewLineOnAttributes = true; // This will write
attributes on new line
StringWriter sw = new
StringWriter(CultureInfo.InvariantCulture); // Culture Independent
XmlWriter writer = XmlWriter.Create(sw, settings); // Create
9th overload is used as StringWriter : TextWriter

// Call the WriteStartDocument method.
writer.WriteStartDocument();

// Writing a comment to the XML Document.
writer.WriteComment("This message was generated automatically");

// Writing the root element start tag.
writer.WriteStartElement("Order");

// Writing the Email and Timestamp attributes.
writer.WriteAttributeString("EmailAddress",
this.EmailAddress.Text);
DateTime dt = DateTime.Now;
writer.WriteStartAttribute("Timestamp");
writer.WriteValue(dt);
writer.WriteEndAttribute();

// Writing the Product and Quantity elements.
writer.WriteStartElement("ProductID");
writer.WriteValue(XmlConvert.ToInt32(this.ProductID.Text));
writer.WriteEndElement();

writer.WriteStartElement("Quantity");
writer.WriteValue(XmlConvert.ToInt32(this.Quantity.Text));
writer.WriteEndElement();

// Writing the Root Element closing tag.
writer.WriteEndElement();

// Finish writing the XML document.
writer.WriteEndDocument();
writer.Close();

SendMessage(sw, dt);
}
}
}
 
R

Robbe Morris [C# MVP]

Could a lot of things regarding permissions. Here is
a code sample on it that might be useful:

http://www.eggheadcafe.com/articles/20021125.asp
http://www.eggheadcafe.com/articles/20040620.asp

--
Robbe Morris - 2004-2006 Microsoft MVP C#
I've mapped the database to .NET class properties and methods to
implement an multi-layered object oriented environment for your
data access layer. Thus, you should rarely ever have to type the words
SqlCommand, SqlDataAdapter, or SqlConnection again.
http://www.eggheadcafe.com/articles/adonet_source_code_generator.asp





Bishoy George said:
I have an application that get data and write xml file from this data.
The application should put the data as a message in the Message Queuing.
Every time I encounter an error saying: "The queue does not exist or you
do not have sufficient permissions to perform the operation".

This is designer code:
---------------------

namespace ReceiveOrder
{
partial class ReceiveOrder
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.Status = new System.Windows.Forms.Label();
this.WelcomeMessage = new System.Windows.Forms.Label();
this.ProductID = new System.Windows.Forms.TextBox();
this.Quantity = new System.Windows.Forms.TextBox();
this.EmailAddress = new System.Windows.Forms.TextBox();
this.Checkout = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 26);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(55, 13);
this.label1.TabIndex = 0;
this.label1.Text = "ProductID";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(12, 66);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(49, 13);
this.label2.TabIndex = 1;
this.label2.Text = "Quantity";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(12, 106);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(73, 13);
this.label3.TabIndex = 2;
this.label3.Text = "Email Address";
//
// Status
//
this.Status.AutoSize = true;
this.Status.Location = new System.Drawing.Point(12, 196);
this.Status.Name = "Status";
this.Status.Size = new System.Drawing.Size(0, 13);
this.Status.TabIndex = 3;
//
// WelcomeMessage
//
this.WelcomeMessage.AutoSize = true;
this.WelcomeMessage.Location = new System.Drawing.Point(12,
236);
this.WelcomeMessage.Name = "WelcomeMessage";
this.WelcomeMessage.Size = new System.Drawing.Size(0, 13);
this.WelcomeMessage.TabIndex = 4;
//
// ProductID
//
this.ProductID.Location = new System.Drawing.Point(88, 22);
this.ProductID.Name = "ProductID";
this.ProductID.Size = new System.Drawing.Size(243, 20);
this.ProductID.TabIndex = 5;
//
// Quantity
//
this.Quantity.Location = new System.Drawing.Point(88, 62);
this.Quantity.Name = "Quantity";
this.Quantity.Size = new System.Drawing.Size(243, 20);
this.Quantity.TabIndex = 6;
//
// EmailAddress
//
this.EmailAddress.Location = new System.Drawing.Point(88, 102);
this.EmailAddress.Name = "EmailAddress";
this.EmailAddress.Size = new System.Drawing.Size(243, 20);
this.EmailAddress.TabIndex = 7;
//
// Checkout
//
this.Checkout.Location = new System.Drawing.Point(88, 141);
this.Checkout.Name = "Checkout";
this.Checkout.Size = new System.Drawing.Size(75, 23);
this.Checkout.TabIndex = 8;
this.Checkout.Text = "Checkout!";
this.Checkout.UseVisualStyleBackColor = true;
this.Checkout.Click += new
System.EventHandler(this.Checkout_Click);
//
// ReceiveOrder
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(343, 197);
this.Controls.Add(this.Checkout);
this.Controls.Add(this.EmailAddress);
this.Controls.Add(this.Quantity);
this.Controls.Add(this.ProductID);
this.Controls.Add(this.WelcomeMessage);
this.Controls.Add(this.Status);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "ReceiveOrder";
this.Text = "Receive Order";
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label Status;
private System.Windows.Forms.Label WelcomeMessage;
private System.Windows.Forms.TextBox ProductID;
private System.Windows.Forms.TextBox Quantity;
private System.Windows.Forms.TextBox EmailAddress;
private System.Windows.Forms.Button Checkout;
}
}


-----------------------------------------------------------------------------------------


This is my code:
---------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Messaging;
using System.Globalization;

namespace ReceiveOrder
{
public partial class ReceiveOrder : Form
{

AccessControlList acl;
MessageQueue queue;

public ReceiveOrder()
{
InitializeComponent();

MakeAccessControlList();
}

private void MakeAccessControlList()
{
//Set Permissions on queue with AccessControlList
//Grant permissions to multiple users at the same time

acl = new AccessControlList();

AccessControlEntry ace = new AccessControlEntry();
ace.Trustee = new Trustee(@"bishoylaptop\bishoy");
ace.EntryType = AccessControlEntryType.Deny;
ace.GenericAccessRights = GenericAccessRights.Write;
ace.StandardAccessRights = StandardAccessRights.Write;

acl.Add(ace);
}

private void SetQueuePermissions()
{
queue.SetPermissions(acl);

//Set Permissions on queue with MessageQueueAccessControlEntry
MessageQueueAccessControlEntry msmqace = new
MessageQueueAccessControlEntry(new Trustee("Everyone"),
MessageQueueAccessRights.WriteMessage, AccessControlEntryType.Allow);
queue.SetPermissions(msmqace);

//Set permissions on queue with those last 2 overloads.
queue.SetPermissions(@"bishoylaptop\bishoy",
MessageQueueAccessRights.ReceiveMessage);
queue.SetPermissions(@"bishoylaptop\bishoy",
MessageQueueAccessRights.WriteMessage, AccessControlEntryType.Allow);

}

private void SendMessage(StringWriter sw, DateTime dt)
{
// Send the XML document to the message queue.
System.Messaging.Message msg = new
System.Messaging.Message(sw.ToString(), new ActiveXMessageFormatter());
queue = new MessageQueue(@".\Private$\MyOrderQueue");

SetQueuePermissions();

if (queue.Transactional) // Gets a value that indicates
whether the queue accepts only transactions
{
MessageQueueTransaction trans1 = new
MessageQueueTransaction();
trans1.Begin();
queue.Send(msg, "Order[" + dt + "]", trans1);
trans1.Commit();
}
else
{
queue.Send(msg, "Order[" + dt + "]");

}

sw.Close();

// Confirm that the order was received.
this.Status.Text = "Thank you, your order was received at " +
dt + ".";
}

private void Checkout_Click(object sender, EventArgs e)
{
// Create an XmlWriter with an XmlWriterSettings object that
specifies indentation and new lines for attributes.
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = "\t";
settings.NewLineOnAttributes = true; // This will write
attributes on new line
StringWriter sw = new
StringWriter(CultureInfo.InvariantCulture); // Culture Independent
XmlWriter writer = XmlWriter.Create(sw, settings); // Create
9th overload is used as StringWriter : TextWriter

// Call the WriteStartDocument method.
writer.WriteStartDocument();

// Writing a comment to the XML Document.
writer.WriteComment("This message was generated
automatically");

// Writing the root element start tag.
writer.WriteStartElement("Order");

// Writing the Email and Timestamp attributes.
writer.WriteAttributeString("EmailAddress",
this.EmailAddress.Text);
DateTime dt = DateTime.Now;
writer.WriteStartAttribute("Timestamp");
writer.WriteValue(dt);
writer.WriteEndAttribute();

// Writing the Product and Quantity elements.
writer.WriteStartElement("ProductID");
writer.WriteValue(XmlConvert.ToInt32(this.ProductID.Text));
writer.WriteEndElement();

writer.WriteStartElement("Quantity");
writer.WriteValue(XmlConvert.ToInt32(this.Quantity.Text));
writer.WriteEndElement();

// Writing the Root Element closing tag.
writer.WriteEndElement();

// Finish writing the XML document.
writer.WriteEndDocument();
writer.Close();

SendMessage(sw, dt);
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top