Monthly Archives: August 2008

Visual Studio 2008 Service Pack 1 Expected Next Week

According to the MSDN Subscriptions home page, we should expect Visual Studio 2008 Service Pack 1 availability after August 11, 2008.

Microsoft Releases SQL Server 2008



SQL Server 2008 is finally out and comes with the Entity Framework, which means that SP1 for Visual Studio 2008 and the .NET Framework is almost out.

Serialized In-Process ASP.NET Session State Store

 


ASP.NET provides out of the box three session state stores:
















Provider Description
InProc

Session state is stored in the ASP.NET cache.

SQLServer

Session state is stored out-of-process in an SQL Server database.

StateServer

Session state is stored out-of-process in an ASP.NET state service.


Because with SQLServer and StateServer providers the state must cross the AppDomain boundary it needs to be serialized when stored and deserialized when loaded. Because the state needs to be loaded and stored on each request, it is only available from the PostAcquireRequestState to the ReleaseRequestState events. And, because of serialization and deserialization, all objects stored must be serializable any reference held to one of the session state items won’t be to the same item after being deserialized.


On the other hand, with the InProc provider, the state will never be serialized or deserialized, which means that objects don’t need to be serializable and any reference to an item in the state is always a reference to the item in the state even before the PostAcquireRequestState event and after the ReleaseRequestState event.


In practice, developers use the InProc provider and applications are deployed to production using the SQLServer provider. This usually leads to application errors, like storing non serializable objects in the state, that are only uncovered in production. That’s why I’ve written a serializable in-process session state store. You can find the sources here.

C# And Visual Basic Generate Different Expression Trees


(This was pointed out to me by Frans Bouma and explained by Jon Skeet)


Imagine you have this set of classes:

public class A
{
public virtual string P
{
get { return "A"; }
}
}

public class B : A
{
}

public class C : B
{
public override string P
{
get { return "C"; }
}
}


And this class:

public static class Reporter
{
public static void Report<T>(T target, Expression<Func<T, string>> expression)
{
Console.WriteLine("Expression: {0}", expression);
Console.WriteLine("\tDeclaring Type: {0}", ((expression as LambdaExpression).Body as MemberExpression).Member.DeclaringType);
Console.WriteLine("\tInvocation Result: {0} for {1}", expression.Compile().Invoke(target), target.GetType());
Console.WriteLine();
}
}

The above class writes to the console the lambda expression, the declaring type of the property and the value of the property for the target and the target’s type.


Now, let’s look at what happens when used from this C# code:

Reporter.Report(new C(), (A a) => a.P);
Reporter.Report(new C(), (B b) => b.P);
Reporter.Report(new C(), (C c) => c.P);

Reporter.Report(new B(), (A a) => a.P);
Reporter.Report(new B(), (B b) => b.P);

Reporter.Report(new A(), (A a) => a.P);


The output will be:

Expression: a => a.P
Declaring Type: A
Invocation Result: C for C

Expression: b => b.P
Declaring Type: A
Invocation Result: C for C

Expression: c => c.P
Declaring Type: A
Invocation Result: C for C

Expression: a => a.P
Declaring Type: A
Invocation Result: A for B

Expression: b => b.P
Declaring Type: A
Invocation Result: A for B

Expression: a => a.P
Declaring Type: A
Invocation Result: A for A


On the other hand, if used from the equivalent Visual Basic code:

Reporter.Report(New C(), Function(a As A) a.P)
Reporter.Report(New C(), Function(b As B) b.P)
Reporter.Report(New C(), Function(c As C) c.P)

Reporter.Report(New B(), Function(a As A) a.P)
Reporter.Report(New B(), Function(b As B) b.P)

Reporter.Report(New A(), Function(a As A) a.P)


The output will be:

Expression: a => a.P
Declaring Type: A
Invocation Result: C for C

Expression: b => b.P
Declaring Type: A
Invocation Result: C for C

Expression: c => c.P
Declaring Type: C
Invocation Result: C for C

Expression: a => a.P
Declaring Type: A
Invocation Result: A for B

Expression: b => b.P
Declaring Type: A
Invocation Result: A for B

Expression: a => a.P
Declaring Type: A
Invocation Result: A for A


Why the differences? It’s because of each language’s specification and compiler:



  • The C# compiler will issue a virtual call to the virtual member in the class where it’s declared as virtual.

  • The Visual Basic compiler will issue a virtual call to the overriding member on the class that overrides it.