Below is the code I have written to display the contents of an MSMQ queue. It works fine in case of winforms. But When i create a web application, Though the queue has some data, Nothing is displayed.
Can u help me..?
Thanks in advance.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Messaging;
using System.Text;
using System.Windows.Forms;
namespace MSMQWebService
{
///
/// Summary description for Service1
///
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class Service1 : System.Web.Services.WebService
{
string QueueName = ".\\Private$\\q1";
[WebMethod]
public string DisplayMessage()
{
bool NoMessage = true;
MessageQueue Q1 = new MessageQueue(QueueName);
System.Messaging.Message[] AllMessages = Q1.GetAllMessages();
foreach (System.Messaging.Message theMessage in AllMessages)
{
NoMessage = false;
byte[] data = new byte[1024];
theMessage.BodyStream.Read(data, 0, 1024);
string strMessage = ASCIIEncoding.ASCII.GetString(data);
Console.WriteLine(strMessage);
}
if (NoMessage)
{
MessageBox.Show("Message Queue is Empty");
}
return "Success";
}
}

Posted Sep 25, 2013, 9:45 PM EDT
Add formatter and reader the body value from the message instance.
Please let me know, does it solves your problem or not.
Posted Sep 25, 2013, 8:58 PM EDT
Please do not mix windows form application and web service.
Create a separate web service application and call it from your web or windows application.
Definitely that should work.
Please let me know,if it is not working for you.
Vikram N65 reputationPosted Sep 25, 2013, 9:02 PM EDT
Ok I shall do it now.
But I have not mixed the above code with winform ryt?
Posted Sep 25, 2013, 9:04 PM EDT
These are the references of windows form controls.
Vikram N65 reputationPosted Sep 25, 2013, 9:08 PM EDT
yeah MessageBox.Show refers to winforms.(But that is not the point of concern now.)
But the "strMessage" I want to display is not working . Hope I have not used any winform attribute there.
Please correct me If I am wrong as I am new to this .
Posted Sep 25, 2013, 9:11 PM EDT
We will not worry about windows forms controls like messagebox.
Please create a new webservice to get the records from msmq and return it via web service.
I will help you to fix your problem.
Vikram N65 reputationPosted Sep 25, 2013, 9:13 PM EDT
Kindly help me out :)
Thanks a lot for your help.
Vikram N65 reputationPosted Sep 25, 2013, 9:28 PM EDT
Posted Sep 25, 2013, 9:31 PM EDT
foreach (System.Messaging.Message theMessage in AllMessages)
}
If yes, what is the value that you're getting in the strMessage varaible?
Vikram N65 reputationPosted Sep 25, 2013, 9:36 PM EDT
the error I am getting is
********
This page contains the following errors:
Below is a rendering of the page up to the first error.
***************But if I use Console.WriteLine(strMessage).
I will get the blank webpage as the out put.
Posted Sep 25, 2013, 10:21 PM EDT
Vikram N65 reputationPosted Sep 25, 2013, 10:21 PM EDT
I am getting the output, but only the first message is getting displayed. Let me try to display all the messages.
Thanks a ton bro :)
Posted Sep 25, 2013, 10:26 PM EDT
StringBuilder result = new StringBuilder();
Vikram N65 reputationPosted Sep 25, 2013, 10:31 PM EDT
Vikram N65 reputationPosted Sep 25, 2013, 10:58 PM EDT
Posted Sep 25, 2013, 11:36 PM EDT
Vikram N65 reputationPosted Sep 26, 2013, 12:16 AM EDT
Posted Sep 26, 2013, 12:18 AM EDT
Vikram N65 reputationPosted Sep 26, 2013, 12:20 AM EDT
Posted Sep 26, 2013, 12:24 AM EDT
Vikram N65 reputationPosted Sep 26, 2013, 12:29 AM EDT
Posted Sep 26, 2013, 12:33 AM EDT
Modified code
Vikram N65 reputationPosted Sep 26, 2013, 12:37 AM EDT
res.Append(theMessage.Body.ToString());
Posted Sep 26, 2013, 12:41 AM EDT
Vikram N65 reputationPosted Sep 26, 2013, 12:43 AM EDT
Posted Sep 26, 2013, 12:45 AM EDT
We're not assigned the values res.append(themessage.body.tostring());
Vikram N65 reputationPosted Sep 26, 2013, 1:08 AM EDT
Vikram N65 reputationPosted Sep 26, 2013, 2:12 AM EDT
Since we are returning single value it is obvious that only one value is returned.
Posted Sep 26, 2013, 2:49 AM EDT
Vikram N65 reputationPosted Sep 26, 2013, 2:53 AM EDT