Like it

Sunday, July 17, 2011

Debugging Windows Services

In the current project, I have to develop some windows services to sending out notification mails. The first challenge I faced is I can't directly run and debug the Windows service. For that I need to install the service (using installutil –I ServiceName) and attach the process to Visual studio, and then only I can debug it. It works fine for me. Then the next challenge was, debugging the code in the onStart event. Because once the service started, then only we can able to attach it to Visual Studio. So I put some code to write to text file, and/or event log. But I feel like it's not a good method. After searching in the net I got a nice workaround.
protected override void OnStart(string[] args)
{
System.Diagnostics.Debugger.Launch();
//Rest of the code
}

While starting the Service, it will display a dialog like this




After selecting Yes, Visual Studio will open up and you can start debugging. You can get more information from these links
  1. http://blogs.msdn.com/yaleeyangmsblog/archive/2007/05/02/three-ways-debugging-net-windows-service.aspx
  2. http://geekswithblogs.net/GertVerhoeven/archive/2007/11/28/117181.aspx
  3. http://msdn.microsoft.com/hi-in/magazine/cc301845(en-us).aspx

WPF Menu - How to implement Shortcut Keys

WPF Menu - How to implement Shortcut Heys
There are many ways to add shortcut keys in WPF
# Using Underscore - In the Header Property, put the "_" before the Text, and you can use "ALT+<Key>". This is not Shortcut key, it is normally called "HotKey"

   1:  <MenuItem Header="_Open" /> 
# Using InputGestureText - This is the way Visual Studio gives directly. - You can set a Key combination directly over here. 

   1:  <MenuItem Header="_Open" InputGestureText="Ctrl + O" /> 
But it will invoke the click event, even if we use the Key combination. This is for display purpose only.
#Using CommandBindings - - I found this way is better and easier one
  1. In the markup create the command Bindings using Command Collections.
    <CommandBinding Command="ApplicationCommands.Open" 
    CanExecute="CommandCanExecute" Executed="CommandExecuted" /> 
    <CommandBinding Command="ApplicationCommands.Close" 
    CanExecute="CommandCanExecute" Executed="CommandExecuted" />
    
    
  2. In the code implement the event Handlers.
       1:  Sub CommandExecuted(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)
       2:  'Add the commands to be executed.
       3:  If e.Command.Equals(ApplicationCommands.Open) Then
       4:  MessageBox.Show("Open Menu item clicked")
       5:  ElseIf e.Command.Equals(ApplicationCommands.Close) Then
       6:  MessageBox.Show("Close menu item clicked")
       7:  End If
       8:  End Sub
       9:  Sub CommandCanExecute(ByVal sender As Object, ByVal e As CanExecuteRoutedEventArgs)
      10:  e.CanExecute = True 'I am always returning true, based on this the menu item will enabled / disabled
      11:  End Sub
  3. Assign the Commands to the Command property of our menu items.
    <MenuItem Command="ApplicationCommands.Open" >  <Separator />   
    <MenuItem Command="ApplicationCommands.Close" />
    Compile it run it. You will see "Ctrl + O" added to the right side Open menu item.
    You can see Ctrl + O added into the Open Menu item(Screenshot)
menu_shortcuts_in_wpf
Happy WPF Programming :)

A Simple Splash screen in C#

I started my .net career in VB.Net. While developing a windows application in VB.Net it is pretty easy to put a Splash screen. There is Project property called Splash screen and you can put any form there, after that the specified form will act as a Splash screen. But when I started an windows application in C#, there is no project property called Splash screen. While doing some searching, I found lot of ways, like Putting a timer and closing the splash etc. But I feel like it is not the right way of doing a splash screen, then I started my own implementation and found one. I am not sure it is good one or not, but it is working for me.

Here is the steps:

1) Added one Form with an Image, as Splashscreen.cs, and my main UI is Welcome.cs
2) In the Program.cs I modified the code like the following

[STAThread]
static void Main()
{
    Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    SplashScreen s = new SplashScreen();
    s.Show();
    Welcome welcome = new Welcome(s);
    Application.Run(welcome);
}

3) And in the Welcome.cs, added one parameterized constructor, with SplashScreen as the Parameter

SplashScreen _s = null;
public Welcome(SplashScreen s)
{
    InitializeComponent();
    this._s = s;
}

4) And in the Form_Load() event I added code to close the splash screen if exists.

if (this._s != null) { this._s.Close(); }

5) Run the application, see the splash is coming and after that Main UI is coming

ASP.NET 2.0 Chat that uses Membership functionality

AspxChat - generic MsAjax-based chatting control for ASP.Net 2


  • Uses standard ASP.Net 2.0 Membership functionality to manage users, which allows a seamless integration into existing applications that use Membership without a single line of code
  • Utilizes Microsoft AJAX Extensions to present a user interface in asynchronous fashion, that is: without continuous page refreshes
  • Provide a simple administrative interface for users in Administrator role
  • Provide a generic voting functionality with a Yes/Now poll
  • Uses implicit localization feature of ASP.Net 2.0 to present a user interface in different languages (English, Ukrainian and Russian localization included)
  • Uses ASP.Net 2.0 Themes feature to fine tune user interface
  • Provide a default page with a standard ASP.Net 2.0 Login control to enable using AspxChat as a stand-alone application
  • AspxChat is configurable with a standard web.config file (presently only 2 parameters can be customized: history depth and default name for non-authenticated users)
If you have any tips, tricks, coding samples or source code you think the group would find useful please email them to me at nadir.fsd@gmail.com

Freehand drawing with .Net Windows forms

