Thursday, 27 January 2011

Accessing GAC DLL's in Visual Studio 2010 "Find References"

Or better titled I've just registered my DLL in the GAC, why does it not appear in Visual Studio's "Find Reference" dialog.

It's probably the first thing people notice after they've started using the GAC for the first time. You've managed to register your DLL and you've confirmed it is in the GAC, but it just won't appear in Visual Studio's "Add Reference" dialog box.   As it turns out, for what were probably good reasons, the visual studio team decided to use the registery to hold the list of DLLs that appear in the "Add Reference" dialog box.

You have two options, the first is to manually edit the registry - adding your DLL to the following registery keys:

  • [HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\.NETFramework\AssemblyFolders]
  • [HKEY_CURRENT_USER\SOFTWARE\Wow6432Node\Microsoft\.NETFramework\AssemblyFolders]

Another option is to install the Visual Studio extension MUSE.VSExtensions, which adds an extra option to the context sensitive menu when you right mouse click on "References".  The exta option is "Add GAC Reference" which pops up a new dialog box listing all the items currently held in the GAC - no messing around in the registery each time you add something new to the GAC.  It is worth noting that the search is case sensitive.

See Adding a DLL to the GAC in .NET 4.0 for more information on adding DLL's to the GAC in .NET 4.0 and differences between this and earlier versions of the framework.

Adding a DLL to the GAC in .NET 4.0

GACUTIL.EXE is no longer supplied / installed along with Visual Studio, instead it has been moved into the Windows SDK v7.1.   So to install a DLL into the GAC for .NET 4.0 (or anything after v1.1 I believe) you must first download and install the SDK.  Once you have done this you must locate the relevant version of GACUTIL from the four possible locations:

  • Program Files\Microsoft SDKs\Windows\v7.1\Bin\
  • Program Files\Microsoft SDKs\Windows\v7.1\Bin\x64
  • Program Files\Microsoft SDKs\Windows\v7.1\Bin\NETFX 4.0 Tools
  • Program Files\Microsoft SDKs\Windows\v7.1\Bin\NETFX 4.0 Tools\x64

The first two are for registering DLLs to be used in .NET 3.5 or earlier, with the first being for x86 and the second for x64 versions.

The second two are for registering DLLs to be used in .NET 4.0, again with the first being for x86 and the second for x64 versions.

With the release of .NET 4.0 the framework now has two GACs, this stackoverflow post explains why.

Tuesday, 25 January 2011

Javascript: Type and Value Comparison

A common problem when doing value comparison inside of Javascript is the automatic type conversion that happens for you, this means all the following statements resolve to "true".

  • 1 == 1;
  • 1 == '1';
  • 1 == "1";

As a little bonus snippet in a post by Steve Wellens he provides an answer to the problem, the triple equals ("===") and it's corresponding not equal ("!==").  These comparison operators perform a type check as well as a value check.  This means that a string value will nolonger equal it's corresponding numeric value as the type check will return false.  A useful bit of functionality to have.

Sunday, 23 January 2011

SQL Server Licensing

As part of my current project I’ve spent some time over the past couple of months trying to determine the best (cheapest) SQL Server configuration to support web servers running in a virtualised environment. As a quick disclaimer, the following are my thoughts on the subject and should be used as guidance for further research only!

Firstly you need to figure out whether you are going to license using the “per user” or “per processor” model. For most typical configurations a “break even” point can be determined when it becomes cheaper to switch to the per processor licensing model instead of the per user model. It is important however to plan for future growth as it can be very expensive to try and switch from one model to another once a system has been deployed. It is not possible to convert SQL “user” CALs into a processor license.

If you are developing a web application that will be exposed via the internet to external customers then it would probably make sense to use the web edition, which only comes with “per processor” licensing. The Microsoft definition of what is a user is critical when selecting the web edition as this can not be used for intranet based applications used by company employees. See the licensing section for more information.

To aid in the selection of the correct edition Microsoft have put together the following SQL Server 2008 Comparison Table

It is also worth considering the environment that is going to host the SQL Server instance? If you intend to host SQL Server in a virtualised environment things can quickly become confusing and potentially (yet again) expensive.  Even the Microsoft FAQ on SQL Licensing appears to contradict itself – the answer to the question “What exactly is a processor license and how does it work?” appears to state that you only need to buy a license per physical processor even for virtualised environments.  However, the answer to a later question “How do I license SQL Server 2008 for my virtual environments?” then contradicts this by giving a more detailed answer highlighting that in a virtualised environment the definition of the “per processor” model changes depending upon which edition of SQL Server you have purchased.

You then need to dig around the Microsoft site a little more to investigate the licensing model a little more – the Licensing Quick Reference PDF provides a lot more information from page 3 onwards.  I won’t duplicate the information held in that document, because if nothing else it may be updated over time but it is worth noting the differences in Data Centre / Enterprise editions and Standard edition. For the standard edition the document then moves on to describe the formula that should be used to determine the number of per processor licenses that should be purchased for each virtualised instance of SQL Server. At the time of writing you divide the number of virtual processors with the number of cores on the physical processor (rounding up) to get the number of SQL licenses needed. This does mean that if you have a 4 core physical CPU and you expose each core as a virtual CPU to the instance of SQL Server, you still only need one “per proc” license. A common misconception is that you must have a “per proc” license for each virtualised CPU exposed to SQL Server, but this hopefully clears up that potentially costly misunderstanding.

Resolving the HTTP/HTTPS “Document contains both secure/unsecure content” message

One of the most commonly encountered issues when developing websites that must support both HTTP and HTTPS pages is the warning that a secure page “contains both secure and unsecure content”. In a nutshell this is when a page that is being displayed via the HTTPS protocol contains one or more references to additional resources (JS/CSS/Images) using just HTTP.

The solution is easy and well documented for locally referenced resources, in that when making the reference to the required file you exclude the protocol and domain name ending up with something like “/styles/main.css”, which will quite happily call “main.css” from a “styles” folder in the root of the web application regardless of whether the containing page is being called by HTTP or HTTPS.

Note: .NET provides functionality such as ResolveUrl(“~/styles/main.css”) which should always be used in preference to hard coded paths such as “/styles/main.css”. Using ResolveUrl(…) will work regardless whether in IIS the web application has been set up as a website or a child application of the parent website. However the hardcoded path will always default to the root of the website, so if your web application has been deployed within IIS as a child application of a website you will typically see lots of 404’s as the browser is unable to locate the resources using the URL provided.

Typically externally referenced resources have proved more troublesome. I’ve seen quick and dirty solutions that always call external references via HTTPS. This gets around the warning when viewing the calling page in HTTPS, but introduces additional processing/overheads when viewing the referencing page in HTTP. More typically the URL for the required external resource is passed to a helper functional which determines which protocol to add depending upon whether the containing page is HTTP or HTTPS.

But it would appear that all the solutions for resolving the protocol for externally referenced resources were over-engineered! Under the section A Better Solution, Dave Ward provides a solution that is pretty much identical to the tried and tested internally referenced resource solution. Basically the RFC 3986 spec allows resources to be referenced using a protocol-less format. So instead of worrying whether you should be calling “http://cdn.domain/common-resouce.js” or “https://cdn.domain/common-resouce.js”, you can just call “//cdn.domain/common-resouce.js” and the protocol to be used for the external reference is determined by the protocol context of the containing page! Thanks for highlighting this Dave, much easier!