安装窗口服务System.Security.SecurityException时出错

sun*_*der 21 c# installation windows-services

我创建了一个窗口服务并安装它,我创建了它的部署项目并安装了它.安装后我盯着它看.它成功开始了.

第二天,我做了一些修改,重建并重新安装,但现在它没有安装.

然后我想到了安装程序的问题,让我们为服务创建一个自定义安装程序,以便我可以随时更新我的​​代码.

如果有人将来需要这个,我就像这样创建它.

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);

        }
}
Run Code Online (Sandbox Code Playgroud)

我通过检查参数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;
Run Code Online (Sandbox Code Playgroud)

现在我正在尝试安装.我收到错误System.Security.SecurityException:找不到源,但无法搜索部分或全部事件日志.无法访问的日志:安全性.

我谷歌它,并了解服务将尝试访问应用程序日志,同时在那里安装和写日志.

我不是在写任何事件日志.我有我的log4net用于记录.但仍然是它的默认行为.

现在如何克服这个问题?它没有安装,即使我拥有所有权限.

谢谢

Ebe*_*oux 68

我发现有时您可能需要"以管理员身份运行".如果从命令提示符进行安装,则可能需要使用"以管理员身份运行" 来启动.