Few days back, I got a requirement to implement Free Hand drawing in .Net. I searched a lot but I didn't get any good solution, the one I found from code project was Invalidating the picture box every time. So today I got a solution, it not seems to be a perfect solution because Paint events occurs, we need to re-paint the whole lines again.
I am attaching the code here. I will update the post once I get some perfect solution for this.
Private m_Drawing As Boolean = False
    Private m_List As List(Of Point) = Nothing
    Private Sub MyPic_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyPic.MouseDown
        If e.Button = Windows.Forms.MouseButtons.Left Then
            m_Drawing = True
        End If
    End Sub
    Private Sub MyPic_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyPic.MouseUp
        If m_Drawing AndAlso m_List IsNot Nothing Then
            If m_List.Count >= 2 Then
                MyPic.CreateGraphics.DrawLines(New Pen(Color.Blue, 1), m_List.ToArray())
            End If
            m_Drawing = False
            m_List.Clear()
        End If
    End Sub
    Private Sub MyPic_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyPic.MouseMove
        If m_List IsNot Nothing AndAlso m_Drawing Then
            m_List.Add(e.Location)
        End If
    End Sub
    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        m_List = New List(Of Point)
    End Sub

ASP.NET, ASP.NET MVC, .NET and NuGet

Here is the latest in my link-listing series.  Also check out my Best of 2010 Summary for links to 100+ other posts I've done in the last year.
[I am also now using Twitter for quick updates and to share links. Follow me at: twitter.com/scottgu]

ASP.NET

  • Introducing new ASP.NET Universal Providers: Great post from Scott Hanselman on the new System.Web.Providers we are working on.  This release delivers new ASP.NET Membership, Role Management, Session, Profile providers that work with SQL Server, SQL CE and SQL Azure.
  • SassAndCoffee 0.9 Released: Paul Betts blogs about the latest release of his SassAndCoffee extension (available via NuGet). It enables you to easily use Sass and Coffeescript within your ASP.NET applications (both MVC and Webforms).

ASP.NET MVC

  • ASP.NET MVC Mini-Profiler: The folks at StackOverflow.com (a great site built with ASP.NET MVC) have released a nice (free) profiler they've built that enables you to easily profile your ASP.NET MVC 3 sites and tune them for performance. 
  • Precompile your MVC Razor Views: Great post from David Ebbo that discusses a new Razor Generator tool that enables you to pre-compile your razor view templates as assemblies – which enables a bunch of cool scenarios.
  • Unit Testing Razor Views: Nice post from David Ebbo that shows how to use his new Razor Generator to enable unit testing of razor view templates with ASP.NET MVC.
  • Bin Deploying ASP.NET MVC 3: Nice post by Phil Haack that covers a cool feature added to VS 2010 SP1 that makes it really easy to \bin deploy ASP.NET MVC and Razor within your application. This enables you to easily deploy the app to servers that don't have ASP.NET MVC 3 installed.

.NET

  • Table Splitting with EF 4.1 Code First: Great post from Morteza Manavi that discusses how to split up a single database table across multiple EF entity classes.  This shows off some of the power behind EF 4.1 and is very useful when working with legacy database schemas.
  • Choosing the Right Collection Class: Nice post from James Michael Hare that talks about the different collection class options available within .NET.  A nice overview for people who haven't looked at all of the support now built into the framework.

NuGet

  • NuGet 1.4 Released: Learn all about the latest release of NuGet – which includes a bunch of cool new capabilities.  It takes only seconds to update to it – go for it!
  • NuGet in Depth: Nice presentation from Scott Hanselman all about NuGet and some of the investments we are making to enable a better open source ecosystem within .NET.
Hope this helps,
Scott

Great Free Video Training on ASP.NET Web Forms and ASP.NET MVC

We've recently published some great end-to-end ASP.NET video training courses on the http://asp.net web-site. 
Created by Pluralsight (a great .NET training company), these video courses are available free of charge and provide a great way to learn (or brush-up your knowledge of) ASP.NET Web Forms 4 and ASP.NET MVC 3.  Each course is taught by a single trainer, and provides a nice end-to-end curriculum (from basic concepts to working with the new Entity Framework "code first" model to security, deployment, and testing).   
Below are some details on the two free training courses we published this weekend (and links for how to watch them):

ASP.NET MVC 3 Training

This weekend we posted the final videos in a brand new 10 part ASP.NET MVC 3 training course taught by K Scott Allen.  You can now watch the entire series for free on the http://asp.net/mvc page (it is on the left-hand side within the "Essential Videos" section):
image
Below is a breakdown of the 10 video modules in the ASP.NET MVC 3 series:
You can find pointers to the entire series on the http://asp.net/mvc page.

ASP.NET Web Forms 4 Training

Dan Wahlin is developing a great course on ASP.NET Web Forms 4.
You can watch the series for free on the http://asp.net/web-forms page (it is on the left-hand side within the "Essential Videos" section):
image
Below is a breakdown of the 9 video modules in the ASP.NET Web Forms series:
You can find pointers to the entire series on the http://asp.net/web-forms page.  We'll be posting links to the final 3 videos in the series later this month.

Even More Content Coming

Keep an eye on the http://asp.net/web-forms and http://asp.net/mvc learning centers in the months ahead as we post even more textual and video training content.  Each of the two learning centers now has a nice structured outline of content that you can use to quickly look things up as well as learn how ASP.NET works.  We'll be refining and extending the content even more in the weeks and months ahead.
Hope this helps,
Scott
P.S. In addition to blogging, I am also now using Twitter for quick updates and to share links. Follow me at: twitter.com/scottgu