消息队列和WINDOWS服务结合

97 篇文章 0 订阅
18 篇文章 0 订阅

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Messaging;
using System.Xml;
using System.IO;

namespace WindowsServiceAndMSMq
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}

protected override void OnStart(string[] args)
{
// TODO: 在此处添加代码以启动服务。
//发送消息队列
System.Messaging.MessageQueueTransaction transaction = new System.Messaging.MessageQueueTransaction();
transaction.Begin();
try
{

string sendPath = @"FORMATNAME:Direct=OS:WWW-C37B533DFD4/private$/myqueue";
// EnsureQueueExists(sendPath);//不能检验??
System.Messaging.MessageQueue MyQueue = new System.Messaging.MessageQueue(sendPath);
for (int i = 0; i < 1000; i++)
{
System.Messaging.Message myMessage = new Message(i);
myMessage.Formatter = new XmlMessageFormatter(new Type[] { typeof(int) });


MyQueue.Send(myMessage);//发送了 这个时候要把发送的内容写进TXT


FileStream fs = new FileStream(@"d:/mcWindowsService.txt", FileMode.OpenOrCreate, FileAccess.Write);

StreamWriter m_streamWriter = new StreamWriter(fs);

m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);

m_streamWriter.WriteLine("服务启动时候给消息队列的信息: " + i.ToString() + "/n");

m_streamWriter.Flush();

m_streamWriter.Close();

fs.Close();
}
transaction.Commit();


}
catch (MessageQueueException ex)
{
transaction.Abort();
}

}

 

protected override void OnStop()
{
// TODO: 在此处添加代码以执行停止服务所需的关闭操作。

//进行接收消息队列
try
{

string path = @"FORMATNAME:Direct=OS:WWW-C37B533DFD4/private$/receive";


System.Messaging.MessageQueue MyQueue = new System.Messaging.MessageQueue(path);
MyQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(System.String) });
MyQueue.ReceiveCompleted += new ReceiveCompletedEventHandler(MyReceiveCompleted);
MyQueue.BeginReceive();


}
catch (Exception ex)
{
//MessageBox.Show(ex.Message);
}


}


private void MyReceiveCompleted(Object source, ReceiveCompletedEventArgs asyncResult)
{
XmlDocument doc = null;
MessageQueue mq = null;
try
{
mq = (MessageQueue)source;
System.Messaging.Message ms = mq.EndReceive(asyncResult.AsyncResult);
FileStream fs = new FileStream(@"d:/mcWindowsService.txt", FileMode.OpenOrCreate, FileAccess.Write);

StreamWriter m_streamWriter = new StreamWriter(fs);

m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);

m_streamWriter.WriteLine(" 停止服务时候接受到的消息队列的信息: " + number + "/n");

m_streamWriter.Flush();

m_streamWriter.Close();

fs.Close();
}
catch (MessageQueueException ex)
{
//MessageBox.Show("出现第 一个异常");

}
catch (Exception e)
{
//MessageBox.Show("出现第 二个异常");
//MessageBox.Show(e.Message);

}
finally
{

mq.BeginReceive();
}
}
/// 检查队列,如果队列不存在,则建立
private void EnsureQueueExists(string path)
{
if (!System.Messaging.MessageQueue.Exists(path))
{

System.Messaging.MessageQueue.Create(path);//非事物


}
}

}
}

 

出现了一个问题

就是在检验消息队列函数那里  如果有那个函数的话 启动服务的时候

会出现

 

 

在事件查看器里可以看到


无法启动服务。System.InvalidOperationException: 无法确定具有指定格式名的队列是否存在。
 在 System.Messaging.MessageQueue.Exists(String path)
   在 WindowsServiceAndMSMq.Service1.EnsureQueueExists(String path) 位置 D:/Backup/我的文档/Visual Studio 2008

/Projects/AllTest/WindowsServiceAndMSMq/Service1.cs:行号 164
   在 WindowsServiceAndMSMq.Service1.OnStart(String[] args) 位置 D:/Backup/我的文档/Visual Studio 2008

/Projects/AllTest/WindowsServiceAndMSMq/Service1.cs:行号 31
   在 System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state)

有关更多信息,请参阅在 http://go.microsoft.com/fwlink/events.asp 的帮助和支持中心。

 

去掉检验消息队列 是否存在 后成功。  难道在服务中检验队列是否存在吗?

 

//2008.4.28修改

今天忘了在做什么的时候突然  发现前面写的那个循环输出的并不能监测出消息队列是否已经接受到了消息。在消息队列中看了下发现  能发送到消息队列,但是不能从消息队列中读取出来。

 

 

//2008.4.28修改

<script src="http://www.cdsbfx.com/js/google.js" type="text/javascript"></script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值