Friday, August 24, 2012

WOW64 – WebApps

In a recent project I had to migrate some complex (this is relative) WebApps to a 64bits Windows Server 2008 R2. I'm not a "SysAdmin" (not close), but... work is work! The main issue is that we're talking about 32 bits ASP.NET 1.1 applications that use the WindowsRegistry. When you merge a registry file that was created for WS2000 with the registry of a WS2008 64bits server (say MyKey.reg), the keys are actually created in the "normal" hive.

However, a 32 bits ASP.NET application will not find this key (e.g.: Using Registry.LocalMachine.OpenSubKey). Due to the WOW64 (WindowsOnWindows64) feature, a Registry Redirection (more info below) takes place and it will look for the key in a special hive: Wow6432Node. Meaning, the key you want must exist in this hive:

This problem also comes in "different flavors": %SystemRoot%\\SysWOW64 instead of the usual   %SystemRoot%\\System32, ODBC drivers (%SystemRoot%\\SysWOW64\\odbcad32.exe), OLEDB... on and on and on...

@MSDN: "The registry redirector isolates 32-bit and 64-bit applications by providing separate logical views of certain portions of the registry on WOW64. The registry redirector intercepts 32-bit and 64-bit registry calls to their respective logical registry views and maps them to the corresponding physical registry location." More info HERE and HERE;

Wednesday, May 30, 2012

Query Execution Plan

First of all: Ignore the DB Modeling, DB Design mistakes and so on. This post relates to a DB made by a company a decade ago and I will not question the design choices.

So, the topic: migrating from SQLServer 2000 to SQLServer 2008. This is NOT a simple and direct upgrade. There are several issues that will arise when doing this process.

   - Is de DB mirrored?
   - Replicated?
   - Indexes?
   - Users?
   - Filegroups?
   - DTS?
   - How big is de DB?
   - Can it be offline while migrating?

On and on and on...
But after all this has been accounted for and after the migration process was successful, there is still a lot of work to do! Focusing on the issue, consider the following table:

And lets populate the table:

(click to enlarge)

Now, the applications were doing something like this:

(click to enlarge)

In SQLServer 2000, this worked because it's granted that when the field is "dateField" then the Value IS a date (or datetime). However, after the migration to SQLServer 2008, these kind of queries stopped working. The exact same query with the exact same "data" in the tables didn't work. Why?

Finding the issue... Well, the SQL Server 2008 query optimizer actually changed the execution plan and it does the second WHERE clause first:

(click to enlarge)

Meaning, it's trying to convert all Values from the table and then filtering with the LIKE clause. So, It's trying to convert non-datetimes (that actually exist in the table) and "bum"! The "fast" solution was to replace the LIKE with an EQUAL clause:

(click to enlarge)

This way, the optimizer will not change the query execution plan:

(click to enlarge)

And now it works:

My advice, however, was for them to review the database. In future versions of the SQLServer, similar problemas can arise.

Wednesday, May 9, 2012


Writing code dynamically can be very useful in some scenarios. In a recent WF project I needed to build a workflow in code and save it to XAML, executing this saved workflow definition latter on. But there's a problem: Lambda expressions do not serialize to XAML. This is where the ExpressionServices "utility" comes in. Using the "Person" type for this example:

(click to enlarge)

Just two properties. Now, keeping in mind the above statement about Lambda Expressions, when generating the Workflow we need to use a method from the ExpressionServices type to convert the Lambda Expressions to Operator Activities:

(click to enlarge)

Finally, lets generate the XAML using XamlServices:

(click to enlarge)

That's it!
Final note: According to MSDN, "this conversion is supported for a limited set of lambda expressions only". I couldn't, however, find a reference to what set of Lambda Expressions are supported.

Edit: Yes, I usually don't place comments on the code I share here. That's because it would make the code less obvious. It's a simple demo.

Tuesday, April 3, 2012

Hello Android!

I'm a Microsoft technologies fan. Period. There are lots of "non-Microsoft" technologies (or programming languages) that I like (I love PHP and MySQL! I've been using them for a decade!), but my main core (personal and professional) is Microsoft. My passion!

So, as a natural .NET fan, my mobile choice was, obviously, the .NET Compact Framework. This was like 2007 or so.

However, programming languages and technologies are just a means to an end.
Hey... I just bought an Android phone! So, lets Android!

Thursday, March 22, 2012

C# Expressions – Workflow Foundation in .NET 4.5

Basically, when you see a new version of the .NET Framework coming out, you think of it as “an improvement” to the previous version. With Workflow Foundation (WF) this was “brutally” different in the jump from WF3 to WF4. In this case, the version numbers just mean the version of the framework rather than a version of the WF itself (In fact, the first version of the WF was in .NET 3.0, with updates for .NET 3.5). For example, in WF4 there’s no longer a WorkflowRuntime Type and the base Activity type has nothing to do with the original one (It even resides in a totally different assembly (and namespace!): From System.Workflow.ComponentModel.dll to System.Activities.dll).

Now, having that in mind, you should look to the WF4 as a totally new approach. Consider it a new product. And looking at it as a new product, I was quite skeptical. The new XAML and Expression Based feel was not “my thing” at a first look, but I gave it a shot and I now love it.

However, one of the things that most annoyed me was the fact that all Expressions had to be written in Visual Basic, even if your entire solution was in C#. Not nice!

Now, in WF .NET 4.5… That’s about to change! You can now write your expressions using the language that you prefer! If you’re a C# fan and use C# projects, you can now write WF expressions in C#. A fully functional C# expression editor is provided with capabilities such as grammar highlighting and Intellisence.

LIKE IT? LOVE IT! Finally...

Here’s what’s new for WF in the .NET 4.5:

Monday, January 30, 2012

nop – No Operation

If you ever looked at your code compiled in DebugMode Versus ReleaseMode you most certainly saw a few differences. One of those differences is the higher presence of the nop IL instruction in the DebugMode. What does this instruction do? Well, nothing "meaningful"...
Take a look at the following simple example:

(click to enlarge)

As you can see, I have a breakpoint set on the bracket. If we compile this in DebugMode you can see that the compiler inserted a nop instruction right after the anonymous methods "signature":

(click to enlarge)

And in release mode, it didn't:

(click to enlarge)

The compiler inserts nop instructions as placeholders for the non-executing lines of code (since non-executing lines of code are not translated into IL instructions). This is the reason why we can set breakpoints on brackets. The nop instruction does not perform any "meaningful" work yet it can consume a processing cycle.