Click here to Skip to main content
13,597,704 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
Hello,
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>
    /// Summary description for Service1
    /// </summary>

    [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 25-Sep-13 17:55pm
Comments
26-Sep-13 6:58am
   
Hmmm even that is not working. Actually I have modified my code as below :
[WebMethod]
public string DisplayMessage()
{
bool NoMessage = true;
MessageQueue Q1 = new MessageQueue(QueueName);
Q1.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
StringBuilder res = new StringBuilder();
System.Messaging.Message[] AllMessages = Q1.GetAllMessages();
foreach (System.Messaging.Message theMessage in AllMessages)
{
NoMessage = false;
res = res.Append(theMessage.Body.ToString());
return res.ToString();
turn strMessage.ToString();
}

if (NoMessage)
{
return "Empty Queue";
}
return "Success";
}


}
But the problem here is only one value is returned. that is only the last content of the queue is getting displayed.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Your want to use console.WriterLine() for writing in browser? it is wrong!
Use Response.Write()
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01-2016 | 2.8.180621.3 | Last Updated 26 Sep 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100