May, 2010

20
May 10

ASP.NET MVC2 Plugin Architecture Tutorial Part 2

This is part 2 of a multi-part tutorial:

After much anticipation and speculation (read mild interest from Google searches) I am ready to get on with the second part of the ASP.NET MVC2 Plugin Architecture Tutorial. In this post we will take the solution developed in part 1 of this tutorial and extend it so that we can make the plugins less aware of their plugin “status”. One of our primary goals will be to remove the need to use fully qualified views such as:

View("~/Plugins/FzySqrPlugin.dll/FzySqrPlugin.Views.HelloWorld.Index.aspx");

We will do this by ever so slightly extending the MVC framework to bend it to our will. Let’s get started shall we?

Continue reading →

05
May 10

Copying a file with msbuild in Visual Studio 2010

This morning I finally became fed up with manually copying the compiled assembly for one of my new plugins. Each time I make a change to the plugin, I need to move it to the plugin host’s project folder. Not a big deal, but unacceptable according to question #2 of the Joel Test

This is a quick fix using msbuild. First right click on the project in Visual Studio and select “Unload Project”. Once unloaded, right click it again and select “Edit Project.csproj”.

Scroll down to the bottom of the project file. You should see something like:

  <!-- To modify your build process, add your task inside one of the targets below and 
  uncomment it. Other similar extension points exist, see Microsoft.Common.targets.
  <Target Name="BeforeBuild">
  </Target>
  <Target Name="AfterBuild">
  </Target>
  -->

Uncomment the AfterBuild target and modify it to look like the following:

<Target Name="AfterBuild" >
    <Copy SourceFiles="bin\FzySqrApp.dll" DestinationFolder="C:\some\special\path\"></Copy>
</Target>

Or, if you only want to have the copy executed for a specific build configuration (in this case BuildAndCopy) you can add a condition:

<Target Name="AfterBuild" Condition="'$(Configuration)' == 'BuildAndCopy'">
    <Copy SourceFiles="bin\FzySqrApp.dll" DestinationFolder="C:\some\special\path\"></Copy>
 </Target>

But what if we are already using the AfterBuild target for something else? Well, it is quite easy to add a new target. Below, I have added the CopyFile target to my project file and set it to run for my BuildAndCopy configuration.

<Target Name="CopyFile" Condition="'$(Configuration)' == 'BuildAndCopy'">
    <Copy SourceFiles="bin\FzySqrApp.dll" DestinationFolder="C:\some\special\path\"></Copy>
</Target>

By default, msbuild will not execute the new target. We can fix this by adding CopyFile to the DefaultTargets property in the tag at the top of the file:

<Project ToolsVersion="4.0" DefaultTargets="Build;CopyFile" 
    xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

After you have made your changes, right click on the Project name and select “Reload Project” and build away!*

*If you need to debug your msbuild configuration, you might find it helpful to increase MSBuild’s verbosity. Go to Tools->Options, Projects And Solutions->Build and Run to set this.