【问题标题】:.Net windows services: install from referenced assembly.Net Windows 服务:从引用的程序集安装
【发布时间】:2010-11-30 13:29:10
【问题描述】:

一行安装windows服务的例子很多:

    ManagedInstallClass.InstallHelper(
      new[] { Assembly.GetExecutingAssembly().Location });

在 exe 模块中声明服务类之前,它工作正常。 但是,如果服务类在引用的程序集中(未在可执行文件中声明,但在链接的 dll 中),则相同的代码对我不起作用。

在这种情况下,服务也已注册但无法启动,因为它已注册到 dll 路径并指向 dll(当我尝试启动时,事件日志中会出现“服务不是 win32 可执行文件”消息)

如果我将GetExecutingAssembly().Location 更改为可执行路径,则找不到安装程序并且根本没有注册服务。

是否可以将服务类放入引用的程序集中,并且仍然能够以最小的努力注册服务?

提前感谢您!

【问题讨论】:

    标签: .net windows-services installation


    【解决方案1】:

    这里有一些 C# 代码,允许您“手动”安装/卸载服务(无需声明自定义 RunInstaller 属性):

    static void InstallService(string path, string name, string displayName, string description)
    {
        ServiceInstaller si = new ServiceInstaller();
        ServiceProcessInstaller spi = new ServiceProcessInstaller();
        si.Parent = spi;
        si.DisplayName = displayName;
        si.Description = description;
        si.ServiceName = name;
        si.StartType = ServiceStartMode.Manual;
    
        // update this if you want a different log
        si.Context = new InstallContext("install.log", null);
        si.Context.Parameters["assemblypath"] = path;
    
        IDictionary stateSaver = new Hashtable();
        si.Install(stateSaver);
    }
    
    static void UninstallService(string name)
    {
        ServiceInstaller si = new ServiceInstaller();
        ServiceProcessInstaller spi = new ServiceProcessInstaller();
        si.Parent = spi;
        si.ServiceName = name;
    
        // update this if you want a different log
        si.Context = new InstallContext("uninstall.log", null);
        si.Uninstall(null);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-21
      • 1970-01-01
      • 2012-09-15
      • 1970-01-01
      • 2011-05-21
      • 1970-01-01
      相关资源
      最近更新 更多