Telerik blogs

Sometimes we need to  build Telerik OpenAccess ORM enhanced projects on a machine that has no installation of the OpenAccess product. There are cases that even require that there is no such installation done on the machines like build servers or other machines that handle the build process. On the other hand when Telerik OpenAccess ORM is installed, the project enhancement is automatically done from our Visual Studio integration add-in. The MSBuild integration can be used as an alternative to the Microsoft Visual Studio integration and is useful while using the command-line build environment. It is targeted for serving build servers and build processes that are done using MSBuild and/or TFSBuild. In this blog I will introduce two possible ways of automating the enhancement process with MSBuild.

 

Using the tasks that come with OpenAccess.targets file

One way to do it is to include the OpenAccess.targets file (that comes with Telerik OpenAccess ORM installation) in your C# or VB project and use the targets defined there to enhance it. The project is automatically installed under the $(MSBuildExtensionsPath) directory. There are few requirements that must be met when using the OpenAccess.targets file:

a) Import the .targets file in your project file, AFTER the import of the original Microsoft build targets which look like:

.vbproj

<Import Project="$(MSBuildBinPath)\Microsoft.VisualBasic.targets" />
.csproj

<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />

 

b) Make sure that there are OpenAccess ORM settings  defined in the file, which look like:

 <ProjectExtensions>
   <VisualStudio>
     <UserProperties OpenAccess_EnhancementOutputLevel="1"   OpenAccess_UpdateDatabase="False" OpenAccess_Enhancing="True" 
          OpenAccess_ConnectionId="DBConnFirstSteps" OpenAccess_ConfigFile="App.config" />
   </VisualStudio>
 </ProjectExtensions>

- you can set the settings manually (they are pretty descriptive and explained here.)
or
- you can use the Telerik OpenAccess ORM VisualStudio integration (the wizards) to set the properties visually.

 

In the end you should have in your project file, something similar to the following:

<!--   ... Content of the .csproj file omitted here ... -->

<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- THE FOLLOWING LINE IMPORTS THE TASKS AND DEPENDENCIES FROM OPENACCESS. -->
<Import Project="$(MSBuildExtensionsPath)\OpenAccess.targets" />
<ProjectExtensions>
   <VisualStudio>
      <UserProperties
               OpenAccess_UseMSBuild="True"
               OpenAccess_EnhancementOutputLevel="1"
               OpenAccess_UpdateDatabase="True""
               OpenAccess_Enhancing="True"
               OpenAccess_ConnectionId="DatabaseConnection1"
               OpenAccess_ConfigFile="App.config" />
   </VisualStudio>
</ProjectExtensions>
</Project>

After having all set in place we can use the task like this:

<OpenAccessEnhancer  
           VerboseMode="2"  
           Assembly="$(TargetPath)"  >
           <Output TaskParameter="Version" PropertyName="OpenAccessEnhancerVersion" />  
</OpenAccessEnhancer>   

where:  $(TargetPath) stores the path to the assembly that will be enhanced.

In order to enhance an assembly that was signed and has a strong name, the following options must be added:

<OpenAccessEnhancer  
           VerboseMode="2"  
           Assembly="$(TargetPath)"  
           SignAssembly="True"  
           KeyFile="$(ProjectDir)$(AssemblyOriginatorKeyFile)"  >  
           <Output TaskParameter="Version" PropertyName="OpenAccessEnhancerVersion" />  
</OpenAccessEnhancer>   

where SignAssembly is set to True and the KeyFile property contains the path to file containing the key.

You can find more information on all available MSBuild tasks for Telerik OpenAccess ORM here.

 

Using custom tasks file

A slightly different and lightweight approach is to use directly Exec tasks that will execute the enhancer. We prepared a small .targets file (the enhance.targets)that can be used out of the box and included in your build projects:

enhance.targets

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
 <PropertyGroup>
   <OpenAccessInstallDir>C:\Program Files\Telerik\OpenAccess ORM</OpenAccessInstallDir>
 </PropertyGroup>
 
 <Target Name="EnhanceAssembly" Condition="'$(_AssemblyTimestampBeforeCompile)'!='$(_AssemblyTimestampAfterCompile)'">
   <Copy SourceFiles="$(TargetPath)" DestinationFiles ="$(TargetPath).notenhanced"/>
   <Copy SourceFiles="$(PdbFile)" DestinationFiles ="$(PdbFile).notenhanced"/>
   <Exec IgnoreExitCode="false"
         WorkingDirectory="$(TargetDir)"
         Command=""$(OpenAccessInstallDir)sdk\venhance.exe" -verboseMode:2 -signAssembly "-keyFile:$(ProjectDir)$(AssemblyOriginatorKeyFile)" "-assembly:$(TargetPath)""
         Condition="'$(AssemblyOriginatorKeyFile)'!=''" />
   <Exec IgnoreExitCode="false"
         WorkingDirectory="$(TargetDir)"
         Command=""$(OpenAccessInstallDir)sdk\venhance.exe" -verboseMode:2 "-assembly:$(TargetPath)""
         Condition="'$(AssemblyOriginatorKeyFile)'==''" />
   <Copy SourceFiles="$(TargetPath)" DestinationFolder ="$(IntermediateOutputPath)"/>
   <Copy SourceFiles="$(PdbFile)" DestinationFolder ="$(IntermediateOutputPath)"/>
 </Target>
 <Target Name="PeVerify" Condition="'$(_AssemblyTimestampBeforeCompile)'!='$(_AssemblyTimestampAfterCompile)'">
   <GetFrameworkSdkPath>
     <Output TaskParameter="Path" PropertyName="SdkPath" />
   </GetFrameworkSdkPath>
   <Exec WorkingDirectory="$(SdkPath)bin" Command="peverify.exe /nologo "$(TargetPath)"" />
 </Target>
 <PropertyGroup>
   <PdbFile>$(OutputPath)\$(AssemblyName).pdb</PdbFile>
   <RunPostBuildEvent>OnOutputUpdated</RunPostBuildEvent>
   <PrepareForRunDependsOn>
     $(PrepareForRunDependsOn);
     EnhanceAssembly;
     PeVerify
   </PrepareForRunDependsOn>
 </PropertyGroup>
</Project>

Just import the line at the bottom of your project fie – to do so unload the file from the solution,

unloadProject

open it for edit and include the import statement like:

<Import Project="[PATH_TO_TARGETS_FILE]\enhance.targets" />

where PATH_TO_TARGETS_FILE should be substituted with the real path to the .targets file.

Comments

Comments are disabled in preview mode.