You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决服务器加载System.Reflection.TypeExtensions程序集失败问题

问题:Wix Installer部署服务器时加载System.Reflection.TypeExtensions程序集失败

本地通过Wix Installer调用依赖Microsoft.Web.Administration的代码创建IIS虚拟目录正常,但部署到服务器时触发错误:

无法加载文件或程序集'System.Reflection.TypeExtensions, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'或其依赖项,找到的程序集清单定义与程序集引用不匹配。

解决方案

1. 添加程序集绑定重定向

在自定义动作项目的App.config中加入绑定重定向规则,让运行时自动适配服务器上存在的程序集版本:

<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Reflection.TypeExtensions" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.1.0.0" newVersion="4.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

注:若服务器安装的是更高版本,可将newVersion改为对应版本号(如4.2.0.0)

2. 确保程序集被正确打包

  • 在自定义动作项目的引用中找到System.Reflection.TypeExtensions,设置Private=True,保证该程序集被复制到输出目录
  • 检查Wix安装包的<Binary>元素,确认已将包含该程序集的自定义动作DLL纳入打包范围

3. 更新Microsoft.Web.Administration包版本

当前使用的7.0.0.0版本依赖较旧的.NET标准库,建议更新到兼容.NET Framework 4.7的最新稳定版本(如11.0.0):

<!-- packages.config中修改对应条目 -->
<package id="Microsoft.Web.Administration" version="11.0.0" targetFramework="net47" />

同时同步更新.csproj文件中的引用路径

4. 验证服务器.NET Framework版本

确保服务器已安装.NET Framework 4.7或更高版本,高版本框架中System.Reflection.TypeExtensions已作为系统程序集存在,可能无需额外打包

相关代码与配置

自定义动作代码

[CustomAction]
public static ActionResult SetVirtualDirectory(Session session)
{
#if DEBUG
    System.Diagnostics.Debugger.Launch();
#endif

    try
    {
        using (ServerManager srv = new ServerManager())
        {
            string appServerPath = Path.Combine(appServerRegistryKey.GetValue("VisionServer_InstallDir").ToString(), "SDMSTools");

            var site = srv.Sites.FirstOrDefault(c => c.Name == "SdmsVision");
            site.Applications[0].VirtualDirectories.Add("/SDMSTools", appServerPath);

            srv.CommitChanges();

            Configuration vDirConfig = srv.GetWebConfiguration("SDMSVision", "SDMSTools");
            ConfigurationSection staticContentSection = vDirConfig.GetSection("system.webServer/staticContent");
            ConfigurationElementCollection staticContentCollection = staticContentSection.GetCollection();

            // 检查.ini扩展名是否已配置
            var iniTypeExists = staticContentCollection.Any(m => m.Attributes["fileExtension"].Value.Equals(".ini"));

            if (!iniTypeExists)
            {
                ConfigurationElement mimeMapElement = staticContentCollection.CreateElement("mimeMap");
                mimeMapElement["fileExtension"] = @".ini";
                mimeMapElement["mimeType"] = @"application/octet-stream";
                staticContentCollection.Add(mimeMapElement);

                srv.CommitChanges();
            }
        }
    }
    catch(Exception e)
    {
        session.Log("The virtual folder SDMSTools has not been created. The following error occurred: " + e.Message);
    }

    return ActionResult.Success;
}

.csproj配置片段

<ItemGroup>
    <Reference Include="Microsoft.Deployment.WindowsInstaller, Version=3.0.0.0, Culture=neutral, PublicKeyToken=ce35f76fcda82bad, processorArchitecture=MSIL">
      <SpecificVersion>False</SpecificVersion>
      <Private>True</Private>
      <HintPath>..\..\Packages\firegiant-wix.3.11.20461\tools\sdk\Microsoft.Deployment.WindowsInstaller.dll</HintPath>
    </Reference>
    <Reference Include="Microsoft.Web.Administration, Version=7.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
      <HintPath>..\..\Packages\Microsoft.Web.Administration.7.0.0.0\lib\net20\Microsoft.Web.Administration.dll</HintPath>
    </Reference>
    <Reference Include="System.Xml.Linq" />
  </ItemGroup>

packages.config配置

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="firegiant-wix" version="3.11.20461" targetFramework="net47" allowedVersions="[3.11.20461]" />
  <package id="Microsoft.Web.Administration" version="7.0.0.0" targetFramework="net47" />
  <package id="Waters.Wix.Build.Support" version="3.0.3" targetFramework="net47" allowedVersions="[3.0.3]" />
</packages>

内容的提问来源于stack exchange,提问作者Vladut

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 04:52:49