C# 我的自定义Windows服务无法使用Windows消息队列
我编码的windows服务不工作:C# 我的自定义Windows服务无法使用Windows消息队列,c#,wcf,windows-services,C#,Wcf,Windows Services,我编码的windows服务不工作: 预期功能:服务应该从Windows消息队列(MSMQ)接收消息,并将消息写入.txt文件 当我运行它而不是作为服务(直接从VisualStudio)时,它会工作 当我将它作为服务安装时,我可以启动它,但它什么都不做,不在任何地方创建/写入.txt文件 (我知道它没有在其他地方写入文件,因为当我从VS运行程序时,消息仍然在队列中,因此它们没有被服务取出) 将其作为服务运行与从visual studio运行之间的区别如下: namespace Componen
- 预期功能:服务应该从Windows消息队列(MSMQ)接收消息,并将消息写入.txt文件
- 当我运行它而不是作为服务(直接从VisualStudio)时,它会工作
- 当我将它作为服务安装时,我可以启动它,但它什么都不做,不在任何地方创建/写入.txt文件
namespace ComponentAtrapador
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
#if DEBUG
Service1 myService = new Service1();
myService.startMethod();
System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
#else
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
#endif
}
}
}
这就是日志非常有用的地方,将两个eventLog1.WriteEntry()放在您的OnTimer方法中,您将看到它失败的地方。 我会查的
这就是日志非常有用的地方,将两个eventLog1.WriteEntry()放在您的OnTimer方法中,您将看到它失败的地方。 我会查的
结果发现该服务没有足够的权限,通过将visual studio上该服务的serviceProcessInstaller设置为User进行修复,以便在我安装该服务时,它会要求提供凭据。只要输入“/[username]”就可以了
另一种修复方法是进入任务管理器>服务>右键单击服务>属性>安全性。然后在那里更改权限。发现该服务没有足够的权限,通过将visual studio上该服务的serviceProcessInstaller设置为User来修复它,以便在我安装它时它会要求提供凭据。只要输入“/[username]”就可以了
另一种修复方法是进入任务管理器>服务>右键单击服务>属性>安全性。并在此处更改权限。精心设计的问题。谢谢。当您作为窗口服务运行与从VisualStudio运行时,这里有两个相关的区别。1) 默认情况下,该程序将以本地系统帐户而不是登录的用户帐户运行。2) 工作目录是system32文件夹,而不是exe所在的位置。请检查您的windows服务是否配置了可以访问专用队列的帐户@“\Private$\SomeTestName尝试使用服务属性屏幕更改帐户上的windows服务日志。精心编制的问题。谢谢。当您作为窗口服务运行与从VisualStudio运行时,这里有两个相关的区别。1) 默认情况下,该程序将以本地系统帐户而不是登录的用户帐户运行。2) 工作目录是system32文件夹,而不是exe所在的文件夹。请检查您的windows服务是否配置了可以访问专用队列@“\private$\SomeTestName”的帐户。尝试使用“服务属性”屏幕更改windows服务登录帐户。
namespace ComponentAtrapador
{
public partial class Service1 : ServiceBase
{
private System.Timers.Timer _timer;
private System.ComponentModel.IContainer components1;
private System.Diagnostics.EventLog eventLog1;
public void startMethod()
{
OnStart(null);
}
public Service1()
{
InitializeComponent();
eventLog1 = new System.Diagnostics.EventLog();
if (!System.Diagnostics.EventLog.SourceExists("MySource"))
{
System.Diagnostics.EventLog.CreateEventSource(
"MySource", "MyNewLog");
}
eventLog1.Source = "MySource";
eventLog1.Log = "MyNewLog";
}
protected override void OnStart(string[] args)
{
eventLog1.WriteEntry("In OnStart");
_timer = new System.Timers.Timer();
_timer.Interval = 5000; // 5 seconds
_timer.Elapsed += new System.Timers.ElapsedEventHandler(this.OnTimer);
_timer.Start();
}
protected override void OnStop()
{
_timer.Stop();
}
public void OnTimer(object sender, System.Timers.ElapsedEventArgs args)
{
string nombreArchivo = "archivoMensaje";
MessageQueue messageQueue = new MessageQueue(@".\Private$\SomeTestName");
System.Messaging.Message[] messages = messageQueue.GetAllMessages();
System.Messaging.Message m = new System.Messaging.Message();
foreach (System.Messaging.Message message in messages)
{
message.Formatter = new XmlMessageFormatter(new String[] { "System.String,mscorlib" });
string text = message.Body.ToString();
System.IO.File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory + nombreArchivo + Properties.Settings.Default.SettingNumero + ".txt", text);
Properties.Settings.Default.SettingNumero++;
Properties.Settings.Default.Save();
//Do something with the message.
}
// after all processing, delete all the messages
messageQueue.Purge();
//}
}
}
}