繁体   English   中英

在.NET上安装Windows Service时探查负载异常

[英]Probing Load Exception While Installing Windows Service On .NET

我有一个用.NET编写的Windows服务,并且我使用了探测功能,以便将dll加载到该Windows服务中。 但是,当我打开命令提示符并尝试使用installutil.exe安装Windows服务时,出现诸如以下错误:“ System.Reflection.ReflectionTypeLoadException:无法加载一个或多个请求的类型。获取LoaderExceptions属性以获取更多信息。正在中止安装,“,

另一方面,当我将dll移到Windows服务所在的同一文件夹中并重复安装过程时,Windows服务已成功安装。

您对此问题有任何想法或建议吗?.NET的Windows服务安装中是否存在探测问题?

如果没有确切的信息,我建议以下几点:

  • 检查异常详细信息以查看到底出了什么问题
  • 使用Fusion Log Viewer来查看绑定失败的程序集
  • 检查您的探测配置是否与您的部署匹配

探测按此处所述进行配置。

我在我的项目中遇到相同的问题,在Windows服务项目中,我有以下app.config部分:

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
         <probing privatePath="SDK" />
    </assemblyBinding>
</runtime>

如果我通过控制台执行该服务,一切都很好,但是当我尝试安装它时,installutil失败,并且出现了相同的异常,因此我的解决方案是通过命令行将服务本身安装起来:

cmd: hotspotcenter -i <service_name="service name">
// service_name i made it optional

安装程序类帮助器:

internal static class BasicServiceInstaller
{
    public static void Install(string serviceName)
    {
        CreateInstaller(serviceName).Install(new Hashtable());
    }

    public static void Uninstall(string serviceName)
    {
        CreateInstaller(serviceName).Uninstall(null);
    }

    private static Installer CreateInstaller(string serviceName)
    {
        var installer = new TransactedInstaller();
        installer.Installers.Add(new ServiceInstaller
        {
            ServiceName = serviceName,
            DisplayName = serviceName,
            StartType = ServiceStartMode.Manual
        });
        installer.Installers.Add(new ServiceProcessInstaller
        {
            Account = ServiceAccount.LocalSystem
        });
        var installContext = new InstallContext(
            serviceName + ".install.log", null);
        installContext.Parameters["assemblypath"] =
            Assembly.GetEntryAssembly().Location;
        installer.Context = installContext;
        return installer;
    }
}

在服务项目的主条目中:

 if (Environment.UserInteractive)
 {
            bool install = false;
            bool uninstall = false;
            string serviceName = "YourDefaultServiceName";

            var p = new OptionSet()
              .Add<bool>("i|install", "Install Windows Service", i => install = i)
              .Add<bool>("i|install=", "Install Windows Service", i => install = i)
              .Add<bool>("u|uninstall", "Uninstall Window Service", u => uninstall = u)
              .Add<string>("sn|service_name=", "Service Name", n => serviceName = n);

            p.Parse(args);

            if (install)
            {
                BasicServiceInstaller.Install(serviceName);
                return;
            }
            else if (uninstall)
            {
                BasicServiceInstaller.Uninstall(serviceName);
                return;
            }

            // if no install or uninstall commands so start the service as a console.
            var host = new YourService();
            host.Start(args);
            Console.ReadKey();
        }
        else
        {
            ServiceBase.Run(new HotspotCenterService());
   }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM