Friday, October 19, 2007

Online MachineKey Generator

Submit this story to DotNetKicks

Found this today, when creating MachineKey for a web farm on ASP.NET:
http://www.orcsweb.com/articles/aspnetmachinekey.aspx

Will save you some time when creating webapps that run in a web farm environment, and it does work.

Session_End and SessionState mode="SQLServer"

Submit this story to DotNetKicks

A webapp I'm working on now needs to keep track of the number of logged in users at any time. We used to rely on Session_End for deleting sessions. However, this turned out to work only if the SessionState mode was set to InProc.  For other scenarios, Session_End is simply not called. Ever.

So we programmed a background service that polls the database now and then, and deletes from our table the rows that don't exist in the AspState-database (AspState obviously keeps track of timed out sessions and removes them from the table). Clean and simple solution.

Tuesday, October 16, 2007

Using using

Submit this story to DotNetKicks

When writing code that makes use of alot of unmanaged memory resources, all objects should be disposed of before exiting methods and such. The keyword using is often forgotten, and also sometimes seen as messy. example:

using (Bitmap bitmap1 = new Bitmap(100, 100))
{
Console.WriteLine("Width:{0}, Height:{1}", bitmap1.Width, bitmap1.Height);
}

should basically be the same as

Bitmap bitmap1 = new Bitmap(100, 100)
Console.WriteLine("Width:{0}, Height:{1}", bitmap1.Width, bitmap1.Height);
bitmap1.Dispose();

shouldn't it?
No, not really... The using-clause does a little more than this. Actually, my using-example is equivalent to the following block of code.

Bitmap bitmap1 = new Bitmap(100, 100);
try
{
Console.WriteLine("Width:{0}, Height:{1}", bitmap1.Width, Bitmap1.Height);
}
finally
{
if (bitmap1 != null)
{
bitmap1.Dispose();
}
}
Console.ReadLine();


Imagine writing nested using-clauses, and try writing the expanded code for that :)
Using using really cleans up your code, and your memory. So use it!


Edit: Oh, as Stian commented. Using should be used for all objects that implements the IDisposable interface. But remember - if you plan on implementing this interface on your own classes, you need to make sure it fully implements all the necessary methods for cleaning up. Otherwise it will all be to waste :)

Monday, October 15, 2007

LINQ and Stored Procedures

Submit this story to DotNetKicks

Just starting off with LINQ, I've already found a strange behavior. In my SP, I have to select some blank columns, that used to exist in the table. Now, they're only there to stop old apps from crashing:

Select '' as inst from footable

This made Linq return only a cryptic error message:
System.FormatException: String must be exactly one character long.
at System.Data.Linq.DBConvert.ChangeType(Object obj, Type type)


But when I replaced the '' in the SP with a ' ' (that is, added a blank space) - everything works. So it looks as if Linq is trying to type every column in the result, and if it's empty, it dies.

Oh, and if you have a variable named @@foo in your script - I don't know why it was called @@foo, it just was - Linq will not be able to run it! It presumes that all variables are named @foo, which probably is the correct SQL, by the way. But - the error message, again, is not so helpful: The procedure expects the parameter "foo", which was not provided.