Visual Studio

A little fun with T4 Templates

So, if you haven’t heard yet about T4, it’s a built-in DSL tool that comes pre-packaged with Visual Studio starting with the 2005 version. Even though T4 can look a bit scary, it can be very useful if you need to generate code on the fly within Visual Studio. I will leave it up to Scott Hanselman to fill you in on all the details since he has an excellent blog post on what T4 is an where you can go for more information.

Last week I wanted to generate a object wrapper around the <appSettings> section in my web.config/app.config file and after doing the same thing over and over again by generating a public read-only property for each setting, I decided to use T4 to read the <appSettings> section and generate a class all the properties that would become the wrapper around the ConfigurationManager.AppSettings[“…”] calls to retrieve the settings. To my surprise, this was pretty easy to do. I did a C# and VB.Net version and here is the C# version of the T4 template.

<#@ assembly name="System.Core" #>
<#@ assembly name="System.Xml" #>
<#@ assembly name="System.Xml.Linq" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Xml.Linq" #>
<#@ import namespace="Microsoft.VisualBasic" #>
<#@ template language="VBv3.5" debug="True" hostspecific="True"  #>
<#@ output extension=".g.cs" #>
<#
	Dim projectNamespace as String = "SettingsGenerator"
	Dim fileName as String = "app.config"
	Dim path as String = ""

	Try
		path = Host.ResolvePath(fileName)
	Catch
		Try
			If fileName.ToLower() = "app.config" then
				fileName = "web.config"
			Else If fileName.ToLower() = "web.config" then
				fileName = "app.config"
			End If
			path = Host.ResolvePath(fileName)
		Catch
			path = "<< App.config or Web.config not found within the project >>"
		End Try
	End Try
#>
//------------------------------------------------------------------------------
// FileName = <#= path #>
// Generated at <#= Now.ToLocaltime() #>
//
// <auto-generated>
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
//
//	  NOTE: Please use the Add a Reference to System.Configuration assembly if
//	        you get compile errors with ConfigurationManager
// </auto-generated>
//------------------------------------------------------------------------------

using System.Configuration;

public static class AppSettings {

<#= RenderApplicationSettings(path) #>
}

<#+ 

	Public Shared Function RenderApplicationSettings(ByVal path as String) as String
		If Not File.Exists(path) Then Return ""

		Dim sb as New StringBuilder()

		Dim doc as XDocument = XDocument.Load(path)

		For Each result as XElement in doc...<appSettings>.<add>
			sb.Append(vbTab)
			sb.Append("public static string " + result.@key + "{")
			sb.AppendLine()
			sb.AppendLine(vbTab + vbTab + "get {")
			sb.AppendFormat("{0}return ConfigurationManager.AppSettings[""{1}""];{2}", vbTab + vbTab + vbTab, result.@key, vbCrLf)
			sb.AppendLine(vbTab + vbTab + "}")
			sb.Append(vbTab)
			sb.AppendLine("}")
			sb.AppendLine()
		Next

		Return sb.ToString()

	End Function
#>

Click the download images below to download the templates for both VB and C#. Until next time…

Happy Programming!

Introduction to MSBUILD on dnrTV

Earlier this week I watch the latest episode of dnrTV on MSBUILD and I must say I can’t wait for part two. MSBUILD is one of this hidden gems in .NET and this tool can be a real timesaver if you know how and when to use it. If you got a hour or so to spare I would encourage you to watch this episode and maybe you will have the same reaction that I did. Until next time…

 

Happy Programming!

image.png

Visual Studio 2010 Beta 1 released for MSDN Subscribers

Soma announced it today. I’ve got it downloaded and installed in my Windows 7 RC1 that boots from a .vhd file, a new feature with Windows 7.  Here is what it looks like:

 

image

 

 

 

 

 

 

Well, as you can see, I’ve already starting play around with it and if there is any gotchas I’ll be sure to blog about it. Anyways, if you don’t have a MSDN Subscription, then they are planning on releasing the bits on Wed for non-subscribers. Until next time…

 

Happy Programming!

image.png

My Experience with Visual Studio 2008 Remote Debugging

Surprisingly, it’s been quite pleasant.  I have tried Remote Debugging with VS.NET 2003, and I didn’t have much luck with it. However, last week there was a scenario where I desperately needed it and given that we have upgraded to VS 2008, I thought I should give it another try. There is two steps I had to perform to get this to work correctly:

  1. Configure your client machine using the Remote Debugger Configuration Wizard.
  2. Copy the Remote Debugger server components to the remote machine

