Friday, April 30, 2010

Stuff You (I) Should Know

The following is a list of APIs and tools which I consider the current state of the art in .NET development (though not exclusively .NET) and that I really should know a lot more about. This should pretty much be complete right now, though I have a nagging feeling I missed one.
The following list is not complete, won't be for some time and may require time travel to keep reasonably accurate - list of best practice advice/docs:
And some videos which I hope will be helpful:
Footnote: This needs reorganising into a list of tech, each with a list of links and FAQ/Quickstarts.

Monday, April 26, 2010

Wednesday, April 21, 2010

Error 107 ERR_SSL_PROTOCOL_ERROR

Needing to apply SSL to an existing website (on my Windows Server 2003 development machine) I recently opened the properties on the website (under IIS, Web Sites), clicked Directory Security and Server Certificate and chose to apply/replace the certificate from an existing site/certificate.

This did not have the required effect, when requesting the page in the browser:
  • Using HTTP://
    The page must be viewed over a secure channel
  • Using HTTPS://
    Error 107 (net::ERR_SSL_PROTOCOL_ERROR): Unknown error IIS
  • And occasionally
    Bad Request (Invalid Hostname)
Essentially, this is because I was trying to reuse an existing certificate on a web site that had a different name. The solution is basically to replace the certificate with a valid certificate either from a trusted Certificate Authority (CA) or to use the SelfSSL.exe.

All of this is documented here, the first link being how to use the SelfSSL to get the job done on a development box:
It is important to note that when applying SSL certificates, especially via SelfSSL, that you open the site's Properties dialog:
  • Click Web Site tab
  • Click Advanced button
  • Under Multiple SSL identifies for this Web site select the site's IP row
  • Click Edit button
  • From the IP address drop down menu select the IP address of that site
  • Click Ok, Ok, Ok
This associates the SSL certificate with the site's IP and ensures that there are no conflicts. Click the Help button in this window for more information.

Wednesday, April 14, 2010

Tuesday, April 06, 2010

Debugging SQL And The Fun Attached To That

Having come across this problem in Commerce Server 2002 recently:
Server Error in '/' Application.

Source:Microsoft OLE DB Provider for SQL ServerDescription:Incorrect syntax near '('.Source:Microsoft OLE DB Provider for SQL ServerDescription:Invalid object name 'MSCS_CatalogScratch.dbo.Catalog__Query__Results__for_spid__304'.Source:Microsoft OLE DB Provider for SQL ServerDescription:Invalid object name 'MSCS_CatalogScratch.dbo.Catalog__Query__Results__for_spid__304'.
I discovered that the stored procedure failing was called "ctlg_GetResults_for_SingleCatalog" but how to work out where?

This page came in useful: http://support.microsoft.com/kb/316549

Basically, it's stepping through SQL Stored Procedures in Visual Studio. The account you're accessing the DB requires execute permissions, etc, of course, but from there you should be able to step through the process just like code. Well, almost.

Friday, April 02, 2010

Always Check IIS

This is a lesson we all learn over and over again, I believe.

