Showing posts with label msbuild. Show all posts
Showing posts with label msbuild. Show all posts

Monday, June 30, 2014

How to run Entity Framework Migrations using MSBuild and Migrate.exe

Recently, one of out client moved to Entity Framework.  Along with that, they also decided to use the Entity Framework Migrations.

On a DEV machine, Entity Framework Migrations can be executed very easily using the the Update-Database command in Package Manager Console.

Naturally, we do have a Continuous Integration (CI) server that builds and integrates the checked-in code.  We needed to run the migrations on the CI server as well.  For building our project we were using MSBuild.  I wanted to integrate Entity Framework Migrations with MSBuild so that the migrations can be executed outside the Visual Studio and on the CI server as well.

Looked like a no brainier task to me, I hoped that there will be an MSBuild task that would run the Entity Framework Migrations and my job would be done!  But to my surprise, Entity Framework Migrations is not fully thought through, it does not come with any task for MSBuild which can be executed out of the box.

Turns out that there is a Migrate.exe that could be used to execute the migrations.  This could then be invoked from MSBuild to get the job done.  So how do they do it, how to run Entity Framework Migrations using MSBuild and Migrate.exe

How do they do it!
  • Migrate.exe can be found under the tools directory of the Entity Framework package that comes along with NuGet.
  • We could create a simple task that can invoke migrate.exe using the Exec MSBuild task. 
  • However, simply running the above task will not get your the desired results.  When you run this task you might end up getting some weird exception.  I got the following exception
  • Turns out that to successfully run Migrate.exe, it needs to be copied to the directory where the DLL holding all the Entity Framework Migrations is located.
  • I don't understand why such a restriction is necessary.  Why could they not use the StartUpDirectory parameter or some other parameter to accept the full directory path of the DLL that has the migrations.  May be when someone wrote the code for Migrate.exe, he/she found it convenient to run it this way!
  • Anyways, so the fix to this problem was simple, first copy the Migrate.exe to the folder where the DLL is located and then run the Migrate.exe
  • Updated target code looks as follows.
As you can see, its not very straightforward to get the Entity Framework Migrations integrated with MSBuild, but with some hacks, we can get it working! 

Thursday, January 24, 2013

MSBuild doesn't copy DLL references from dependent projects

Recently in one of my projects we came across an interesting problem.  To explain the I will try and simplify it a bit. 

The Problem:

The problem was that there were two projects in my solution.  Lets say Project A and Project B.
  • Project A refers Project B 
  • Project B refers a third party DLL in my case it was the NHibernate.ByteCode.Castle.dll 
  • This DLL was referenced by Project B and had the Copy Local attribute set to True
  • However, when we build the projects using MSBuild, the Reference DLL NHibernate.ByteCode.Castle.dll was not copied to the output folder of Project A
There was no explanation for this behavior.  I had checked the project file as well, the NHibernate.ByteCode.Castle.dll was included in the Project B correctly with Private node set to true.  Then why was it that the reference DLL was not copied in the output folder for Project A?

The Analysis:
 

After some initial investigation, I noticed that there was no code in Project B that was directly making use of NHibernate.ByteCode.Castle.dll.  This DLL is loaded internally by NHibernate to enable Lazy Loading at the property level.

Since there is no Code that directly makes uses of the NHibernate.ByteCode.Castle.dll, MSBuild does not think its necessary to copy the DLL to the output folder of Project A.

The Solution:

The solution is a cheap trick, we need to make MSBuild think that we are actually using some classes from the NHibernate.ByteCode.Castle.dll.

What I did was, add a class into Project B called DynamicLoadedClasses.  This class will have a private constructor which simply instantiate any class from the NHibernate.ByteCode.Castle DLL.  Here's how my DynamicLoadedClass looks like
As we can see the DynamicLoadedClasses simply has a private constructor that instantiates the NHibernate.ByteCode.Castle.ProxyFactory class from the NHibernate.ByteCode.Castle DLL. This fools MSBuild that we are acutally using a class from the referenced DLL.

Now when we build the solution using MSBuild, we find that in the output directory of Project A, the DLL NHibernate.ByteCode.Castle.DLL actually gets copied.

You can find more details about this problem here.

Saturday, May 28, 2011

How to build the web project on the CI server without Visual Studio 2010

On one of the ASP.Net project, we were using Visual Studio 2010 IDE and MSBuild, to compile and build the application.  Pretty standard setup for a web project right?.  On developer machine all worked very smoothly, since each developer had Visual Studio 2010 installed locally on their machines.

