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 26, 2013, 1:45 AM
Add formatter and reader the body value from the message instance.
Please let me know, does it solves your problem or not.
Vikram NPosted Sep 26, 2013, 6:53 AM
Posted Sep 26, 2013, 6:49 AM
Vikram NPosted Sep 26, 2013, 6:12 AM
Since we are returning single value it is obvious that only one value is returned.
Vikram NPosted Sep 26, 2013, 5:08 AM
Posted Sep 26, 2013, 4:45 AM
We're not assigned the values res.append(themessage.body.tostring());
Vikram NPosted Sep 26, 2013, 4:43 AM
Posted Sep 26, 2013, 4:41 AM
Vikram NPosted Sep 26, 2013, 4:37 AM
res.Append(theMessage.Body.ToString());
Posted Sep 26, 2013, 4:33 AM
Modified code
Vikram NPosted Sep 26, 2013, 4:29 AM
Posted Sep 26, 2013, 4:24 AM
Vikram NPosted Sep 26, 2013, 4:20 AM
Posted Sep 26, 2013, 4:18 AM
Vikram NPosted Sep 26, 2013, 4:16 AM
Posted Sep 26, 2013, 3:36 AM
Vikram NPosted Sep 26, 2013, 2:58 AM
Vikram NPosted Sep 26, 2013, 2:31 AM
In this line : result.Append(theMessage.Body.ToString());
Posted Sep 26, 2013, 2:26 AM
StringBuilder result = new StringBuilder();
Vikram NPosted Sep 26, 2013, 2:21 AM
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 26, 2013, 2:21 AM
Vikram NPosted Sep 26, 2013, 1:36 AM
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 26, 2013, 1:31 AM
foreach (System.Messaging.Message theMessage in AllMessages)
}
If yes, what is the value that you're getting in the strMessage varaible?
Vikram NPosted Sep 26, 2013, 1:28 AM
Vikram NPosted Sep 26, 2013, 1:13 AM
Kindly help me out :)
Thanks a lot for your help.
Posted Sep 26, 2013, 1:11 AM
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 NPosted Sep 26, 2013, 1:08 AM
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 26, 2013, 1:04 AM
These are the references of windows form controls.
Vikram NPosted Sep 26, 2013, 1:02 AM
Ok I shall do it now.
But I have not mixed the above code with winform ryt?
Posted Sep 26, 2013, 12:58 AM
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.