Config File Transformations For All

Config file transformations are a very useful feature supported by VS 2010 web application projects that allow you to maintain config settings for multiple environments, such as dev, staging, production. Unfortunately this feature is only available in web application projects, it is not supported on any other project type out of the box.

This came to my attention as I have been working on a project where we split an existing web application project into 5 separate projects. We moved the EF DataModel into a DAL project, created a BLL class library and separated the UI of the application into 2 web application projects and a website project. In doing so we lost the ability to run config file transformations on 3 out of the 5 projects that made up the solution. This was causing us some amount of headache due to the fact that we are using a non POCO EF model throughout our Web / BLL / DAL projects and we need to keep our EF DataModel connection strings in app.config to facilitate debugging and integration testing.

So I did a bit of research on how to best handle this situation and I found a Visual Studio extension called Slow Cheetah that allows you to run XML transforms on any config file in any type of project. Perfect! It even runs the transformations on build as well as on publish which can save a bit of time.

Now while this does work great I did find it a bit tricky to get it configured for each project type as the configuration settings are slightly different from project to project. I have outlined what worked for me below. I hope this helps someone else out there while working through this type of setup.

For Class Library Projects and Test Projects:

Set your app.config build action to ‘resource’ and copy to output directory to ‘copy always‘ on your app.config properties screen. Building the project will copy the transformed app.config file to your output directory which should be /bin/[configurationname] where configuration name is your currently selected configuration.

For Website Projects:

SlowCheetah does not work with website projects. As a workaround / hack I have been adding a [website].config file to one of my web application projects that contains all config for my website and running the transforms from that project.

For Web Application Projects:

Set your web.config build action to ‘content’ and copy to output directory to ‘copy always’. Web application transforms are executed when you Publish the project.

You can find Slow Cheetah here:

http://visualstudiogallery.msdn.microsoft.com/69023d00-a4f9-4a34-a6cd-7e854ba318b5

If you want to dig a bit deeper you can also read a great post by Scott Hanselman.

 

 

Don’t overlook NuGet

I am currently working on a project where I am using the NVelocity templating engine, which is a .NET port of the open source Apache Velocity project.  So I downloaded the package from SourceForge and dropped the NVelocity.dll file into my /bin folder thinking all would be well.  And it was locally, no problems at all.  It wasn’t until I went to deploy my Web Application Project that I started to run into issues.  It seemed that the .dll had some dependencies that were not documented ( at least not obviously! ) as I was getting the following on publish:

“Could not load file or assembly NVelocity.dll or one of its dependencies. The system cannot find the file specified.”

So I loaded up Dependency Walker to see what was going on and I noticed the following:

Error: Modules with different CPU types were found.

OK, so I am running a x64 system, that can be expected.  It also noted that IEShims.dll was missing, though that seemed to be a bit of a redherring.  After a while of wading through Dependecy Walker screens and wondering if I had just wasted the past half day of work it dawned on me that I hadn’t even checked NuGet.  And lucky enough NVelocity is available as a package there.

So I ran the NuGet package, it installed fine, and it deployed without any issues on the first try!

So the moral of the story is check NuGet first, there are tons of packages available and you might even find something new and interesting.

‘Object Does Not Exist’ in ASP.NET

I have a fairly large form that I have been working on that I have wrapped up in a .NET UserControl.  I had to switch focus onto another project for a couple of weeks so it became a bit of a distant and not so pleasant memory!  When I came back to it and added a button control to test some UpdatePanel postback issues I got the following error:

‘btnTest does not exist in the current context’

Now I hadn’t worked nor thought about this particular project for over a week so I wasn’t really sure what was going on.  It was a run-time error, everything compiled fine.  I ended up spending a couple of (expletive filled!) hours going down dead ends and getting nowhere.  Something just didn’t feel right about this but I couldn’t see what it was until I noticed a ContactForm_copy.ascx file in the UserControls folder in Visual Studio.  How did that get there?!  It seems I had made a filesystem copy of the UserControl and somehow managed to include it in my project.  After deleting the copy of the file everything worked as normal, problem solved.

So the moral of the story is: Don’t leave backup copies of your project files in the same folder as the original.  It seems like a reasonable thing to do to keep a backup copy around while experimenting, and everything compiles fine since we are dealing with partial classes in code-behind.  But save yourself some trouble and move your backups to a designated folder and avoid the dreaded  ‘Object Does Not Exist’ error that we all know and (don’t)  love.

Rackspace Medium Trust & PLINQO

So I got a nice email from Rackspace on Sunday informing me that they were going to change their security policy on their cloud hosting servers from full trust to medium trust… on wednesday! Which didn’t give me a whole lot of time to test my deployed websites. So I have spent the better part of a day running through security policy issues on various websites that have been affected. Getting PLINQO to work under partial trust was a bit of a challenge but I finally sorted that out too.

