簡體   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