.net - Web Deploy 能否为目标服务器的GAC 打包本地程序集?

标签 .net gac msdeploy webdeploy

我一直在关注一个非常有帮助的 blog post by Xinyang Qiu用于将 gacAssembly 元素添加到我的 Web 部署站点 list 。不幸的是,the gacAssembly provider似乎无法打包本地程序集(即不在 GAC 中的程序集)以部署到 GAC。在打包时,我收到以下错误:

One or more entries in the manifest 'sitemanifest' are not valid. The library '<full path>\<assembly>' could not be loaded. The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)

我认为它是在解释我作为程序集名称的一部分提供的路径,这当然不是我的意思。

有什么办法解决这个问题吗?是否可以将位于本地目录中的程序集粘贴到 Web 部署包中以部署到服务器的 GAC?

最佳答案

Web Deploy 2.0 有一个新的提供程序可以完全满足您的要求:gacInstall 提供程序(请参阅 http://technet.microsoft.com/en-us/library/gg607836(WS.10).aspx )允许在目标位置对源包中嵌入的程序集进行 GAC。

给定一个像这样的简单 list :

<sitemanifest>
  <gacInstall path="C:\Temp\MyAssembly.dll"  />
</sitemanifest>

..您可以像这样创建一个包含程序集的包:

msdeploy -verb:sync -source:manifest="path to manifest.xml" -dest:package="path to package.zip"

..然后使用该软件包将其安装在远程计算机上的 GAC 中:

msdeploy -verb:sync -source:package="path the package.zip" -dest:auto,computerName=REMOTEMACHINENAME

(当然前提是程序集有一个强名称!)

现在,可以将相同的原理应用于 VS Web 项目。不过,您需要确保 Web Deploy 2.x 安装在开发机器和目标 Web 服务器上。而不是指定 <MsDeploySourceManifest Include="gacAssembly">它必须是 <MsDeploySourceManifest Include="gacInstall"> .

这是我试过的完整的 {projectName}.wpp.targets 文件(基于问题中提到的博文中的文件):

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <PropertyGroup>
    <CreatePackageOnPublish>True</CreatePackageOnPublish>
    <IncludeGacAssemblyForMyProject>True</IncludeGacAssemblyForMyProject>
    <UseMsdeployExe>False</UseMsdeployExe>
  </PropertyGroup>


  <PropertyGroup>
    <!--Targets get execute before this Target-->
    <OnBeforeCollectGacAssemblyForPackage Condition="'$(OnBeforeCollectGacAssemblyForPackage)'==''">
    </OnBeforeCollectGacAssemblyForPackage>
    <!--Targets get execute after this Target-->
    <OnAfterCollectGacAssemblyForPackage Condition="'$(OnAfterCollectGacAssemblyForPackage)'==''">
    </OnAfterCollectGacAssemblyForPackage>

    <CollectGacAssemblyForPackageDependsOn Condition="'$(CollectGacAssemblyForPackageDependsOn)'==''">
      $(OnBeforeCollectGacAssemblyForPackage);
      Build;
    </CollectGacAssemblyForPackageDependsOn>

    <AfterWriteItemsToSourceManifest>
      $(AfterWriteItemsToSourceManifest);
      _PrintSourceManifests;
    </AfterWriteItemsToSourceManifest>
  </PropertyGroup>


  <PropertyGroup>
    <IncludeGacAssemblyForMyProject Condition="'$(IncludeGacAssemblyForMyProject)'==''">False</IncludeGacAssemblyForMyProject>
    <AfterAddContentPathToSourceManifest Condition="'$(AfterAddContentPathToSourceManifest)'==''">
      $(AfterAddContentPathToSourceManifest);
      CollectGacAssemblyForPackage;
    </AfterAddContentPathToSourceManifest>
  </PropertyGroup>


  <Target Name="CollectGacAssemblyForPackage"
          DependsOnTargets="$(CollectGacAssemblyForPackageDependsOn)"
          Condition="$(IncludeGacAssemblyForMyProject)">

    <ItemGroup>
      <MyGacAssembly Include="@(ReferencePath)" Condition="'%(ReferencePath.GacInstall)'=='true'" />
    </ItemGroup>

    <Message Text="MsDeployPath: $(MSDeployPath)" /> <!-- For debugging -->
    <Message Text="Adding [gacInstall]: %(MyGacAssembly.Identity)" />

    <ItemGroup>
      <MsDeploySourceManifest Include="gacInstall" Condition="$(IncludeGacAssemblyForMyProject)">
        <Path>%(MyGacAssembly.Identity)</Path>
      </MsDeploySourceManifest>
    </ItemGroup>

    <CallTarget Targets="$(OnAfterCollectGacAssemblyForPackage)" RunEachTargetSeparately="false" />
  </Target>

  <Target Name="_PrintSourceManifests">
    <!-- Just for debugging -->
    <Message Text="Exporting Manifests:" />
    <Message Text=" @(MsDeploySourceManifest) : %(MsDeploySourceManifest.Path)" />
  </Target>

</Project>

我添加的另一件事是如何选择 gacInstall 程序集,即通过元数据条目 <GacInstall>True</GacInstall>添加到各自的<Reference/> project.csproj 文件中的标记:

<Reference Include="System.Data">
  <GacInstall>True</GacInstall>
</Reference>
<Reference Include="MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f430b784831ac64e, processorArchitecture=MSIL">
  <HintPath>..\..\LIB\MyAssembly.dll</HintPath>
  <GacInstall>True</GacInstall>
</Reference>

无论您引用的是 GAC 程序集还是本地程序集,它都有效。

希望对您有所帮助:)

关于.net - Web Deploy 能否为目标服务器的GAC 打包本地程序集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5783902/

相关文章:

.net - 作为项目经理,了解 .net 最重要的事情是什么?

c# - 如何学习 MVC——*不*在网络环境中

c# - 如何使用 GAC 和 NGEN 部署 C# 应用程序

.net - 为什么不将 .NET Framework 4.0 DLL 添加到 GAC?

c# - 如何使用 Python for .NET 正确嵌入

c# - 使用异步 CTP 同时下载 HTML 页面

c# - 为什么我无法访问 C :\WINDOWS\assembly\GAC Folder?

powershell - 从 MSdeploy runco​​mmand 运行 PowerShell 不会退出

msbuild - MSBuild 远程 Web 部署中的 App_Offline

.net - 使用MSDeploy将可执行文件复制到服务器,然后运行