That’s it.  Ok. I didn’t give you much here, so here is some more detail.

To run the Remote Debugger Configuration Wizard go to:

Start -> Program -> Microsoft Visual Studio 2008 -> Visual Studio Tools -> Visual Studio 2008 Remote Debugger Configuration Wizard

The wizard will begin and you will be brought to this screen:

 

 

 

 

 

 

 

 

 

This screen is basically asking if you want to run the remote debugger as a Windows Service. For me, I didn’t check the box to run as a service and it’s probably not even necessary. There is nothing much else to do but to click Next until the wizard completes. At the end it will tell you that all the necessary ports (most likely TCP) were opened and ready to connect to remote machines.

To run the server components on the remote machine, you need to copy the contents of this folder to the remote machine.

C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\Remote Debugger\x86

Once copied to the remote machine you will just have to run the msvsmon.exe program. The main window would look like this when it’s ready to accept incoming connections:

image

 

 

 

 

 

Play close attention to the server name listed in the entry. This is the name you will need to enter into the Qualifier field in the Debug -> Attach to Process screen inside Visual Studio when you want to begin the debugging process.

image

 

 

 

 

 

 

 

 

Remember to set your breakpoint first and attach to the process you want on the remote machine. The process you will want to debug should be listed in the Available Processes list.

One thing to watch out for is that in my setup, my username and password on my client machine and my remote machine were kept in sync. This way I don’t run into any unexpected access/permissions issues when connecting to the remote machine. I would also note that I was an Administrator on both machines so this would rule out any permission issues.

Let me know if you found this helpful. I do appreciate how the Visual Studio tools team simplified this process to it is as easy as following the 2 steps I listed above.

Until next time…

Happy coding!

kick it on DotNetKicks.com

organizeusings.png

New Feature: Organize using statements with VS 2008

Wow!  I will be using ( no pun intended ) this feature quite often!

 

OrganizeUsings

 

I’ve seen some VS macros that currently do this in VS 2005 and I think Visual Studio IDE enhancement tools like Resharper, has this feature already baked into their product. However, in VS 2008, we get this for free, at least if you are using C#. Until next time…

 

Happy Coding!

 

kick it on DotNetKicks.com

Ctrl-S in web.config in Visual Studio 2008 closes web.config file

Finally! Someone found a fix and root cause for this nagging bug I’ve been encountering with VS2008. You can find everything you need here. Thanks Rick! 

Until next time…

 

Happy Coding!

VS 2008 – "Visual Studio encountered an unexpected error"

Now that VS 2008 has been released to MSDN Subscribers, I went ahead like everyone else to rush to download and install it.  Not to my surprise, the install went perfectly since I didn’t install any pre-release version on my machine. I leaned my lesson last time when VS 2005 was released and all the headaches I had then. However, once installed, I was unable to use VS 2008 because it kept crashing and providing me with an error of "Visual Studio encountered an unexpected error"  when trying to interact with any part of the user interface. I couldn’t even exit from the program. I had to keep killing the process.  I’m like great, your not the only one who this error caught unexpectedly.  So, I figured a reboot would take care of it since I didn’t bother to reboot after the install. Nope, didn’t work.  I did a repair. Didn’t work. So, I did a complete uninstall, then reinstall and still didn’t work.  So, at this point I am frustrated since about 2 or 3 hrs have already passed.

I then decided to do some debugging…yes debugging…and I decided to start Visual Studio in Safe mode. You can do that by choosing:

Start, Run and enter this command     devenv /SafeMode

OR

Execute the same command from the command prompt directly.

After doing this, Visual Studio worked fine. I could use the menus, bring up dialog boxes, etc.  So, I believe that running is SafeMode doesn’t load any add-ins or custom VSIP packages, and I suspected one of these were probably causing the issue.  I was right, I use VisualSVN for accessing my Subversion source control repositories through Visual Studio. Its a great tool and even thought it $50, I find it very useful and much better than anything I could get for free.

So, I uninstall VisualSVN, version 1.2.4 (I think), and started VS 2008 and everything worked perfectly.  I then proceed to the VisualSVN web site to download the latest version, installed, and the new version and VS 2008 worked nicely.

I hope this helped somebody even if you don’t use VisualSVN, there may be some other add-in that may cause similar issues with VS 2008. Until next time…

Happy Coding!

 

 

kick it on DotNetKicks.com