Geeks With Blogs
Path Notes of a Kodefu Master blog

Yesterday, I wrote about running GacUtil from within the AfterBuild target, due to some constraints with MPF and T4.

I discovered that the path I provided was wrong. It's the only GacUtil.exe under the Microsoft.NET folder, but it won't register the assembly. Since I wasn't sure which path I should be using, I used the GacUtil task from MSBuild Community Tasks. Here is the relevant code it uses to retrieve the path to gacutil.exe:

protected override string GenerateFullPathToTool()
return ToolLocationHelper.GetPathToDotNetFrameworkSdkFile(ToolName, TargetDotNetFrameworkVersion.Version20);
protected override string ToolName
get { return "gacutil.exe"; }
This was fine, but it turns out that you get an error from regpkg.exe if the assembly is location in the GAC if you're trying to generate from a path.
regpkg : warning : The Assembly specified at [Path]\[Assembly]cannot be loaded because an alternate copy with the same identity exists in the Assembly probing path at 'C:\WINDOWS\assembly\GAC_MSIL\[Assembly]\\[Assembly]. The Assembly at 'C:\WINDOWS\assembly\GAC_MSIL\[Assembly]\\[Assembly] will be loaded instead.
regpkg : error : Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
  Could not load file or assembly '[Assembly], Version=, Culture=neutral, PublicKeyToken=7c2bf94927fd6e28' or one of its dependencies. The system cannot find the file specified.
To fix this, I made the GacUtil uninstall the assembly in the BeforeBuild target. However, I kept receiving the same error. Generating a diagnostic log from MSBuild showed me the actual culprit: the RegisterManagedPackage target builds after the AfterBuild target. I fixed it by making AfterBuild depend on RegisterManagedPackage. I've pasted the final, relevant script below. Note that the RegPkg command in the AfterBuild is used to generate installer wix. TargetName is used in the GacUtil uninstall because it requires the name without the extension.
<Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets" />
Target Name="BeforeBuild">
GacUtil Command="Uninstall" Assemblies="$(TargetName)" ContinueOnError="true"/>
Target Name="AfterBuild" DependsOnTargets="RegisterManagedPackage">
.wix is specified instead of .wxi because the RegPkg task requires it to generate the wix script. -->
RegPkg ItemToRegister="$(TargetPath)" ProductVersion="$(TargetVSVersion)" SDKVersion="$(VsSDKVersion)" OutputFile="..\Installer\RegistryEntries.wix" Unregister="false" UseCodeBase="false" UseRanu="false" />
GacUtil Assemblies="$(TargetPath)"/>
Posted on Thursday, April 30, 2009 11:13 AM Kodefu | Back to top

Comments on this post: GacUtil, MPF, and CSProj

# re: GacUtil, MPF, and CSProj
Requesting Gravatar...
The nightly build of MSBuild Community Tasks uses the fusion api directly so you won't have path issues for Gacutil any more. You can try that out.

~ Paul
Left by Paul on May 01, 2009 7:07 AM

Your comment:
 (will show your gravatar)

Copyright © Chris Eargle | Powered by: