.NET

Working with the .NET framework.

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...

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 =...

WCF Fails to Build or Update Service Reference When Path Too Long

I was working yesterday on a solution in Visual Studio and noticed that every time I'd rebuild VS would report the build as failed... but without any error messages. I thought it was just a fluke, but then I had to update a service reference. When I tried, I got the following error message: Could not resolve mscorlib for target framework ".NETFramework,v4.0". This can happen if the target framework is not installed or if the framework moniker is incorrectly formatted. I searched all over and verified the TargetFramework settings on every project. No...

MVC3 Project Template Requires Solution Node in Solution Explorer

I've got Visual Studio 2010, MVC3, and the latest tools update. I'm all patched up. But I'm seeing something weird. I start out and I have a solution with a single C# class library in it. I decide to add an MVC3 site to it, so I do File -> Add New Project. I select an MVC3 project. I click OK, and I select an Empty web application. When I click OK on this screen, I get an error "Cannot add the item because the item to add it to...

NuGet Doesn't Help Me

There's been a lot of hoopla around NuGet and the whole .NET package management "thing." There's a lot of praise going around, and I think they've done a good job for what they're doing. That said, I have what I'm sure is going to be an unpopular opinion: NuGet doesn't help me. It seems to me the primary benefits of NuGet are: Get third-party dependencies into new projects faster. Help you more easily update packages in your project. That's all well and good, but I think...

Adding Top Level Menu Integration to a DXCore Plugin

Most of the DXCore/CodeRush plugins I write are Tool Window plugins like CR_Documentor or are plugins you'd bind to a hot key like CR_JoinLines. For Tool Windows, DXCore automatically gives you menu integration... ...and for hot key plugins, you don't need it. But sometimes your plugin isn't really a tool window, or maybe you need to integrate into a different menu, like the standard "Tools" menu. I'll show you how to do that. Before you begin, consider if you really need this. It's not hard to add, but if you're like me, you already have a...

Six Reasons Not to Use Guard Classes

"Guard" classes - those little "convenience wrappers" around common argument checking and exception throwing. You know what I'm talking about, things like... public static class Guard { public static void AgainstNull(object value, string parameterName) { if(value == null) { throw new ArgumentNullException(parameterName); } } } Then, rather than the if/throw block in your method, you have something like... public void MyMethod(string theParameter) { Guard.AgainstNull(theParameter, "theParameter"); // Do the rest of the work... } It seems like a good idea, right? Reduce the three lines of if/throw checking...

Make Your Sandcastle Help File Builder Project Dynamic with MSBuild

When building a Sandcastle Help File Builder (SHFB) project in the GUI, you manually specify the list of assemblies you want to document. However, if you want to make the execution of it a little more flexible, you can do a bit of MSBuild magic to dynamically build up the list of documentation sources for the project prior to execution. The reason this works is that SHFB has changed its command-line execution to run through MSBuild using build tasks rather than a standalone executable. (Though with the executable, you could go through some steps to fashion a response file,...

Long URL Path Segments in ASP.NET Routing Yield 400 Errors

Ran into an interesting gotcha while working with routing and a handler on a web site. We had a route set up like this: CustomHandler/{value} Nothing too special, except the "value" route parameters are fairly long strings. In some cases 300 characters or more. We were finding that in some cases things worked great, but in others IIS would return a 400 error claiming "invalid URL." The URL total length was less than 1000 characters, so it wasn't that. Turns out there's a registry setting for indicating the maximum length of any individual path segment...

Typemock Isolator Troubleshooting Guide

Typemock Isolator is a cool and very powerful mocking framework. However, "with great power comes great responsibility," and it's easy to get into situations where there are errors appearing that are hard to troubleshoot. For example, you may end up with a test that passes if run by itself but when run as part of a fixture (or a full set of test fixtures) it fails inexplicably. This guide can help you look for "red flags" that, in my experience, can cause these hard-to-figure-out issues. Almost every time you encounter one of these inexplicable errors it's because someone corrupted...

posted @ Monday, August 22, 2011 8:39 AM | Feedback (0) | Filed Under [ .NET ]

Running Static Files Through VirtualPathProvider in IIS7

We have a custom VirtualPathProvider that serves some static files (*.js, *.css) from embedded resources in assemblies. It is similar in function to the WebResource.axd that ships with ASP.NET, but instead of having some crazy URL, you just access the file directly and the VPP finds it in embedded resources and serves it just like it was on the disk. It makes for a nice deployment experience and easy upgrade. The problem I've run into a bunch, particularly with routing showing up, is that even with a wildcard map to ASP.NET, my static files end up with a 404...

Test Your Deployed Web Site Package with IIS Express and Visual Studio

With the ability to transform your web.config file when deploying your web site came, at least for me, a question: How do I test my web site's behavior without deploying a whole copy of my web site? I figured out a reasonable, if slightly kludgy, solution and I figured I'd share. The general idea is to have a project in Visual Studio that... Acts as the point of entry for debugging the packaged version of the web site. Automatically updates IIS Express configuration to point to the packaged web site. ...

Why Aren't You on the Latest and Greatest Framework?

I was watching some Twitter stream by and caught a bit of a discussion asking about why people haven't moved to xUnit.net yet for unit testing. It started here... Legitimate, good question. xUnit is a nice unit test framework. The thing is, I see a lot of these things fly past - Why haven't you updated to ASP.NET MVC3? Why haven't you switched your project to .NET 4.0 from .NET 3.5? How come you're not installing every third-party dependency in your project through NuGet now? What? You're still on jQuery 1.5.2? But 1.6 is out! You're...

When StaticFileHandler is not StaticFileHandler

I could also have called this "wildcard .NET mapping in IIS Express from web.config." I'm sure that, like, everyone out there but me has figured this out by now, but... well, I'll blog it anyway. Problem: Your ASP.NET web site has a VirtualPathProvider that serves static files (e.g., .jpg, .css, etc.). It works great in the Visual Studio development web server but switching to IIS Express, it suddenly doesn't work. My team has just such a provider that serves static files out of embedded resources. We switched from Cassini over to IIS Express and couldn't for the...