If you are planning to create your own Add-in for Visual Studio, I found a very good source written by Jon Rista - Writing a Visual Studio 2005 Add-in.
Follow the link below and you'll see explanation and will have ability to download source code (C#).
Monday, April 23, 2007
How to create an Add-in for Visual Studio?
Monday, April 16, 2007
Bible for Refactoring.
Have you seen how code “smells”? If not yet, strongly suggesting you to read the book of Martin Fowler “Refactoring: Improving the Design of Existing Code”. That is another bible for software developers. Many if not all of us are cleaning the code, changing structure, making refactoring based on code style we like.
This book explains why refactoring is so important and brings refactoring to the level of systematization, makes strong agreement between developers to "when" and "how" to refractor the code. Shows how to simplify communication between developers by using terms which contains definition of “problem” and “solution”.
I would like add myself that refactoring should be a part of the software development cycle and the book helps to make it happened.
So, read a book and you will see how your code code “smells” :).
Saturday, April 14, 2007
ASP.NET: Monitoring of web page life cycle
I wrote simple code which is showing order of execution of events and protected methods related to the page life cycle.
I simply added output messages into the each event and protected method of Page and Control (UserControl which is located on that page). This is an example how I used it:
...
protected void Page_Init(object sender, EventArgs e)
{
Container.messageCollection.Add("P: Page_Init");
}
...
protected override void OnInit(EventArgs e)
{
Container.messageCollection.Add("P: Before base.OnInit");
base.OnInit(e);
Container.messageCollection.Add("P: After base.OnInit");
}
etc.
After filling a collection I just write messages out in order how they were added.
And this is an output result:
1: P: Before base.OnPreInit
2: P: Page_PreInit
3: P: After base.OnPreInit
4: C: Before base.OnInit
5: C: Page_Init
6: C: After base.OnInit
7: P: Before base.OnInit
8: P: Page_Init
9: P: After base.OnInit
10: P: Before base.OnInitComplete
11: P: Page_InitComplete
12: P: After base.OnInitComplete
13: P: Before base.OnPreLoad
14: P: Page_PreLoad
15: P: After base.OnPreLoad
16: P: Before base.OnLoad
17: P: Page_Load
18: P: After base.OnLoad
19: C: Before base.OnLoad
20: C: Page_Load
21: C: After base.OnLoad
22: P: Before base.OnLoadComplete
23: P: Page_LoadComplete
24: P: After base.OnLoadComplete
25: P: Before base.OnPreRender
26: P: Page_PreRender
27: P: After base.OnPreRender
28: C: Before base.OnPreRender
29: C: Page_PreRender
30: C: After base.OnPreRender
31: P: Before base.OnPreRenderComplete
32: P: After base.OnPreRenderComplete
33: C: Before base.OnUnload
34: C: Page_Unload
35: C: After base.OnUnload
36: P: Before base.OnUnload
37: P: Page_Unload
38: P: After base.OnUnload
First "C" letter or red color are messages from UserControl side. Messages with "P" letter or blue color represent page cycle in Page level.
So, now you can see an order how events in page and contol occure during page life cycle.
ASP.NET: Monitoring of web page life cycle
I wrote simple code which is showing order of execution of events and protected methods related to the page life cycle.
I simply added output messages into the each event and protected method of Page and Control (UserControl which is located on that page). This is an example how I used it:
...
protected void Page_Init(object sender, EventArgs e)
{
Container.messageCollection.Add("P: Page_Init");
}
...
protected override void OnInit(EventArgs e)
{
Container.messageCollection.Add("P: Before base.OnInit");
base.OnInit(e);
Container.messageCollection.Add("P: After base.OnInit");
}
etc.
After filling a collection I just write messages out in order how they were added.
And this is an output result:
1: P: Before base.OnPreInit
2: P: Page_PreInit
3: P: After base.OnPreInit
4: C: Before base.OnInit
5: C: Page_Init
6: C: After base.OnInit
7: P: Before base.OnInit
8: P: Page_Init
9: P: After base.OnInit
10: P: Before base.OnInitComplete
11: P: Page_InitComplete
12: P: After base.OnInitComplete
13: P: Before base.OnPreLoad
14: P: Page_PreLoad
15: P: After base.OnPreLoad
16: P: Before base.OnLoad
17: P: Page_Load
18: P: After base.OnLoad
19: C: Before base.OnLoad
20: C: Page_Load
21: C: After base.OnLoad
22: P: Before base.OnLoadComplete
23: P: Page_LoadComplete
24: P: After base.OnLoadComplete
25: P: Before base.OnPreRender
26: P: Page_PreRender
27: P: After base.OnPreRender
28: C: Before base.OnPreRender
29: C: Page_PreRender
30: C: After base.OnPreRender
31: P: Before base.OnPreRenderComplete
32: P: After base.OnPreRenderComplete
33: C: Before base.OnUnload
34: C: Page_Unload
35: C: After base.OnUnload
36: P: Before base.OnUnload
37: P: Page_Unload
38: P: After base.OnUnload
First "C" letter or red color are messages from UserControl side. Messages with "P" letter or blue color represet page cycle in Page level.
So, now you can see an order how events in page and contol occure during page life cycle.
Monday, April 9, 2007
.NET 2.0. Access file in Design Time mode.
Most of programmers who are dealing with ASP.NET know how to access files on the server. The common way how to do this is to use HttpContext.Current.Server.MapPath static method and get physical path of the file by passing virtual. But what if we need for instance to read some data from text file which located in our project in Design Time mode. Server.MapPath won't help us so much as our application is not hosted by IIS or Cassini and HttpContext didn't fill yet. How to get physical path of the file then? Thanks to .Net 2.0 it is simple.
For instance let's try to get physical path of the file (sample.txt) stored in the root directory of project thru server control .
To achieve our goal we need three players ISite, IWebApplication and IProjectItem.
ISite - Gets information about the container that hosts the current control when rendered on a design surface.
We have this property available in control level (Control.Site). Be aware Site property is filled only in design time mode and not available when application hosted under the IIS or Cassini.
IWebApplication - IServiceProvider based interface which provides access to Web application in Design Mode.
By using this service we have ability to access items in our projects and reading web.config file in Design Mode.
IProjectItem - Provides an interface for an item that is retrieved at design time.
Contains such information as physical path, virtual path, name etc
Step 1.
First we have to get IWebApplication service from the list of running services.
IWebApplication webApp = (IWebApplication)Site.GetService(typeof(IWebApplication));
Step 2.
Get project item by calling appropriate method.
IProjectItem item = webApp.GetProjectItemFromUrl("~/sample.txt");
Step 3.
Then just get information about physical path from property of IProjectItem interface.
string filePhysicalPath = item.PhysicalPath;
That is it. Easy isn't it, but big headache for the programmers who need that and doesn't know about it. :)
Sunday, April 8, 2007
Community Server: Google Indexing
Due to fact that Community Server stores context of blogs in SqlServer, google won't index them.
How to make google to start indexing your blogs on Community Server? Dan Bartels created an Add-on which is dynamically creates sitemap for google. Follow by the link and download files with installation extraction.
http://communityserver.org/files/folders/add-ons/entry547382.aspx
Installation procedure takes 5 minutes.
Tuesday, April 3, 2007
How to determine Design Time mode in ASP.NET
During creating of Custom Controls sometimes programmers are dealing with implementation of code which should run only in Design Time mode. And the main question is, how to determine in ASP.NET whether your code is running in Design Time mode or not?
I can offer several approaches for checking that.
DesignMode property
First and obvious decision is to use DesignMode property of Control class. It is a Boolean value which returns true in case when your code in running in Visual Studio.
But this approach has two disadvantages:
1. What if you need to determine design time mode not in control implementation but in some class which is part of your WebSite project. In that case access to DesignMode won’t be such easy ;)
2. DesignMode property is new one in .Net Framework 2.0 and not exists in 1.1
There are several ways how to avoid this problem.
HttpContext
We can check HttpContext object against null value. During design time mode HttpContext object is empty which means we can write something like this:
public bool IsDesignMode
{
get { return System.Web.HttpContext.Current == null; }
}
HostingEnvironment
Second approach is to use IsHosted static property of HostingEnvironment object which provides functionality to manage application within application domain. During design time application is not hosted.
public bool IsDesignMode
{
get{ return !System.Web.Hosting.HostingEnvironment.IsHosted; }
}
Site
Next approach is to use System.Web.UI.Control.Site . It gets information about a container which hosts the current control when rendered on a design surface . And in our case the code can look like this:
public bool IsDesignMode
{
get { return Site!=null; }
}
I tried all approaches during control implementation and all of them are working fine.
Asp.Net: How to debug custom controls in Design Time mode.
I am glad to say “YES”, it is possible and easy.
It will be much easy and clear if we do it together step by step in concrete example.
First of all lets create a test WebControlLibrary project.
Then right click on project and select property.
In the property window navigate to Debug section and switch radio button from “Start Project” (set by default) to “Start external program” and browse for devenv.exe executable of Visual Studio which you are using.
Close the property window and for testing purposes set a breakpoint in RenderContent method.
To start debugging right click on project and select “Debug --> Create New Instance”
Debug process will create an instance of Visual Studio. To test behavior of control you can choose one of your existing web projects or create new one. For now I will create fresh WebSite project. Notice that original version of Visual Studio is in Debug mode. During creation or opening web project I am suggesting to turn off any Add-ins if you are using in Visual Studio. Sometimes those tools are raising exceptions and I don’t see reason to use them during testing of design time behavior of custom control.
Now it will be maybe easier to add our assembly to the Toolbox and use it from there. It will automatically add reference of our assembly. By using Drag and Drop technique it will automatically register assembly on a page .
On Toolbox right click and select “Choose Items”.
Navigate for our assembly by using “Browse” button. Make sure that you are picking debug version of assembly (by default it will be located in Debug folder inside of Bin).
After clicking “OK” on “Choose Toolbox Items” window our custom control (or controls if you have several in project) will appear in Toolbox window of Visual Studio.
Now just find it there, drug and drop to the web page. Switch from “Source” mode of your page editor to “Design” and “the magic will appear” :)
Now add breakpoints you need, step thru code, do the stuff you are usually doing during debugging. Next time you can just run "Debug-->Create New Instance" and just choose created WebSite project.
I hope this information will help you to create nice, professional controls without any bugs :).
Have a fun.

