Visual Studio 2008 Renaming Bug

0 comments

I'm a big fan of the renaming feature in visual studio. You can right click on any variable in your code and select Refactor -> Rename. Then rename the variable once and all other references are updated.


Except I like to inject code right into my .aspx pages instead of the code behind files sometimes. The syntax for that is:

<% Response.Write(SOME C# CODE); %>

The rename functionality won't find these references in the .aspx files. Instead I get a compile error and can then rename the remaining references easily enough.

It seems like this should be fixable. The fix might hurt the performance of the renaming feature a little bit but I think it would be worth it. Does anyone know if this is already fixed in VS 2010?

SSL Hell - Multiple web servers in a cluster

0 comments

I was following the instructions that GoDaddy provided to install an new SSL cert on IIS6. These instructions cover how to get up and running on one web server, but they don't detail how to export the cert to other web servers. The site I was working on had several in a cluster.


So I thought I'd detail what worked for me. The trick is to export the cert to a .pfx file. Note: the steps below are intended to be followed after you've already set up the certificate on the first web server.

To do this, first start with the certifcates snap-in all set up. GoDaddy's instructions for this are pasted below:

  • Click the Start menu and click Run.... Type mmc in the Run window and click OK to start the Microsoft Management Console (MMC).
  • In the Management Console, select File then Add/Remove Snap In.
  • In the Add or Remove Snap-ins dialog, click the Add button and then select Certificates.
  • Choose Computer Account then click Next.
  • Choose Local Computer, then click Finish.
  • Close the Add or Remove Snap-ins dialog and click OK to return to the main MMC window.

Now you're ready to export:
  • Browse to Personal -> Certificates.
  • Right click the certificate you need to export in the right hand window and select "All Tasks" -> Export ...
  • Click Next on the first meaningless page of the wizard
  • Select "Yes, export the private key" and click Next
  • Leave the default selected (just "enable strong protection" checked) and click Next
  • type in some password that you won't forget and click Next
  • Browse to a location to export and click Next
  • Click Finish
It is important that you do specify a password for everything to go smoothly. Now for the importing:
  • Copy the .pfx file to an additional web server
  • get the certificates snap-in up on the new server (same as instructions above)
  • Under Personal -> Certificates right click the folder and select "All Tasks" -> Import ...
  • browse to the pfx file (will need to change the "File of Type" drop down in order to see it)
  • click Next through the wizard, leaving the other defaults.
  • Don't forget to import the intermediate certificate! That is the same as the first web server.
That is it. Now in IIS you can go specify that cert. You won't be responding to a CSR (certificate signing request) like the first time. You'll just be choosing an existing certificate.

Query is fast in management studio but hangs from Console App

0 comments

I'm going to include my little narrative of how I troubleshot this issue. But first let me just get the answer out there. You need to include this at the start of your stored procedure:

set arithabort on

My troubleshooting story is also pretty concise I guess. I used SQL Server Profiler to see what my app was doing differently from management studio. The actual call was identical, but any .NET MSSQL connection calls a bunch of set commands before it does anything. The entire list I found is below:

set quoted_identifier on
set arithabort off
set numeric_roundabort off
set ansi_warnings on
set ansi_padding on
set ansi_nulls on
set concat_null_yields_null on
set cursor_close_on_commit off
set implicit_transactions off
set language us_english
set dateformat mdy
set datefirst 7
set transaction isolation level read committed

By starting with a fresh query editor window and adding in these set commands one at a time I found the culprit pretty quickly. Then I just set the property the way I want it inside the proc to override the damage the connection from .NET causes.

StackOverflow was also really helpful in two posts. One focusing on the SqlDataAdapter and another that was more generally about SqlCommand. Just go help vote up the answer that this post is consistent with. Unless you find otherwise, then let me know.

What would be nice is being able to control which set commands are issued from my app. I've found nothing on how to do this. If you find something please share!

Automated Speed Monitoring with MSSQL

0 comments

What? Why?


I'm in the web data extraction business. That means I'm often writing hundreds of thousands of rows to a table over the course of days. Well when you're dependent on a third party website for your data, a lot can go wrong over the course of days. So I started formulating my own table monitoring solutions.

At first I would just periodically do a SELECT * here and a SELECT count(*) there to get by. But when a weekend goes by and on Monday morning you're 10K rows short, you want to know if things just slowed down a little or if everything broke sometime Sunday night. My solution is to automate my little SELECTs to happen on a regular schedule and then store themselves in a log table. By comparing the latest row as I insert I can do an on the fly rate calculation as well. Sound useful? The details follow ...

How.


First I needed a log table. I made something like:

CREATE TABLE [dbo].[SpeedLog](
[Count] [int] NULL,
[Timestamp] [datetime] NULL,
[PerHour] [int] NULL,
[PerDay] [int] NULL
) ON [PRIMARY]

Next I made a view that produces a new row to insert into this table. I find it handy to have this logic isolated in its own view rather than having an enormous insert statement,

CREATE VIEW [dbo].[vw_Status]
as
SELECT
count(*) as [Count]
, CURRENT_TIMESTAMP as [Timestamp]
, (count(*)-MAX(lRow.UrlSnapShotsCount))/DATEDIFF(hour, MAX(lRow.[Timestamp]), CURRENT_TIMESTAMP) [PerHour]
, (count(*)-MAX(lRow.UrlSnapShotsCount))/DATEDIFF(hour, MAX(lRow.[Timestamp]), CURRENT_TIMESTAMP)*24 [PerDay]
FROM MyGrowingTable u
LEFT JOIN
(SELECT a.[Count], b.[Timestamp], a.PerHour, a.PerDay FROM SpeedLog a
INNER JOIN (SELECT MAX([Timestamp]) [Timestamp] FROM SpeedLog) b ON a.[Timestamp] = b.[Timestamp]) lRow
ON 1=1

That is a bit much to throw at anyone all at once. To abstract it a little in pseudo code I'm saying:

SELECT CurrentCount, (y2-y1)/(x2-x1) as PerHour, PerHour*24 as PerDay
FROM MyGrowingTable
LEFT JOIN (the most recently inserted row of my log table) lRow
ON 1=1

Since my lRow sub query only returns one row ever I can get away with a 1=1 ON clause and still have one row as a result. It is important to do a LEFT join to lRow so that a first row with no rate info can be inserted.

Now I just need to insert on some regular schedule. I do this via a SQL Server Agent Job. If you've never used one of those before don't worry. It is a point and click affair within Management Studio. You just have to make sure you have SQL Server Agent running as its own service. Then you can add a new job with one task within it that runs:

INSERT INTO SpeedLog
SELECT * FROM vw_Status

Since the logic is in the view, you can tweak things without having to dive into the Agent Job every time.

Where next?

count(*) is a pretty simple measure of a table. For your own purposes maybe you want to track the average number of rows where some column is null. That would be no problem. You could track as many metrics as you wanted by just adding more and more columns to the log table and your view. More complex rate analysis could be done on the entire log table as well.

As with every solution I create. It seems like a problem other people must have run into and solved already. I think I'm just bad at finding solutions. If you have something else to tackle this problem please share!

Load HttpModule! Load! HttpModules in App_Code.

1 comments

I've done HttpModules in separate assemblies and don't remember integration being too tricky. But when I was just writing one within the App_Code of the same site I was having all sorts of issues. I think I found some common gotchas related to HttpModules.

First, my module was not loading but I didn't even get an error page. I was able to get useful errors by switching the App Pool of the site to "Classic .NET AppPool" from the "DefaultAppPool". Turns out this did more than give me error messages, when all was finished I had to have this classic app pool for the module to work. Thanks to lucky abhishek for figuring out the app pool problem first.

Once I was seeing errors I first found that I had other modules in a parent site that could not load. In IIS web.config settings from parent sites are inherited by sites in any virtual sub directories. So I was loading other HttpModules that I didn't care about in this sub site. I fixed this with a quick

<remove name="NameOfModuleIDontHave" />

in the HttpModules section of the web.config in order to straighten that out.

Last I just had to figure out why I "could not load type MYTYPE". Turns out for the type I was using the namespace instead of the class name qualified with the namespace. In other words in my web.config I had

<add type="ParentNamespace.SubNamespace" name="MyClassName" />

instead of

<add type="ParentNamespace.SubNamespace.MyClassName" name="MyClassName" />

As a side note. After I got this working I found for my purposes that the name attribute doesn't mean anything. I can change it to whatever and my sample HttpModule still works.

One last thing I found was that the type attribute above is usually of the form path,assembly. When you're using a class in App_Code you can use the text "App_code" as the assembly if you want to be a little more clear. So this also works:

<add type="ParentNamespace.SubNamespace.MyClassName,App_code" name="MyClassName" />

But in my case it didn't seem to be required.

For reference, this is the skeleton HttpModule I was trying to load.

The Custom Windows Toolbar - Underrated.

1 comments

It is one of those features that never gets any hype. Much like the quick launch bar, windows allows you to add your own custom taskbar toolbars. Just

- right click the taskbar
- Toolbars -> New Toolbar
- Point to a folder and you're all set.
This video really spells out how to create a custom toolbar. Below is a shot of mine at the moment.

There is virtually no limit to the number of shortcuts. Sub folders are supported to allow for some simple grouping within a toolbar. It makes for a much better experience than the raw directory trees I usually have to think about navigating.

Only recently has it dawned on me how to use these toolbars to really save me some time. If you're a developer you may find these ideas useful if you haven't already implemented something similar.

Visual Studio

I'm always working on different solutions in visual studio. So many so that the "Start Page" no longer is that useful (even after tricking it to show me more recently opened solutions). Plus, I hate having extra tabs open. So the first thing I do after using the start page is close it. Well if you click a shortcut to a solution file no start page opens! Ha ha, finally I've thwarted that useless tab and its "developer news" that I never read. Instead I have my "vs" toolbar.

Command Prompts

All of those visual studio solutions usually have a console application or two in them. When I want to revisit these my directory navigating gets even crazier thanks to the /bin/Debug directories that are created by default. My solution is to create batch files of the form
cmd /k cd "C:\My Solutions\My Project\bin\Debug"
I talk about this idea in another post here. The end result is my "cmd" toolbar which opens up a command prompt at whatever starting directory I want.

Remote Desktops

I am constantly hoping on and off of machines for work. My rdp toolbar beats the snot out of Start -> All programs -> Accessories -> remote desktop connection. Instead I have a saved rdp for each machine one click away.

Other Ideas

NUnit
I don't do a ton of NUnit testing but I should. A little looking around shows that nunit-gui takes command line args. You could create shortcuts to your common test fixtures.

Multiple homepages
You can pass a url to the command line of most browsers. So you can have shortcuts to multiple sites in a toolbar.

I'm sure there are even more obvious uses that I've not thought of. Please share what you've come up with.

Windows Symbolic Link Equivalent - But ghetto and not at all

1 comments

Say you want to create a directory that points to another directory. For example something in your home directory that point to a long and convoluted path that you often have to go down. In Linux the answer is pretty easy, the ln command. Here is a decent forum post on using the ln command.


But I'm bound to the world of Microsoft. There must be some DOS equivalent right? Well a little Googling found me a discussion on junctions. My take away is that they aren't quite the same thing, and they aren't well documented. So instead I took an approach that I feel a little bit more comfortable with.

What I ended up settling on was creating some batch files in my home directory with contents similar to
cd "C:\Some\Long\Directory\Path"
Ghetto right? But for me the combo of
- Windows Key + R
- "cmd" + Enter
- MyBatFile Enter
is all I need. With a little tab completing it is quite fast. Before I was getting pretty fast at tab completing through my entire long directory name, so this is a definite improvement from how I was operating.

You could also make shortcuts on the desktop or in a custom toolbar off the start menu of the form
cmd /k cd "C:\My\Dir"
if you like a little point and click in your life.

Finally you could get all of your shortcuts in your PATH environment variable. Then you could jump around without having be in your home directory.

I know this isn't a very impressive tip. I guess what I'm really just conveying a more philosophical lesson. Sometimes quick and dirty is the way to go. Especially when it is saving time on something as simple as directory navigating.