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