Run Dotfuscator CE in Visual Studio 2012 in Building

Unfortunately my approach is not perfect in that it still opens the GUI of Dotfuscator that requires a manual clicking. It is good for me though since I only run it during building in Release mode.

Here is how I run the command during building:

Find the location of Dotfuscator CE which is included in VS2012 by going to Tools -> External Tools.

Copy the path from the Command box.

3-29-2013 2-25-27 PM

Open the property of your project and go to Build Events.

3-29-2013 2-27-17 PM

Click on Edit Post-build and enter the following

if “$(ConfigurationName)” == “Release” (“%The path copied from the Command box%” /in:”$(TargetPath)” /out:”$(TargetDir)\Dotfuscator”
copy /Y “$(TargetDir)\Dotfuscator\$(TargetFileName)” “$(TargetPath)” )

What these lines do is

1. When the project is being built in Release mode, run the Dotfuscator.

2. Output the altered DLL into a folder named Dotfuscator under your target path (Normally the bin/release folder).

3. Copy this altered DLL to the folder where regular DLL resides and overwrite it.

Use System.Windows.Forms.WebBrowser in a ASP.NET web application

There are certain scenarios where you would need to use some windows control in your web application server side. Taking a snapshot of a URL/web page is an example where you would need to use Windows.Forms.WebBrowser control to load up a URL/HTML and save the page to a Bitmap. However you may receive an error saying: ActiveX control ‘8856f961-340a-11d0-a96b-00c04fd705a2’ cannot be instantiated because the current thread is not in a single-threaded apartment.

This is because by default ASP.NET web application runs in Multi-Thread Apartment (MTA) mode, but WebBrowser control object is required to run in Single-Threaded Apartment (STA) mode as it does not handle its own threads synchronization, thus not thread safe.

Adding AspCompat=”true” to the page directive will force ASP.NET request thread to be in STA mode thus share a single thread with the COM object, e.g. WebBorwser control, it creates, and effectively resolve our issue:

<%@ Page AspCompat="true" Language="C#" %>

These links explain the concepts better than I do:
COM Component Compatibility
How to use System.Windows.Forms.WebBrowser in a web app?
What does AspCompat=”true” mean and when should I use it?

Which Port is a SQL Server Named Instance Using

I guess when dealing with firewalls you have to have a clear understanding of which port your application is using.

Today I took it for granted that a SQL Server named instance such as \SQLEXPRESS is sharing the same 1433 port with the default SQL Server instance. Later after I found out it didn’t work, it finally occurred to me that 2 processes cannot listen to the same port.

Anyways, I googled and found out that named instances are using dynamic port by default, which is assigned by SQL Browser who is listening to 1434. You can also view the assigned dynamic port via SQL Server Configuration Manager -> Select your instance -> Double click on TCP/IP -> Click on IP Addresses tab,  at the bottom of the page  you will see a dynamic port. And below it you can assign a static port, which I did and solved my problem.

3-18-2013 6-33-55 PM



Port number of SQL Server

User Already Exists in the Current Database – Orphaned Users in SQL Server

Today when moving databases via backup and restoring, I have had an issue of re-creating User Mappings on new databases. An error “User Already Exists in the Current Database” prevents me from adding any mapping to existing databases. Below is a solution:

The following script shows the existing users of the selected database
EXEC sp_change_users_login ‘Report’

The following script fix the user if you already have the login created.
EXEC sp_change_users_login ‘Auto_Fix’, ‘%user_name%’

This allow you to create a new login for the existing user.
EXEC sp_change_users_login ‘Auto_Fix’, ‘%user_name%’, ‘%login%, ‘%password%’


User Already Exists in the Current Database – SQL Server

Session state is not available in this context

Who would know that HttpApplication.Session returns an null exception when the session is null, whereas HttpContext.Current.Session returns null when the session is null?

I have this ASP.NET MVC application that checks session status in Application_AcquireRequestState handler. If a session is a newly created session and a cookie containing session id has been included in the request header, the codes sign the user out (because the user’s session has expired).

The problem is that before checking whether the user’s session is a new session, I need to make sure that the session is not null. When doing this I first attempted using this.Session which is originally from base class HttpApplication.

 if (Session != null && Session.IsNewSession)

However, when later I deployed it to client’s environment I noticed that there were many Session state is not available in this context error thrown out. I also noticed an interesting thing – when Debug mode is on, the error is not being thrown out. Which is partly why I didn’t encounter this when development. However, this behavior seems not consistent – Today when I tested the same piece of code in a different environment, the same exception got thrown out only when in Debug mode.

So this issue can occur depends on the environment. What’s going on and what do we do?

The answer is – use HttpContext.Current.Session instead.

It turns out that the implementation of HttpApplication.Session will throw an exception when the session is null. So basically you cannot even check its value like what I did. According to an answer from Stackoverflow, the implementation is something like this:

[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public HttpSessionState Session
HttpSessionState session = null;

if (this._session != null)
session = this._session;
else if (this._context != null)
session = this._context.Session;

if (session == null)
throw new HttpException(SR.GetString("Session_not_available"));

return session;