IDE:VS 2010,Windows服务.net 4.0
我创建了一个简单的Windows服务,并使用以下代码测试了此服务:
protected override void OnStart(string[] args)
{
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"F:\22Yardz_Pro\" + "error.txt", true))
{
file.WriteLine("Service is working \n");
}
//ReceiveMsmqMessage();
}
这里的安装程序我已经设置了
serviceProcessInstaller1. Account = Local System
然后我将代码更改为
** protected override void OnStart(string[] args)
{
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"F:\MyProj\" + "error.txt", true))
{
file.WriteLine("Service is working \n");
}
ReceiveMsmqMessage();
}
private static void ReceiveMsmqMessage()
{
//string MsmqPath = System.Configuration.ConfigurationManager.AppSettings["MsmqPath"];
MessageQueue queue = new MessageQueue(@".\Private$\MyProjQ");
System.Messaging.Message myMessage = queue.Receive();
myMessage.Formatter = new XmlMessageFormatter(new String[] { "System.String,mscorlib" });
string message = (myMessage.Body.ToString());
string labelName = myMessage.Label;
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"F:\MyProj\" + "error.txt", true))
{
file.WriteLine("Service started " + message + " \n");
}
}**
你能告诉我为什么我的msmq代码不工作,我已经在winForms项目中正确地测试了这段代码,这也是在调试模式下工作但是当我使用cmd安装服务时和进入services.msc之后我启动此服务我收到错误
The _MsmqTesting service on local computer has started and then stopped. Some services stop automatically if there are not in use by other services or programs
你能告诉我我在做什么错吗?
答案 0 :(得分:0)
最后它工作了,我把我的代码放在Try catch块中,并且在catch块中我已经在txt文件中编写了异常消息,所以我知道我的队列对于本地或系统用户是不可访问的,所以我改变了我的队列可访问性,它工作。