Recently trying to get the files in the MVC web app's directory '/Scripts/*' to be accessed I was constantly frustrated in not being able to load simple .js files, etc, in the browser page.

In this case, it was because Default Web Site in IIS already has a Scripts virtual directory setup. This, of course, does not behave as a normal subdirectory would and was stopping access to the files within it by the browser. This is expected, unless you've forgotten that fact because you've been working on WinSrv2k3 for too long...

Tuesday, March 23, 2010

Cannot Load ...global.asax

Ok, this may not cover every situation where the global.asax file is not able to load, but one thing to check, if annoying and persistent, runtime, global.asax errors are popping up is the build output directory. I had mine set to \bin\Development where it should have been \bin

Nuff said.

Wednesday, March 10, 2010

Easy Reflection

Reflection in .NET, as in Java, is pretty easy (and fun.) It is worth taking an amble down the many classes and tutorials in, and relating to, the System.Reflection namespace.

It is vitally important not to over-use reflection and to be aware that any reflection code probably has a better, non-reflection API somewhere to get the job done.

That said, there are easy mechanisms to get some basic reflection, like calling an empty argument constructor:
Hopefully, I'll flesh this out with some examples and more links to other places, but for now, this will have to do.

Hashing Passwords For Commerce Server

This post generally applies to Commerce Server 2007.

Initially labouring under the impression that CS2k7 used SHA1 or SHA256 (depending on which version is installed)...
I then learnt that we were, in fact, using MD5...
For those who want to know, MD5 is old, SHAn is newer. I believe CNG is the newest mechanism, but is not yet used by CS...

Tuesday, March 09, 2010

The Right Or Wrong Level Of Abstraction

I just have to post this:
I'm doing some encryption-involved work atm and this is particularly relevant.

It's also relevant because I often find myself, usually at the beginning of a project (like, right now), asking me, "What level should I be working and is there an API to do this for me?"

Usually, the answers are, "Don't know" and "No", in that order. Which is annoying but also called, "Life."

Anyway, read that article, the articles linked from it. It be good. Here it is again...

Drive Space Disappearing Captain!

Being very mystified recently that my drive apparently kept filling up, no matter how much space I freed by file deletions, I went on the hunt for any directory et al that might be growing, uncontrolled.

My journey took me to the directory:
  • C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data
Where I found a "nnn_log.ldf" file which was over 50% of my total drive capacity.

Upon opening "SQL Server Management Studio" I found the database in question, opened the Properties dialog and clicked on Files, in the left-hand list of panels. There, under the "Autogrowth" field of the "Database files:" display was a happily chugging "By 10 percent, unrestricted growth".

Simply changing the restricted size to something sensible did not work as hitting Ok threw an error dialog up.

A quick Googling later and this showed up:
So, to get rid of an overly large log file attached to the DB, the process is thus:
  • Take DB offline
  • Detach DB (Untick "Keep Full Text Catalogs", if that is the problem part)
  • Delete the overly large file
  • Re-attach DB, removing the LDF file from the "database details" gridview
  • Click "No" to skip re-attaching the log files/full text catalog (you've deleted it)
  • Open the DB's "Properties -> Files" dialog
  • Under "Autogrowth" click "..."
  • Untick "Enable Autogrowth"
  • Click Ok
For your DB, you should now have removed any overly large files and stopped any existing files from getting any bigger. Of course, if you do need them to grow, just keep an eye on the Autogrowth properties.

Thursday, March 04, 2010

T-SQL InformationSchema.Columns

A handy bit of T-SQL was passed to me today (by @jaminadey) which returns all the fields in a given table which are non-null...
  • SELECT * FROM INFORMATION_SCHEMA.Columns where TABLE_NAME = '' and is_nullable = 'NO'
Of course, you'll see there are lots of other fields which can be inspected, so play around and introspect that DB!

Friday, February 26, 2010

Good Exception Rules - By Hanselman

Just wanted to stick this up as it's a good line of thought for handling exceptions.
There are, of course, many ideas on this, but 'from the horses mouth' often works well :)

Thursday, February 25, 2010

Sync Google Contacts To Your iPhone Contacts

Ok, this is fairly straightforward if you've already set up your Mail/etc on your iPhone to use Google Mail as an Exchange account.

If not, you've got some backing up to do, but it's ok - this blog post documents the whole process and makes it fairly painless:
One warning it makes: You will lose all your contacts and calendar entries if you simply sync with Google with importing your existing contacts into Google first!

Tuesday, February 23, 2010

Shared Heap Exhausted Or Damaged

I recently had an issue which caused a lot of error messages in the Control Panel -> Admin Tools -> Event Viewer -> Application log. While I've not fixed this issue on my machine specifically, I thought this was worth recording. The error I'm getting is:

The description for Event ID ( 1 ) in Source ( nview_info ) cannot be found. The local computer may not have the necessary registry information or message DLL files to display messages from a remote computer. You may be able to use the /AUXSOURCE= flag to retrieve this description; see Help and Support for details. The following information is part of the event: NVIEW : devenv: shared heap exhausted or damaged

Some may find the following link useful in this situation.

Sunday, February 21, 2010

The Project Is Not Supported By This Installation

Today I got this error dialog when trying to open a solution "The project file '....csproj' cannot be opened. The project type is not supported by this installation." Visual Studio went on to display all the projects in the solution it could, except the website project.

Googling this returned this helpful post:
Indeed, reading the 5th post on that thread lead me to deleting the ProjectTypeGuids element from within the first PropertyGroup element. Hitting Reload Project in Solution Explorer showed the project correctly.

Friday, February 19, 2010

Different Build Modes

It is often useful to have different Visual Studio build modes in order to make better/more appropriate compilations of your projects for different environments, such as Development (your local dev machine), Staging (internal/client testing) and Production (final, fully live environment.)

To this end setting up VS properly is important and actually fairly simple. The process I use is thus:
  • Create a \Configuration directory in the root of your web project
  • Create \[build mode] directories for each build mode inside the \Configuration directory
  • Open the web project properties panel, click "Build Events" and paste this into the first multi-line text box, titled "Pre-build event command line":
mkdir $(ProjectDir)Configuration\Current
copy $(ProjectDir)Configuration\$(ConfigurationName)\*.config $(ProjectDir)Configuration\Current
  • Create empty ConnectionStrings.config and AppSettings.config files within each of \Configuration\[build mode] directories. These will need the initial empty elements for their appropriate config blocks, such as and
  • Replace the above config blocks in your web.config file with element attributes pointing to the \Configuration\Current directory:
  • Open the Solution Configurations drop down and select "Configuration Manager..." This is on the Standard toolbar and will probably show "Debug" as the default item.
  • Create the appropriate build modes under "Active solution configuration". Select New... and just create a series of new configuration names, such as Development, Staging and Production. Copy the default settings from Debug, for ease of use.
  • Using the Active solution configuration drop down, select each of the configuration modes you've just created and for each one click New... under the "Configuration" column for each project in your solution. When the New dialog opens, just create the configuration with the same name as the active solution's configuration: Development under Development and so on. Again, copy the Debug project configuration, for ease of use.
  • After closing the configuration dialog, select a configuration mode from the drop down and hit Build -> Build Solution.
At this point you may discover the following exception when building:
The command "mkdir .........." exited with code n.

This happens because one of the files you're asking copy to work upon either doesn't exist, doesn't have permissions to access or the directory your command refers to simply isn't there.

Problem: The directory will be missing if your Visual Studio build mode is currently in, for example, Release and you've only got \Configuration directories for, eg, Development and Staging. The problem copy is hitting is that the \Release directory doesn't exist.

Solution: Change the current build mode to Staging or Development and all should be well.

Wednesday, February 10, 2010

Not Validating A Complete Model

I am currently trying to edit a single model object through a series of views, using the MVC 2 RC 2 framework, found here:
I discovered that while using the default server-side validation that my model could not be properly validated because some of the model properties were not fully populated, or had incorrect values when, for example, the first view's form was submitted. The incorrect/null/etc values were fine for me because the process had not reached the point they would be populated, but the ModelState.IsValid still returned false, of course. This would be fine, except I needed it to be true, fitting to my standards of what was 'true' about the state of 'my model'.

The method I came up with follows, but I will say that it's not perfect and will almost certainly be superseded by something better in the future. Here it is:

///
/// Used simply to ensure that validation has occured for the model properties that were rendered on
/// the current view.
///
/// The list of form field names to check for errors. Eg: the FormCollection object
/// True if none of the ModelState values matching the whitelist keys have errors attached, else false.
private bool IsModelStateFieldsValidated(IEnumerable whiteList)
{
foreach (string key in whiteList)
if (ModelState[key].Errors.Count > 0)
return false;

return true;
}

If the action method takes a 'FormCollection collection' parameter then that can simply be passed into the IsModelStateFieldsValidated method directly.

The method above just very simply checks the field names which were on the page (which, of course, may not be every property in the model class) and checks that there were no exceptions found in the ModelState value collection - for those property names only. Any other exceptions will be ignored, because they are not pertinent to the current view.