C# 安装窗口服务 System.Security.SecurityException 时出错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10426117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error while installing window service System.Security.SecurityException
提问by sunder
I created a window service and to install it I created its deployment project and installed that. After installing I stared it. It successfully started.
我创建了一个窗口服务并安装它,我创建了它的部署项目并安装了它。安装后我盯着它。它成功启动。
Next day I made some modification, and rebuild and reinstalled but now its not installing.
第二天我做了一些修改,重建并重新安装,但现在没有安装。
Then I thought its issue with installer, lets create a custom installer for service so that anytime I can update my code.
然后我想到了安装程序的问题,让我们为服务创建一个自定义安装程序,以便我可以随时更新我的代码。
I created it like this incase if anyone need this for future.
如果将来有人需要它,我会像这样创建它。
public class MyInstaller : Installer
{
ServiceProcessInstaller spi;
ServiceInstaller si;
public MyInstaller()
{
spi = new ServiceProcessInstaller();
spi.Account = ServiceAccount.LocalSystem;
si = new ServiceInstaller();
si.StartType = ServiceStartMode.Manual;
si.ServiceName = "MyService";
si.DisplayName = "My Service";
si.Description = "service installed from command line";
this.Installers.Add(spi);
this.Installers.Add(si);
}
}
I called it from main method by check the parameter args.
我通过检查参数args从main方法调用它。
case "-i":
case "-install":
ti = new TransactedInstaller();
mi = new MyInstaller();
ti.Installers.Add(mi);
string logPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\install.log";
ctx = new InstallContext(logPath, cmdline);
ti.Context = ctx; //.Context ( ctx );
ti.Install(new Hashtable());
break;
Now when I am trying to install. I recevied error System.Security.SecurityException: The source was not found, but some or all event logs could not be searched. Inaccessible logs: Security.
现在,当我尝试安装时。我收到错误 System.Security.SecurityException: The source was not found, 但无法搜索部分或全部事件日志。无法访问的日志:安全。
I google it, and come to know service will try to access application log while installing and write log there.
我用谷歌搜索,发现服务会在安装和写入日志时尝试访问应用程序日志。
I am not writing any event log. I have my log4net for logging. But still its default behaviour.
我没有写任何事件日志。我有我的 log4net 用于记录。但仍然是它的默认行为。
How to overcome this issue now? Its not getting installed even I have all permissions.
现在如何克服这个问题?即使我拥有所有权限,它也没有安装。
Thanks
谢谢
采纳答案by Eben Roux
I have found that at times you may need to "Run as Administrator". If you are installing from a command prompt you may need to start thatup with "Run as Administrator".
我发现有时您可能需要“以管理员身份运行”。如果您从命令提示符安装,您可能需要使用“以管理员身份运行”来启动它。
回答by Jay Carlton
I might have had a separate root cause for the message, but I fixed it by changing my service to run as LocalSystem (which is what I want anyway) instead of LocalService.
我可能对该消息有一个单独的根本原因,但我通过将我的服务更改为作为 LocalSystem(无论如何我想要的)而不是 LocalService 运行来修复它。
回答by ingconti
I can confirm that under "windows 7 64 bit" AND "Windows 10" you must:
我可以确认在“Windows 7 64 位”和“Windows 10”下,您必须:
1) run Visual studio command prompt AS ADMINISTRATOR (right click.. Other.. tun as admin)
1)以管理员身份运行Visual Studio命令提示符(右键单击..其他..以管理员身份运行)
2) go to "obj" folder where You have the exe. (cd [all path to \obj] ) 3) launch installutil [myservice.exe]
2)转到您拥有exe的“obj”文件夹。(cd [all path to \obj] ) 3) 启动 installutil [myservice.exe]
if not run as "admin", it fails even on old win7. :(
如果不以“管理员”身份运行,即使在旧的 win7 上也会失败。:(
note: MSDN does explain it:
注意:MSDN 确实解释了它:
"To install a Windows service, you must have administrative credentials on the computer on which you're installing it."
“要安装 Windows 服务,您必须在安装它的计算机上拥有管理凭据。”
:)
:)


