Code Snippets

Handy blocks of code you can use.

Recursively Copying Indirect Project Dependencies in MSBuild

I’ve run across a similar situation to many folks I’ve seen online, where I have a solution with a pretty modular application and when I build it, I don’t get all the indirect dependencies copied in. I found a blog article with an MSBuild target in it that supposedly fixes some of this indirect copying nonsense, but as it turns out, it doesn’t actually go far enough. My app looks something like this (from a reference perspective) Project: App Host Project: App...

Install/Uninstall of INF Using Chocolatey

I’m messing around with Boxstarter and Chocolatey and one of the things I wanted to do was install the various “Command Prompt Here” context menu extensions I use all the time. These extensions are .inf files and, unfortunately, there isn’t really any documentation on how to create a Chocolatey package that installs an .inf. So here’s how you do it: First, package the .inf file in the tools folder of your package alongside the chocolateyInstall.ps1 script. .inf files are pretty small anyway and you want the file to be around for uninstall, so it’s best to just include...

Using ASP.NET MVC 5 IAuthenticationFilter for Authentication Challenges

One of the new ASP.NET MVC 5 features, authentication filters, has dreadfully little documentation. There’s a Visual Studio Magazine article on it, but that basically replicates the AuthorizeAttribute in a different way. It doesn’t really explain much else. Diving into the source doesn’t tell you too much, either. The context you get in the filter has a little more of an idea about what you should be doing, but… it’s really not enough. The real magic happens in the ControllerActionInvoker.InvokeAction method. The source shows that the general flow is like this: MVC action gets...

Putting Controllers in Plugin Assemblies for ASP.NET MVC

With Autofac (particularly the multitenant extensions) I see a lot of questions come through that boil down to this: I have an ASP.NET MVC project. I have some controllers in a plugin assembly that isn't referenced by the main project. At application startup, I do some scanning and use Autofac to dynamically register the controllers. For some reason I get an error when I try to visit one of these controllers. How can I have a controller in a plugin assembly? Shannon Deminick has a nice blog article that explains this in a different context - similar question,...

Controlling NuGet Packaging Version with TeamCity Variables

We use TeamCity as our build server and one of the cool things TeamCity has built in is the ability to serve as a NuGet server. You build your product, run a nuget pack task on it, and TeamCity will automatically add it to the feed. One of the somewhat odd things I’ve found with TeamCity’s NuGet server is that it seems to require that you let TeamCity run the actual nuget pack on packages it should host. That is, even if you wanted to do that action in your build script, you can’t – simply putting the package...

Automating NuGet Dependency Version Updates with MSBuild

Although I wasn't a big fan of NuGet when it started getting big, I have to admit it's grown on me. I think part of that has to do with the large amount of improvement we've seen since back then. Regardless, I'm in a position with Autofac and other projects where I'm not only consuming NuGet packages, I'm also producing them. One of the biggest pains I have when maintaining the .nuspec files for my packages is that you can update a dependency for your project (via NuGet) but the corresponding version value isn't updated in the .nuspec. (This...

MSBuild Build Variants in Sublime Text 2

Sublime Text 2 has a nice feature where you can hit "Ctrl+B" or select "Tools -> Build" from the menus and, based on the current file type, a build system will be automatically selected and executed against that file. I recently released an update to my MSBuild package for Sublime Text 2 that includes some "build variants" for MSBuild that specifically set Debug or Release configuration during the build. Basically: msbuild yourscript.proj /p:Configuration=Debug or msbuild yourscript.proj /p:Configuration=Release I didn't know this was possible until recently or I'd have put...

jQuery.each and Object Boxing

I just spent like six hours trying to figure this out, so profit from my experience. I have some custom code wiring up jQuery Validation in a form and for some reason, the jQuery "html" method was causing a "HIERARCHY_REQUEST_ERR: DOM Exception 3" whenever validation errors were being displayed. After banging my head against it for a while and getting some help from a co-worker, we found it came down to the $.each iterator. I was compiling up a list of error messages and using the $.each to add them to settings for a jQuery validator. It looked...

Format Configuration Files on Build

We have a large group of devs all working on a single web application. As part of that, we have different folks all working in different branches, making changes to configuration files. Visual Studio does a nice job of letting you keep code formatted via plugins like CodeRush and PowerCommands (“format on save”). Config files? Not so much. As you can imagine, this creates no end of churn in merge conflicts as things switch from tabs to spaces and back, NuGet mucks around with dependency redirects, and entries get added and removed. To address some of this,...

Html.DemoPartial - Side-by-Side Render and Code for Partial Views

I was working on an ASP.NET MVC site for demonstrating some internal concepts recently. One of the things I wanted to show was how to use some HtmlHelper extension methods. I wanted the viewer to be able to see the rendered output and then flip to see the code, sort of like how you see in snazzy demos like the DevExpress MVC Extensions. Here's a little screencast/demo of what I'm talking about. Unable to display content. Adobe Flash is required. So... how do you do that? Here's how it...

Call an MSBuild Target Like a Function

I'm working on building a bunch of projects that all follow a specific convention for naming, NuGet packaging, and so on. As part of that, I want to run the build for each component – from clean to package – all at once rather than clean everything, then build everything, then package everything. (For the sake of the article, let's ignore whether that's a good idea or not and just stick with me.) MSBuild has batching, which sort of works like "for-each," but in examples you see you can really only "batch" on tasks. Targets (groups of tasks) allow...

How to Add a VSIX Installer to a DXCore Plugin

As of CodeRush/Refactor/DXCore 11.2, DXCore supports installation of plugins using Visual Studio Extensibility (VSIX) just like any other extension you might find in the gallery. This is beyond cool for a couple of reasons. First, I've been [slowly] working on a couple of different ways to create some nature of "plugin gallery" for DXCore extensions. There are so many community plugins, plus things like CR_Documentor and CR_CodeTweet that aren't on the community plugin site, that it's really hard to know what's out there. This would allow DXCore plugins to use the standard Visual Studio Gallery mechanism for browsing. ...

Data Type Validation and Model Binding in ASP.NET MVC

When validating input in a web forms application, you need to validate data types on the client and server side because you're working with text boxes and server controls. When you move to MVC, the client-side validation is still an interesting problem to solve, but the server-side validation all happens as a by-product of model binding. The DefaultModelBinder has some special built-in provisions to handle data type parsing errors and automatically convert those into standardized model state errors. If you're writing a custom model binder and you want to participate in this... Get the...

Explore From Here - Command Line

I'm a big fan of the "command prompt here" context menu extensions for Windows Explorer. I use them all the time. Sometimes, though, I need to go the other-way-around. That is, I'm at a command prompt and I want Windows Explorer open at the current location of my prompt. explorer %CD% Pretty simple, but super helpful. I had one of those "man, I'm stupid" moments when I put two-and-two together on this. I ended up making a little batch file "explore.bat" and stuck it in my path. @echo off echo Opening Explorer on %CD% explorer %CD% So now...

Choosing an Exception Type When Unit Testing Error Handling

When I'm testing exception handling code, I have tests for exceptions I know I need to handle and tests for exceptions I'm not expecting. For example, say I have a component that calls a WCF service. If there's a communication issue, I want to mask that and return some stub/placeholder data. If there’s some other issue, I want to just let the exception bubble up and be handled by a global error handler. Something like this: public DataObject GetData(SomeParameter p) { if(p == null) { throw new ArgumentNullException("p"); } DataObject data =...