We setup a continuous integration (CI) server Jenkins (formally Hudson) for this project.  Naturally, we didn't wanted to install Visual Studio 2010 on the CI server.  But we still wanted to build our app on the CI server.  Hence, we installed Microsoft Windows SDK 7.1 and all its components on the CI server.  After installing the SDK, we were able to build all projects except the web project on the CI server.

The error that we were getting was

Obviously, since the Visual Studio 2010 was not installed on the CI server, the path mentioned in the error didn't exist and that's why the web project was not able to build.

I opened the csproj file in Notepad++, to find the reference of "Microsoft.WebApplication.targets".  I found a reference of "Microsoft.WebApplication.targets" in the csproj which looked liked 

Indeed the web csproj file had an import statement, which added the reference to "Microsoft.WebApplication.targets". But I was not sure why this reference was needed in the first place.

My first attempt at fixing the CI server:

I removed the reference of "Microsoft.WebApplication.targets" from the web csproj file and tried building the application using MSBuild.  To my surprise, the app build fine, it ran all the tests without any issue.  My confidence grew a little, I thought I had the solution.  I thought the reference was added to the csproj by Visual Studio 2010, but was never used.

I committed the fix, build went green on the CI Server as well!

After a few hours, some developers complained that, when they now open up the web project, Visual Studio 2010 prompted for converting the project into a Visual Studio 2010.  Certainly, this behaviour was triggered after I removed the import statement from the web csproj file.

Its pretty clear now, why the import was required.  Visual Studio 2010 identifies that, the project is a Visual Studio 2010 Web project using the import of "Microsoft.WebApplication.targets".  Hence, although the build run's fine using MSbuild on developer machine as well as on CI server this is not the right fix!

My second attempt at fixing the CI server:

Googling a little I found that, there are only two options to fix this particular issue.
  • Install Visual Studio 2010 on the CI server
  • Copy the "Microsoft.WebApplication.targets" file manually from any developer machine to the CI server
It was sad to see that there was no standalone installer that installed the "Microsoft.WebApplication.targets" at the desired location.  We had to either install the full Visual Studio 2010 or copy the file manually.


I decided to chose the lesser evil of the two.  We copied the file "Microsoft.WebApplication.targets" from my machine (located at path C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\WebApplications on a Windows 7 64 Bit installation) onto the CI server (at the path C:\Program Files\MSBuild\Microsoft\VisualStudio\v10.0\WebApplications.  CI server has Windows 2003 OS installed on it.) and then ran the build.  Yep! It worked!  The build went green on the CI server.

I know, its just an hack, but for now I had no other option!

Saturday, April 30, 2011

How to fix the “circular file references are not allowed” Error in ASP.Net


On one of my .Net projects, I came across an interesting issue.  I wasted couple of hours fixing it.  Hence, I decided to share my findings, so that others do not have to waste their time fixing the same issue. 

The issue

The issue was pretty simple, the app would not build.  The error that I was getting while building a ASP.Net web project using MSBuild was:

Of course the error said there is some sort of circular reference in my code.  I looked around to check and recheck, if I have created a circular reference by mistake.  However, if there was any circular reference, the code would not compile.  Code was compiling fine but it was failing when we ran aspnet_compiler.exe.


The ASP.Net Compilation tool (aspnet_compiler.exe) enables you to compile an ASP.Net Web application, this helps application performance because end users do not encounter a delay on the first request to the application.

I checked again, but certainly there was no code related circular dependency then why the aspnet_compiler.exe was complaining about the circular file references?

The Explanation

Googling a little, I found that, by default, in a Website Project, ASP.Net creates one DLL per folder. Hence, if you have the following setup:
  • User control ucA.ascx is present in the directory "A".  ucA.ascx refers another user control ucB.ascx
  • User control ucB.ascx is present in the directory "B".  ucB.ascx refers another user control ucC.ascx
  • User control ucC.ascx is present in the directory "A".
The folder A's DLL will reference the folder B's DLL, which will again reference the folder A's DLL, causing a "circular file reference".

This is the reason why the aspnet_compiler.exe fails with the "circular file reference" error.

The Fix

There are two ways this issue could be fixed
  • Rearrange the user controls (or MasterPages) to remove the circular references.  Usually this means moving the user controls in separate directories.  In our example, moving ucC.ascx to a new directory "C" (Preferred Solution).
  • Use batch=”false” in the compilation tag of the web.config file.  This will cause a new DLL to be created for each control/page in the site. This should fix the error but is really lousy for performance, so it should be avoided.
I moved the ucC.ascx in a different directory and yes the error went away!
Have some Fun!