How to package .NET assemblies

If we want to package and deploy .NET assemblies for a web application only, we can simply reference them and change their “Copy Local” property to “True”. 

For example, we can do the following to package a MVC project from VS2010.

  1. Create a MVC project
  2. In its references, change the following three assemblies “Copy Local” property value to True.
    • System.Web.Mvc
    • System.Web.Routing
    • System.Web.Abstractions

    You can multi-select them and set the value together.

  3. Right click the project node and select “Build Deployment Package”
  4. Examine the package, you will see the above assemblies’ corresponding dll files are included in the package’s bin directory.

 

Deploy the above package to a 4.0 site, no matter if the corresponding MVC runtime is installed on the server, your application will simply work fine.

image

If we want to package and deploy GACed dll, we need to use gacAssembly web deploy provider and extend our project similarly to the one stated in the how to package registry blog.  The extended projectName.wpp.targets file looks like following:

 

<!--********************************************************************-->
<!-- Task CollectGacAssemblyForPackage –>
<!-- GacAssembly reference: http://technet.microsoft.com/en-us/library/dd569056(WS.10).aspx -->
<!--********************************************************************-->
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <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>
  </PropertyGroup>

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

  <ItemGroup>
    <MyGacAssembly Include = "$(MyGacAssemblies)"/>
  </ItemGroup>

  <Target Name="CollectGacAssemblyForPackage"
          DependsOnTargets="$(CollectGacAssemblyForPackageDependsOn)"
          Condition="$(IncludeGacAssemblyForMyProject) AND '$(MyGacAssemblies)'!=''">

    <Message Text="Adding %(MyGacAssembly.Identity)" />
    <ItemGroup>
      <MsDeploySourceManifest Include="gacAssembly"
                                 Condition="$(IncludeGacAssemblyForMyProject)">
        <Path>%(MyGacAssembly.Identity)</Path>
      </MsDeploySourceManifest>
    </ItemGroup>
    <CallTarget Targets="$(OnAfterCollectGacAssemblyForPackage)" RunEachTargetSeparately="false" />
  </Target>
</Project>

Then you can use the following command line to package the project with corresponding Mvc GACs and 4.0 version of System.Web.Routing and System.Web.Abstractions.

msbuild WebApplication2.csproj /target:package /p:IncludeGacAssemblyForMyProject=True;MyGacAssemblies="System.Web.Mvc;System.Web.Routing,Version=4.0.0.0;System.Web.Abstractions,Version=4.0.0.0"

Note, gacAssembly web deploy provider has some limitation as described in the link.

 

Xinyang Qiu | Visual Web Developer