Tuesday, December 23, 2008

How to: Debug Windows Service in Visual Studio .NET

How to: Debug Windows Service in Visual Studio .NET


Debugging a Windows Service Application is a very unpleasant task because such application doesn’t run from within Visual Studio .NET. A Windows Service Application runs in the Services Control Manager context



In order to debug a service you must start it and then attach a debugger to the process in which it is running. After that, we can place breakpoints wherever we need to because all of the standard debugging functionality of Visual Studio .NET will be available. You can only attach the debugger to a running service. The attachment process interrupts the current functioning of your service; it does not actually stop or pause the service's processing. That is, if your service is running when you begin debugging, it is still technically in the Started state as you debug it, but its processing has been suspended


How to attach a debugger to a Windows Service
First ensure your service is installed and is running properly by inspecting the Service windows under Control Panel/Administrative Tools. Go to your Visual Studio’s main menu and choose: Debug/Attach process to… (this is available for Visual Studio .NET 2008



From the available processes list, choose the process you want to attach a debugger to. The service’s process will have the same name as the exe file generated by the compiler for that service.

Downsides of attaching a debugger

  • You can't debug the OnStart() and Main() methods because you can only attach a debugger to a service after it has started running, which by then, the OnStart() method has already completed its execution and returned the control to the operating system.
  • You must attach the debugger each time the project is restarted.
  • This feature is not availble for Visual Studio .NET Express Edition


Saturday, November 29, 2008

The type initializer threw an exception

Can not see the Form in the Designer. "The type initializer threw an exception." error
I got a problem recently when one of the forms I created could not be opened in Designer. Error message was as brief as "The type initializer for "..." threw an exception.". I found a good advice from Microsoft Developer Support guy Keith Fink how to debug the problem here.
Here is the advice:
Usually this means that your constructor of a particular type is throwing an exception. One way to debug this is to use aseparate instance of Devenv.exe and attach to the instance where yourproject is open, and then open the inherited form that causes the problem.

In the instance of Devenv.exe that you are using to debug with, followthese steps.

1. Start Devenv.exe.2. Select Tools->Debug Processes.3. Select the other instance of Devenv (where your project is loaded), andclick Attach.4. Make sure to select Common Language Runtime, and uncheck Nativedebugging, and click OK.5. Click Close.6. Select Debug->Exceptions.7. Expand Common Language Runtime Exceptions->System.8. Select the System.TypeInitializationException.9. Under the "When the exception is thrown" section, select the "Break intothe debugger" option, and click OK.10. Switch over to the other instance of Devenv, and open the form so thatthe exception is thrown. When that happens the first instance of Devenvshould break on the line of code where the exception is thrown.

One more hint - in the Form code DesignMode property is useful (it'll tell you if your Form is loaded in designer).
Note that the DesignMode property should not be checked from within the constructor or from any code that the constructor calls. A constructor is called before a control is sited, and it's the site that determines whether or not a control is in design mode. DesignMode will also be false in the constructor. I personally like to put it into Form_Load event (Form_FormClosing if you want to do something like logging on form close).
Example:
...
if(!this.DesignMode)
{
// chek authentication or something else
}

And finally a good article about
Design-time integration of controls.

10 Articles you should Read

10 .Dot net articels you should READ
Multi-Lingual Language Translations for ASP.NET
Here are a few free (paid for by me) language translations of common software terms for use in your multi-lingual ASP.NET application. This is particularly helpful if you want to create a list of available languages in Spanish, German, Chinese, etc... The language translation spreadsheet has generic term translations for Spanish, English, German, Italian, French, Chinese-Simple, and Japanese. The language name translations also includes Dutch, Korean, and Russian.
Dependency Injection using Spring.NET
Spring.NET is one of the popular open source framework ported from Java platform. It offers lot of functionality and in this article I will discuss about Dependency Injection and how it is supported in Spring.NET
Obfuscation and Packaging of .NET Applications via Compressed Embedded Assemblies
Shows a technique for embedding all the assemblies for an application as compressed, embedded resources into a single stub loader executable, extracting, decompressing and loading into the AppDomain at startup.
WPF & PowerShell - Part 7 (Sharing Hosts)
Well here we are at the end of a week of WPF. We've learned how to create basic, simple user interactive interfaces. We've seen a brief glimmer of the golden UI layer that is WPF, and have seen how we can use PowerShell to add easy interactivity to XAML. You've seen tricks to help you work your way through .NET code, and help you unwrap the mysteries of WPF. We've seen how we can use PowerShell's list processing technology allows for simple binding to WPF's controls, and how WPF applets can help you present a simple front ends to PowerShell functionality. We've gotten a very brief taste of what the pipeline can bring to User interfaces, and we've showed you how to make controls that run in the background so you can build your control in PowerShell and still use PowerShell.
Patterns in Practice - The Open Closed Principle
This is the first installment of a new MSDN® Magazine column on software design fundamentals. My marching orders are to discuss design patterns and principles in a manner that isn't bound to a specific tool or lifecycle methodology. In other words, my plan is to talk about the bedrock knowledge that can lead you to better designs in any technology or project.I'd like to start with a discussion of the Open Closed Principle and other related ideas popularized by Robert C. Martin in his book, Agile Software Development, Principles, Patterns, and Practices. Don't be turned off by the word "agile" in the title, because this is all about striving for good software designs.
How to Find Memory Leaks With CLRProfiler
We all know managed code can have memory leaks. You can find a good example here: A .NET memory leak you did not think about. Microsoft provides us with the CLR Profiler, an open source tool for analyzing the behavior of your managed application, which you can download here. It contains very good documentation about the different functions of the tool, however I still find it a bit hard to start with, so here is a simple step-by-step example of how to use it.
Animation in-depth with Silverlight 2.0 Beta - Part Four
In this example I'll show you how animations can be performed using a VideoBrush. This is one of the examples that attracts the users attention by completing the animation at the right time. The video will be clipped and rotated during the animation.
IronRuby on Rails. Microsoft makes it happen.
Now... No more doubts about Open Source Support by Microsoft. The first ever Open Source Project comes live now.
Mr. John Lam, the person behind IronRuby has provided more information on his blog here...
http://www.iunknown.com/2008/05/ironruby-and-rails.html Here are the Highlights of his blog.
Optimization Tips: Using HTTP Compression
I know it has been a few weeks since the last installment in this series, and with TechEd on the horizon it'll probably be a couple 'til the next, but as long as there is some time in between let's explore another area of performance optimization with the RadControls for ASP.NET AJAX. This week, we're going to take a look at HTTP compression and how this simple technique can deliver a valuable performance boost to your website.
ASP.NET Memory: Thou shalt not store UI objects in cache or session scope
Surprisingly the issue I wrote about in "the eventhandlers that made the memory baloon" (Jan 2006) is something that still happens very frequently, I reference it in cases at least a few times a month. Just this last week I had different variations of it crop up in different cases so in this post I will show a different variation, what to look out for and how to identify it.

Using enum in .net

Using a Enum in C#.net
Code Sample
// declaring a enum
enum Operations
{
add,sub,mul,div
};


public void mymethod()
{
// creating object of enum
Operations op ;

// assinging value to enum
op = Operations.add ;

// using it to compare
if(op== Operations.add)
{
// do somethng.. addition
}

}