Sep 23 2009

ThreadStatic gotcha with ASP.Net

Category: .NetRory Primrose @ 06:40

I had a requirement recently in a service implementation that business rules at any point throughout the execution of a service request could determine a message to return in the header of the response. Because these circumstances don’t affect the execution of the request (no exception is thrown), I needed a loosely coupled way of recording all the required messages and handling them higher up the stack.

Storing data against the executing thread was the obvious solution. I considered manually storing information in the context data of the thread, but then thought this would be a great opportunity to use the ThreadStaticAttribute. I coded this up and it all seemed to work well.

Yesterday a bug was raised against this code because messages unrelated to the service request were being returned. I looked at the code and rechecked the ThreadStatic documentation. It shouldn’t be possible, but it was definitely happening.

I quickly realised that the thread must be being reused across multiple service requests. This reminded me that ASP.Net uses a ThreadPool and it would not be doing any clean up of ThreadStatic data as it reused an existing thread for a new service request.

The simple fix was to reset the ThreadStatic reference when the response is generated.

Tags:

Sep 18 2009

DisconnectedContext when debugging with MSTest

Category: .NetRory Primrose @ 10:42

I have been hitting a DisconnectedContext issue when I’ve been running MSTest in the Visual Studio IDE with the debugger attached. At first I thought it was an issue with debugging WF, but it is now also happening with LINQ to SQL.

image

The debugging on the thread of the executing test appears to be stopped when this is hit. Other threads/tests in the test run will still hit breakpoints however.

Searching the net hasn’t yielded any pointers. Anyone out there have some information about this?

Tags:

Sep 15 2009

Adding delete comment support to the Commentor extension

Category: .Net | ApplicationsRory Primrose @ 09:24

The Commentor extension for BlogEngine.Net written by rtur is a great extension for catching comment spam on the blog. Unfortunately the custom rules you can apply to comments only support a Block or Allow action.

What I want is to prevent the comments being saved at all if a manual rule has been violated. This is better for my needs as I don’t need to clean up the comments on the blog and I don’t get hassled by constant emails regarding comments that require moderation.

The changes to the extension to support this are relatively minor.

First is a change to the User controls\Commentor\Settings.aspx user control. The action dropdown now has a Delete action.

<td>
    <asp:DropDownList ID="ddAction" runat="server">
        <asp:ListItem Text="Block" Value="Block" Selected=true></asp:ListItem>
        <asp:ListItem Text="Allow" Value="Allow" Selected=false></asp:ListItem>
        <asp:ListItem Text="Delete" Value="Delete" Selected=false></asp:ListItem>
    </asp:DropDownList>
</td>

The next set of changes were to implement the Delete action in the App_Code\Extensions\Commentor.cs class.

The ManualFilter enum definition has been extended to include the Delete action.

private enum ManualFilter { None, Block, Allow, Delete }

The Post_AddingComment method now supports this enum value in its processing of a new comment.

// validate against filter
switch (CheckFilter(comment))
{
    case ManualFilter.Block:
        comment.IsApproved = false;
        return;
    case ManualFilter.Allow:
        comment.IsApproved = true;
        return;
    case ManualFilter.Delete:
        comment.IsApproved = false;
        e.Cancel = true;
        return;
    default:
        break;
}

And finally, the CheckRow method has been updated to use the new Delete action.

if (match)
{
    if (action == "Block")
        return ManualFilter.Block;
    if (action == "Delete")
        return ManualFilter.Delete;
    return ManualFilter.Allow;
}

Make these changes and you will be able to use delete actions to prevent comments from being saved.

NOTE: The settings for existing rules will need to be updated where they are stored (most likely in a database or xml file) in order for them to use the Delete action. A restart of the website will be required to pick up these changes that were made directly against the settings datastore. Alternatively you will need to delete and recreate any existing custom rules that you want to use the Delete action with.

My Commentor extension settings now look like the following.

Commentor settings

Tags:

Sep 15 2009

Sense of satisfaction

Category: PersonalRory Primrose @ 04:46

I just found out that something I came up with a few years ago in another job has hit production and is making a big difference. There were a lot of people involved in supporting the idea and some innovators who took the concept and applied it in ways I didn’t think of. Overall it is a great result and I’m very happy to see such a change being applied.

seal

Tags:

Sep 9 2009

Quick tip for undoing unchanged TFS checkouts

Category: .NetRory Primrose @ 05:13

Reviewing changesets with a large number of files that haven’t changed can be a significant waste of time. Sliming changesets down to only changed files is a great idea before checking in files.

I have previously been doing this manually which in itself is time consuming. Fortunately the TFS power toys package comes with a handy utility to automatically uncheck out any files that haven’t changed. Running TFPT.exe uu /r will do the trick. Running up an instance of the Visual Studio command prompt and entering in this command can also be a hassle. Thankfully Visual Studio makes this easy with the support for external tools.

To set this up, go to Tools –> External Tools which will display the following dialog:

External Tools

Click Add and enter the following settings (full Command value is C:\Program Files\Microsoft Team Foundation Server 2008 Power Tools\TFPT.exe)

image

Sending the output to the Output Window makes this setup really easy to review the changes.

The Tools menu should now contain the Undo Null Changes command.

image

Enjoy.

Update: Set initial directory to $(SolutionDir) as sometimes the workspace can’t be determined depending on the context of how the command is launched.

Update #2: Add /noget to the end of the arguments to prevent the local workspace being updated with the latest version before checking for null changes.

Tags: