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

时间:2012-05-03 06:19:06

标签: c# windows-services installation

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

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

然后我认为安装程序存在问题,我们可以为服务创建自定义安装程序,以便我可以随时更新代码。

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

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

        }
}

我通过检查参数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;

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

我谷歌它,并且知道服务将尝试在安装和写日志时访问应用程序日志。

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

现在如何解决这个问题?即使我拥有所有权限,它也没有安装。

由于

3 个答案:

答案 0 :(得分:65)

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

答案 1 :(得分:1)

我可能有一个单独的根本原因,但是我通过将我的服务更改为LocalSystem(这是我想要的)而不是LocalService来修复它。

答案 2 :(得分:0)

I can confirm that under "windows 7 64 bit" AND "Windows 10" you must:

1) run Visual studio command prompt AS ADMINISTRATOR (right click.. Other.. tun as admin)

enter image description here

2) go to "obj" folder where You have the exe. (cd [all path to \obj] ) 3) launch installutil [myservice.exe]

if not run as "admin", it fails even on old win7. :(

note: MSDN does explain it:

(at: https://docs.microsoft.com/en-us/dotnet/framework/windows-services/walkthrough-creating-a-windows-service-application-in-the-component-designer)

"To install a Windows service, you must have administrative credentials on the computer on which you're installing it."

:)