Have you ever wondered about the scope and life time of static (Shared in VB.Net) variables in ASP.Net?
- Do they get destroyed after each requests?
- Do they get destroyed after the IIS services is restarted?
- multiple apps run in the same Application Pool, do they all get the same copy (and value) of static variables?
- Does a Singleton make sense in ASP.Net
Here is what I could gather from doing some research on the web:
- static variables work in ASP.Net the same way they work in a Windows app: they are global across the whole .Net AppDomain
- each ASP.Net application gets their own AppDomain, even if they are hosted in the same application pool (same w3wp.exe or aspnet_wp.exe)
- so even if you have multiple ASP.Net applications per Application Pool, they would still get their own AppDomains, and thus the singletons/shared variables would be isolated from each other
Some people encourage initializing the singletons/static variables in the Application_Startup event, just to make sure that all the ASP.Net infrastructure.
Here are some supporting links:
http://weblogs.asp.net/jeff/archive/2007/09/21/how-do-you-get-a-true-singleton-in-an-asp-net-app.aspx
http://www.odetocode.com/Articles/305.aspx
http://blogs.msdn.com/david.wang/archive/2005/09/01/HOWTO-Provision-ASP-dotNet-AppDomains-and-IIS6-Application-Pools.aspx
http://www.vzyl.co.za/post/2008/04/Singleton-Design-Pattern-amp3b-Lifetime-of-a-static-variable-in-ASPNET-20.aspx
http://www.yoda.arachsys.com/csharp/singleton.html
Friday, April 18, 2008
Friday, November 16, 2007
BLowery Compression module and Visual Studio Webserver (Cassini)
We have been using the HTTPCompress module (BLowery), and when running your web site in development mode using the built-in web server in Visual Studio 2005 you can get the error "Server cannot append header after HTTP headers have been sent." in cases where you want to do a Response.Flush (e.g. for downloading a file to the client).
Now the HTTPCompress module can be configured to exclude certain pages by using the "excludedPaths" tag in the web.config file. But this did not seem to work.
The cause was this:
This is the line in the HTTPCompress Module that is supposed to chop off the absolute portion of the path:
string realPath = app.Request.Path.Remove(0, app.Request.ApplicationPath.Length+1);
This works fine if your web application is not at the root. But in the Visual Studio Development Web Server (formerly Cassini), the Request.ApplicationPath always comes back as "/". In that case the above code will actually chop off the first character of the relative path.
I.e. "/WebResource.axd" will become "ebResource.axd".
The Solution:
This is ugly, but when you do development using the Visual Studio built-in web server you need to add both paths to the web.config file.
<excludedPaths>
<add path="ebresource.axd"/>
<add path="WebResource.axd"/>
</excludedPaths>
Of course another option is to modify the HTTPCompress source code and fix this.
Now the HTTPCompress module can be configured to exclude certain pages by using the "excludedPaths" tag in the web.config file. But this did not seem to work.
The cause was this:
This is the line in the HTTPCompress Module that is supposed to chop off the absolute portion of the path:
string realPath = app.Request.Path.Remove(0, app.Request.ApplicationPath.Length+1);
This works fine if your web application is not at the root. But in the Visual Studio Development Web Server (formerly Cassini), the Request.ApplicationPath always comes back as "/". In that case the above code will actually chop off the first character of the relative path.
I.e. "/WebResource.axd" will become "ebResource.axd".
The Solution:
This is ugly, but when you do development using the Visual Studio built-in web server you need to add both paths to the web.config file.
<excludedPaths>
<add path="ebresource.axd"/>
<add path="WebResource.axd"/>
</excludedPaths>
Of course another option is to modify the HTTPCompress source code and fix this.
Friday, June 1, 2007
Formatting a Column in a GridView with DataFormatString
Today I came across a little gotcha with the ASP.Net GridView control.
If you have a bound column bound to a DateTime field and you want to change the formatting say from "05/30/2007 15:25:22" to "05/30/2007" then the way to do this is to use the DataFormatString parameter of the BoundField:
<asp:BoundField datafield="EVENT_DATE" headertext="Event Date" DataFormatString="{0:MM/dd/yyyy}">
<ItemStyle width="10%"></ItemStyle>
</asp:BoundField>
But this didn't seem to work for some reason. The fix is to switch off HTML encoding for the column, as explained in this Microsoft post.
So the correct code is:
<asp:BoundField datafield="EVENT_DATE" headertext="Event Date" DataFormatString="{0:MM/dd/yyyy}" HtmlEncode="False">
<ItemStyle width="10%"></ItemStyle>
</asp:BoundField>
Now it works like a charm ...
If you have a bound column bound to a DateTime field and you want to change the formatting say from "05/30/2007 15:25:22" to "05/30/2007" then the way to do this is to use the DataFormatString parameter of the BoundField:
<asp:BoundField datafield="EVENT_DATE" headertext="Event Date" DataFormatString="{0:MM/dd/yyyy}">
<ItemStyle width="10%"></ItemStyle>
</asp:BoundField>
But this didn't seem to work for some reason. The fix is to switch off HTML encoding for the column, as explained in this Microsoft post.
So the correct code is:
<asp:BoundField datafield="EVENT_DATE" headertext="Event Date" DataFormatString="{0:MM/dd/yyyy}" HtmlEncode="False">
<ItemStyle width="10%"></ItemStyle>
</asp:BoundField>
Now it works like a charm ...
Thursday, May 31, 2007
How to enable ASP applications and ASP.Net applications on WSS
On a server running Windows Sharepoint Services (WSS) where WSS has taken over the default web site, ASP applications are disabled by default. Also, ASP.Net applications will not run by default since WSS imposes some serious security restrictions on the web site. See this MS KB article (828810) for details and resolution. Basically you need to set up an excluded path in the Sharepoint Services Administration page.
Friday, May 18, 2007
Object-Oriented Techniques in Javascript
Here is a good MSDN article about some of the more object-oriented principles in JavaScript:
Create Advanced Web Applications With Object-Oriented Techniques.
Create Advanced Web Applications With Object-Oriented Techniques.
Wednesday, May 16, 2007
AJAX Tips and Tricks
Here is an interesting article about some AJAX internals, put together by one of the co-founders of PageFlakes. Interesting is especially the part on caching web service responses (if appropriate in your case).
Monday, May 14, 2007
Microsoft JScript runtime error: 'AjaxControlToolkit' is undefined
Just ran into this weird error when trying to add an AJAX control to a web page and we spent a lot of time trying to resolve this.
Basically the web page worked fine but when moving it to the server it generated a client-side JS error: "Microsoft JScript runtime error: 'AjaxControlToolkit' is undefined".
this is the corresponding code:
Sys.Application.add_init(function() {
$create(AjaxControlToolkit.ModalPopupBehavior, {"BackgroundCssClass":"modalBackground", ...}, null, null, $get("btnNew"));
});
Google search did reveal people having similar problems but no good solution.
Then there was some good information in this thread.
Basically it turns out that the the script is being compressed under certain circumstances (like debug mode vs. non-debug) and whether the compression settings are defined in the web.config file.
Here is what fixed it for us. The important setting is the one "enableCompression='false'":
<system.web.extensions>
<scripting>
<scriptResourceHandler enableCompression="false" enableCaching="true">
</scriptResourceHandler >
</scripting>
</system.web.extensions>
Make sure you switch the compression off, since it seems it does not work, even when using IE7 like I do.
Basically the web page worked fine but when moving it to the server it generated a client-side JS error: "Microsoft JScript runtime error: 'AjaxControlToolkit' is undefined".
this is the corresponding code:
Sys.Application.add_init(function() {
$create(AjaxControlToolkit.ModalPopupBehavior, {"BackgroundCssClass":"modalBackground", ...}, null, null, $get("btnNew"));
});
Google search did reveal people having similar problems but no good solution.
Then there was some good information in this thread.
Basically it turns out that the the script is being compressed under certain circumstances (like debug mode vs. non-debug) and whether the compression settings are defined in the web.config file.
Here is what fixed it for us. The important setting is the one "enableCompression='false'":
<system.web.extensions>
<scripting>
<scriptResourceHandler enableCompression="false" enableCaching="true">
</scriptResourceHandler >
</scripting>
</system.web.extensions>
Make sure you switch the compression off, since it seems it does not work, even when using IE7 like I do.
Monday, April 23, 2007
Winforms Tabbed UI using multiple forms
The problem at hand was how to create a Windows Forms application that uses a tabbed UI, but where you want to put the logic into separate forms, i.e. each tab page is a separate WinForm (this is mainly to not have all the code and controls in the main page).
I found 3 main approaches to this (there are probably more, but these accomplish the stated goal without excessive coding):
1. the MDIWindowManager project (free under BSD license)
2. the DockPanel Suite project (free, from SourceForge)
3. do-it-yourself (VB.Net code)
Use this method in the main form to add a form to a tab page.
The do-it-yourself method is simple and works, the other 2 links with the accompanying source code provide more functionality.
I found 3 main approaches to this (there are probably more, but these accomplish the stated goal without excessive coding):
1. the MDIWindowManager project (free under BSD license)
2. the DockPanel Suite project (free, from SourceForge)
3. do-it-yourself (VB.Net code)
Private Sub AddFormToTab(ByVal objForm As Form, ByRef objTabPage As TabPage)
objForm.TopLevel = False
objForm.Text = ""
objForm.MaximizeBox = False
objForm.MinimizeBox = False
objForm.ControlBox = False
objTabPage.Controls.Add(objForm)
objForm.Dock = DockStyle.Fill ' Expands the form to fill the tabpage
objForm.Show()
End Sub
Use this method in the main form to add a form to a tab page.
The do-it-yourself method is simple and works, the other 2 links with the accompanying source code provide more functionality.
Tuesday, April 17, 2007
Everyone's talking about RIAs now...
It seems to me that the current hot acronym is RIA (Rich Internet Applications). I first read about it on Adobe's website when reading about Adobe Apollo (see my post on this). Now Rick Strahl has published a very good post about the trends we're seeing in the end user application development space. He does a great job putting things like WPF, WPF/E, Adobe Flex/Apollo, HTML/AJAX in perspective. As I said before, something is going on there that every serious developer needs to keep an eye out for...
I like one of the comments to his post (and pretty much mirrors my experiences):
I like one of the comments to his post (and pretty much mirrors my experiences):
"Another great read Rick. I have to admit that the last two big projects that I was on both were vb6 -> asp.net/ajax conversions and BOTH got halfway through development and were re-architected for clickonce and wpf because ajax still doesn't allow you to throw 30 infragistics web combos on a page. Some presentation layers just don't work well with html and probably never will granted time and a hardware refresh might change that.
When there's a preexisting expectation of the thick client experience, even ajax doesn't cut it. I'd like to see more of this clear separation of layers. The asp.net code behind model doesn't give you the ability to cut with clean lines where ui starts and the last layer stopped. throwing cab and things like ioc into the picture just complicate things. I'm all for the service model. I think Flash is there but they never got it right for the developer. It wasn't until flex builder that they released something with intellisense. Flex is nice but it just adds more overhead and is an abstraction for people who aren't core coders. ..."
Tuesday, April 10, 2007
SQL Server BLOB datatypes going away
This is something to be aware of:
It looks like the SQL Server datatypes TEXT, NTEXT, IMAGE are going away sometime in the future as noted in SQL server Books online: "ntext, text, and image (Transact-SQL)".
So for any new tables that need to store large data fields, M$ is suggesting to use VARCHAR(MAX), NVARCHAR(MAX), and VARBINARY(MAX).
It looks like the SQL Server datatypes TEXT, NTEXT, IMAGE are going away sometime in the future as noted in SQL server Books online: "ntext, text, and image (Transact-SQL)".
So for any new tables that need to store large data fields, M$ is suggesting to use VARCHAR(MAX), NVARCHAR(MAX), and VARBINARY(MAX).
Subscribe to:
Posts (Atom)