C# Windows服务出现错误-System.Security.SecurityException
感谢您阅读我的帖子,我正在编写Windows服务,当我尝试启动它时,EventViewer上出现以下错误:C# Windows服务出现错误-System.Security.SecurityException,c#,windows-services,C#,Windows Services,感谢您阅读我的帖子,我正在编写Windows服务,当我尝试启动它时,EventViewer上出现以下错误: Application: SerivicioBIOHAcademico.exe Framework Version: v4.0.30319 Description: The process was terminated due to an unhandled exception. Exception Info: System.Security.SecurityException Stack
Application: SerivicioBIOHAcademico.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.Security.SecurityException
Stack:
at System.Diagnostics.EventLog.FindSourceRegistration(System.String, System.String, Boolean, Boolean)
at System.Diagnostics.EventLog.SourceExists(System.String, System.String, Boolean)
at System.Diagnostics.EventLog.SourceExists(System.String)
at SerivicioBIOHAcademico.BIOHAcad..ctor()
at SerivicioBIOHAcademico.Program.Main()
下面是我的服务应用程序(C#)中的一些代码
希望您能帮助我解决这个问题,提前谢谢。我修复了它,似乎错误是因为我从另一个服务名称中获得了相同的日志名,我更改了它,现在它似乎可以工作了(假设它已启动) 我用另一个博客上的这个来解决这个错误,并找出问题所在
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Diagnostics;
namespace SerivicioBIOHAcademico
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
try
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new BIOHAcad()
};
ServiceBase.Run(ServicesToRun);
}
catch(Exception ex)
{
string SourceName = "WindowsService.ExceptionLog";
if (!EventLog.SourceExists(SourceName))
{
EventLog.CreateEventSource(SourceName, "Application");
}
EventLog eventLog = new EventLog();
eventLog.Source = SourceName;
string message = string.Format("Exception: {0} \n\nStack: {1}", ex.Message, ex.StackTrace);
eventLog.WriteEntry(message, EventLogEntryType.Error);
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.ServiceProcess;
使用系统文本;
使用系统诊断;
命名空间SerivicioBIOHAcademico
{
静态类程序
{
///
///应用程序的主要入口点。
///
静态void Main()
{
尝试
{
ServiceBase[]ServicesToRun;
ServicesToRun=新的ServiceBase[]
{
新BIOHAcad()
};
ServiceBase.Run(ServicesToRun);
}
捕获(例外情况除外)
{
字符串SourceName=“WindowsService.ExceptionLog”;
如果(!EventLog.SourceExists(SourceName))
{
CreateEventSource(SourceName,“应用程序”);
}
EventLog EventLog=新事件日志();
eventLog.Source=SourceName;
string message=string.Format(“异常:{0}\n\n堆栈:{1}”,例如message,例如StackTrace);
WriteEntry(消息,EventLogEntryType.Error);
}
}
}
}
它给了我需要的错误我在这里遇到了相同的问题,里卡多里奥斯提供的错误处理帮助我找出了问题的原因。我想我会写一篇文章来节省别人的时间。如果自定义日志名的前8个字符不唯一,则可能会遇到此错误。这就是我收到的错误:
例外:只有自定义日志名称的前八个字符是有效的,并且系统上已经有另一个日志使用给定名称的前八个字符 对我来说,当我完全关注微软自己的文章时,访问安全事件日志是一个例外
EventLog.SourceExists(eventLogSource)
需要在try/catch
块中进行检查。在我的情况下,我确保我的服务作为本地系统运行,然后工作正常。您的异常是System.Security.SecurityException
而不是System.IO.DirectoryNotFoundException
。尝试以更合适的用户身份运行它(尝试LocalSystem)。哪行代码引发异常?错误1053,更改为LocalSystem仍然为nothing。现在它给了我两个错误:EventType clr20r3,P1 seriviobioacademico.exe,p21.0.0,P3 4f58c7ba,P4系统,P5 4.0.0.0,P6 4ea7ab8b,P7 3bf8,P8 2e1,P9系统参数异常,P10 NIL。和..应用程序:SerivicioBIOHAcademico.exe框架版本:v4.0.30319说明:由于未处理的异常,进程已终止。异常信息:System.ArgumentException Stack:at System.Diagnostics.EventLog.CreateEventSource(System.Diagnostics.EventSourceCreationData)at System.Diagnostics.EventLog.CreateEventSource(System.String,System.String)at SerivicioBIOHAcademico.BioHacademico..ctor()at SerivicioBIOHAcademico.Program.Main()我遇到了类似的问题。作为本地系统运行为我修复了它。谢谢,巴布科克先生
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Diagnostics;
namespace SerivicioBIOHAcademico
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
try
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new BIOHAcad()
};
ServiceBase.Run(ServicesToRun);
}
catch(Exception ex)
{
string SourceName = "WindowsService.ExceptionLog";
if (!EventLog.SourceExists(SourceName))
{
EventLog.CreateEventSource(SourceName, "Application");
}
EventLog eventLog = new EventLog();
eventLog.Source = SourceName;
string message = string.Format("Exception: {0} \n\nStack: {1}", ex.Message, ex.StackTrace);
eventLog.WriteEntry(message, EventLogEntryType.Error);
}
}
}
}