Thursday, February 3, 2011

Creating Console apps that use the Server Profile

When you create a Console app in Visual Studio 2010, it gets create in a way that it targets the “.NET Framework 4 Client Profile”. What that means is that it can’t use any ASP.NET component, which for ASP.NET developers is rather useless.

The standard way to fix this is to go to the Project’s properties and change the Target Framework:

image

You then get an alert telling you that your project needs to be reloaded:

image

And once you click yes, you can be on your way to other greatness.

While this works, it’s frankly painful when you have to do this many times a day. To make things worse, if you forget to do it, you often get strange failures which don’t make it obvious what the issue is, leading to frustration or worse.

I have no idea who was behind the decision to make the default be the client profile, but I’ll go on record saying that it was a dumb idea! :)

Fix this permanently using a custom project template

Luckily, it’s pretty easy to fix this by using a custom VS Project Template. @luhmann sent one to me, so I didn’t even have to write it :)

Here is what you need to do:

  1. Go to this folder: %USERPROFILE%\Documents\Visual Studio 2010\Templates\ProjectTemplates\Visual C#\
  2. Under that, create a ‘Windows’ folder if you don’t already have one (you probably don’t)
  3. Download the custom template from here, and save it into that Windows folder (but don't unzip it!).

Now when you need to create a C# Console app, you’ll see a new entry from the custom template:

image

If you use that, your console app won’t be using the evil Client Template, which will lead to greater happiness.

Note that if you really wanted, you could replace the default template by that one, but I like seeing both entries side by side as a reminder of what’s going on. And who knows, some day I might just want to use the Client Template!

5 comments:

  1. David -

    Thanks for the tip - though I think Console apps targeting Client by default is the right decision. Console apps for ASP.NET are not common, at least in my experience (makes me curious what you're working on, actually).

    ReplyDelete
  2. @Stephen: you're probably right that most 'normal' console app should target the Client Profile. For me, most of my apps are small test things that call into System.Web or related assemblies, so that doesn't work. In any case, it would have been nice to be able to choose at project creation. The workflow to switch after creation is just too nasty.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. May i know the type of applications we can create with Console App(Not Client Profile) & advantages of them?

    ReplyDelete
  5. @sirigineedi it's needed when you want to use the full framework as opposed to the smaller subset. For me, System.Web is the big one that's missing. See http://msdn.microsoft.com/en-us/library/cc656912.aspx for more info.

    ReplyDelete