5

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

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

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

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

编辑

看起来WIF 包含在 4.5 的框架中,因此特定于 3.5/4.0 的方法就可以了。无需面向未来。

4

4 回答 4

3

微软官方评论:

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

参考这里

于 2014-08-22T04:40:11.377 回答
2

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

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

于 2012-08-10T19:07:29.967 回答
1
  1. 您可以将 WIF 库与您的应用程序捆绑在一起。
  2. 您可以在那里使用 WebPI 并参考 WIF SDK。
  3. 也许如果您尝试从 GAC 加载程序集就可以了。仅反射装配加载
于 2012-08-07T21:40:56.277 回答
1

在检查了 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 和晦涩的程序集加载错误时不会浪费时间。

感谢您的建议。

于 2012-08-16T15:48:58.927 回答