A few things to keep in mind:

SQLite will not run under medium trust, so if you are using that provider for ELMAH for instance you will need (and should anyway) switch to using the SQLErrorLog provider and remove the SQLLite dll from your bin folder to make sure you have no issues.

Rackspace provides a modifed medium trust config file that you can use in your dev environment for testing, that proved quite handy and can be found at http://help.rackspacecloud.com/article.php?id=383

If you are using Codesmith and / or PLINQO you will need to recompile the CodeSmith.Data.dll to allow partially trusted callers. You can get the latest build at http://community.codesmithtools.com/nightly/plinqo/. Just add a reference to System.Security in the AssemblyInfo.cs file and add the [assembly: AllowPartiallyTrustedCallers] attribute and recompile. Replace your existing CodeSmith.Data.dll in your data access project and rebuild the website and you should have no more security policy issues with PLINQO.

From this point forward I will keep my dev environment configured for RackSpace Medium Trust, I do not want to go through this pain again!

Extending LINQ to SQL with PLINQO

As mentioned I had moved to LINQ to SQL from EF a short time ago, primarily due to issues surrounding the use of detached entities and passing entities around an n-tier design. LINQ to SQL out of the box didn’t fully support what I wanted to do but it was more manageable than EF.

Enter PLINQO. PLINQO ( Professional LINQ to Objects ) is a set of CodeSmith templates that wrap and extend LINQ to SQL functionality. This has been a godsend for me, I have been using PLINQO for only a week now and it has already saved me a ton of time. Right off the bat you get full support for Entity.Detach and Entity.Clone which in and of itself would be compelling, but there is quite a bit more to PLINQO than that.

PLINQO also shines in that you can refactor your db schema and have that reflected in your dbml without having to drop and recreate entities which would under basic LINQ to SQL cause you to lose any customisations you may have implemented. PLINQO syncs changes from your db schema to the dbml model without any hassle at all and preserves your entity state while doing so.

There are some performance enhancements over basic LINQ to SQL too. PLINQO provides support for batch updating and deleting, effectively eliminating performance killing round trips to the server. The PLINQO generated DataContext also offers an overloaded ExecuteQuery method that lets you execute as many queries as you want within one trip to the database. This can save significant amounts of time if you have a bunch of queries that you need to perform one after another, like listing users, orders, products etc etc.

Only downside from my perspective is that you do need to use a regex pattern to exclude tables from your model, and I’m not afraid to admit that I just don’t speak regex very well. My regex is like my Spanish, I know the basics (Hola, Si, Cerveza) but I am in no way fluent so that’s a bit of a downer but I can live with that.

The team behind PLINQO seem to be constantly updating and adding new features, it’s an ongoing dynamic project and well worth a look.

LINQ to SQL

I am using a typical 3-tier architecture on the Privilege Solutions web application that I have been working on for the past couple of months. Nothing at all fancy here, just your normal ASP.NET website presentation tier with a BusinessObjects class library and a DataObjects class library. Nothing that would get me an oscar for innovation in software architecture there so I didn’t of course anticipate any problems.
So with my safe bet architecture I thought to myself why not reach a bit here and use Entity Framework for data access. Hadn’t used EF yet and really wanted to cut my teeth on that so I decided to go ahead and use it knowing it would add a bit of time to the schedule but would hopefull save time down the road. So I added the EF data model to my website project and got started. All was going well for the first few days until it was time to pull this all out into it’s own class library. That’s when the fun began!
The way EF manages entity state is just amazingly painful to deal with right now if you are moving entities around an n-tier architecture. There are work arounds but it just seemed at this point that the work involved far outweighed the benefit.
So I ended up pulling out the EF data model and throwing in a LINQ to SQL model in it’s place, which is quite a bit friendlier when using an n-tier architecture. I have developed a nice domain model by building the business objects as facades that wrap up various LINQ to SQL entities, so far so good!
Something to be aware of though when you have your LINQ to SQL model in a separate class library is the way the DataContext references the default connection string. If you create your DataContext without the default constructor you are in effect not providing a connection string and your connection string will be read from your config settings.
So you go about your business creating your connection string in App.config and name it ‘PrivilegeConnectionString’, same as in your web.config file. The problem here is that the DataContext actually uses the fully qualified typename for the connection string, something like “PrivilegeDataModel.Properties.Settings.PrivilegeConnectionString”, so upon deployment your connection string isn’t reconciling at all.
This took me ages to figure out what was really happening here, lot of poking around with different configurations until I realized what was happening. So just make sure you use a fully qualified typename as your connection string name in your web.config and it’s smooth sailing.
And the LINQ to SQL learning curve continues.