【问题标题】:Best way to detect if WIF runtime is installed检测是否安装了 WIF 运行时的最佳方法
【发布时间】:2014-08-22 13:19:51
【问题描述】:

我的 ASP.Net 应用程序的一些页面使用 WIF 直接连接到另一个服务。 WIF 只是在这里取得进展,虽然它安装在测试和生产服务器上,但每次新程序员或测试人员获得最新版本并且碰巧在没有在其机器上安装 WIF 运行时的情况下访问这些页面时,他都会获得 YSOD 和一个关于找不到 Microsoft.IdentityModel 的错误......他们从未阅读过,而是启动了一个 IM,告诉我我的应用程序已损坏。

我想检测是否安装了 WIF 运行时,如果没有安装,则显示每条有用的错误消息并链接到下载页面。我不想检查特定的 .dll 路径,因为这可能会改变……而且 3.5 和 4.0 已经有不同的路径。

是否有检测是否安装了 WIF 运行时的最佳方法?

(显然在一个没有引用它的页面中......如果没有安装它就无法正确显示)

编辑

它看起来像 WIF is included in the framework with 4.5,所以 3.5/4.0 特定的方法就可以了。无需面向未来。

【问题讨论】:

    标签: c# asp.net wif


    【解决方案1】:

    微软官方评论:

    Q: Under what registry key is WIF installed?
    A: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\WindowsIdentityFoundation\setup\v3.5\.
    

    参考here

    【讨论】:

      【解决方案2】:

      我会尝试在 try/catch -block 中从 GAC 加载 Microsoft.IdentityModel.dll 或者更好,只捕获当前没有 WIF 运行时的特定异常用户,然后使用它来将用户重定向到具体的错误页面/消息。

      要回答您的确切问题 - 有没有办法检测是否存在正确的 WIF 安装包:如果您的应用程序可以访问注册表(至少在只读模式下),还可以检查 Windows 身份Foundation 相关项目出现在 SOFTWARE\Microsoft\Windows Identity Foundation(以及 64 位系统的相关 Wow6432Node)中。

      【讨论】:

      • 因为只有少数页面需要 WIF,所以我想在应用程序启动后立即检查,并在他们访问那些应用程序缺少所需组件的页面之前让他们知道。我们不做任何花哨的部署打包,所以没有安装程序来完成这项工作。
      • 此注册表检查是其他 Microsoft 安装程序所做的一切(我剖析了其中的几个)。有关我最终得到的代码,请参见下面的答案。
      【解决方案3】:
      1. 您可以将 WIF 库与您的应用捆绑在一起。
      2. 您可以使用 WebPI 并在那里参考 WIF SDK。
      3. 如果您尝试从 GAC 加载程序集,也许可以解决问题。 Reflection Only Assembly Loading

      【讨论】:

        【解决方案4】:

        在检查了 Microsoft 的一些安装程序以查看他们如何检测到 WIF 运行时的存在之后,我采用了上述答案中的注册表检查建议,这就是他们所做的一切。

        这是我的选择:

        /// <summary>
        /// Determines if WIF is installed on the machine.
        /// </summary>
        public static class WifDetector
        {
            /// <summary>
            /// Gets a value indicating that WIF appears to be installed.
            /// </summary>
            public static bool WifInstalled { get; private set; }
        
            static WifDetector()
            {
                WifInstalled = IsWifInstalled();
            }
        
            private static bool IsWifInstalled()
            {
                try
                {
                    //return File.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles),
                    //                                "Reference Assemblies\\Microsoft\\Windows Identity Foundation\\v3.5\\Microsoft.IdentityModel.dll"));
                    //The registry approach seems simpler.
                    using( var registryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Wow6432Node\\Microsoft\\Windows Identity Foundation") ?? 
                                             Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows Identity Foundation") )
                    {
                        return registryKey != null;
                    }
                }
                catch
                {
                    //if we don't have permissions or something, this probably isn't a developer machine, hopefully the server admins will figure out the pre-reqs.
                    return true;
                }
            }
        }
        

        然后在基本页面或母版页面中检查该值,并让用户知道。实际检查只在类型初始化器中执行一次,之后就只是简单的静态属性访问。

            private void CheckWifInstallation()
            {
                if (!WifDetector.WifInstalled)
                {
                    var alert = new ClientSideAlert(
                            "This application requires the Windows Identity Foundation runtime to be installed on the webserver:\n");
                    alert.AddMessageLine("Please install the appropriate WIF runtime for this operating system by visiting:\n");
                    alert.AddMessageLine("http://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=17331 \n");
                    alert.AddMessageLine("or simply search for 'WIF runtime install'\n");
                    alert.AddMessageLine("Thanks, and have a nice day!'");
                    alert.Display(Page);
                }
            }
        

        我们没有用于开发人员机器的精美 Web 部署包,它只是从源代码获取并运行。这将使没有此库的开发人员在遇到 YSOD 和晦涩的程序集加载错误时不会浪费时间。

        感谢您的建议。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-09-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多