.NET

Working with the .NET framework.

Answered Autofac FAQ: How do I pick a service implementation by context?

Over at Autofac we’re trying to get a more robust set of documentation out to help folks. The wiki is nice, but it leaves a lot to be desired. As part of that, we’re also trying to get some answers published to some of the more frequently asked questions we see popping up on StackOverflow. Today I pushed out the doc answering that timeless question, “How do I pick a service implementation based on a particular context or consuming object?”

posted @ Thursday, July 17, 2014 12:02 PM | Feedback (0) | Filed Under [ .NET ]

Tweaking Web API URL Request Validation

In working with a REST API project I’m on, I was tasked to create a DELETE operation that would take the resource ID in the URL path, like: DELETE /api/someresource/reallylongresourceidhere HTTP/1.1 The resource ID we had was really, really long base-64 encoded value. About 750 characters long. No, don’t bug me about why that was the case, just… stick with me. I had to get it to work in IIS and OWIN hosting. STOP. STOP RIGHT HERE. I’m going to tell you some ways to tweak URL request validation. This is a security thing. Security is Good....

Updating Package References in Selected Projects With Package Manager Console

In the Autofac project we’ve maintain all of the various packages and integrations in one assembly. In order to make sure each package builds against the right version of Autofac, all references are redirected through NuGet. A challenge we face is when we are testing a new release of Autofac, we want to update specific integration projects with the latest version of Autofac so we can do testing, eventually upgrading everything as needed. Running through the GUI to do something like that is a time consumer. Instead, I use a little script in the Package Manager Console to...

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

Scripting NuPkg Reference Changes for Testing

I was testing out some changes to versioning in Autofac. We have a MyGet feed, but all of the internal dependencies of the various NuGet packages when they’re built point to the CI versions, so it’s sort of hard to stage a test of what things will look like when they’re released – you have to rename each .nupkg file to remove the “-CI-XYZ” build number, open each .nupkg file, change the internal .nuspec file to remove the “-CI-XYZ” build number info, then re-zip everything up. In testing, I had to do this a few times, so I scripted it....

posted @ Tuesday, April 01, 2014 8:11 AM | Feedback (0) | Filed Under [ .NET ]

Two Web API Gotchas

I just spent a day fighting these so I figured I’d share. You may or may not run into them. They do get pretty low-level, like, “not the common use case.” PROBLEM 1: Why Isn’t My Data Serializing as XML? I had set up my media formatters so the XML formatter would kick in and provide some clean looking XML when I provided a querystring parameter, like http://server/api/something?format=xml. I did it like this: var fmt = configuration.Formatters.XmlFormatter; fmt.MediaTypeMappings.Add(new QueryStringMapping("format", "xml", "text/xml")); fmt.UseXmlSerializer = true; fmt.WriterSettings.Indent = true; It seemed to work on super simple stuff, but then it seemed to arbitrarily...

Updated Autofac Assembly Versioning Strategy - New Package Releases

Until now, Autofac assemblies have changed version using a slow-changing assembly version but a standard semantic version for the NuGet package and file version. The benefit of that approach is we could avoid some painful assembly redirect issues. The drawback, of course, is that even minor changes (adding new functionality in a backwards-compatible way) can cause problems – one project uses version 3.0.0.0 of Autofac and works great, a different project also uses version 3.0.0.0 of Autofac but breaks because it needs some of that newer functionality. That’s hard to troubleshoot and pretty much impossible to fix. (It’s...

posted @ Thursday, March 27, 2014 3:01 PM | Feedback (0) | Filed Under [ .NET ]

AutoMapper, Autofac, Web API, and Per-Request Dependency Lifetime Scopes

I’m working on a new Web API project where I want to use AutoMapper for some type conversion. As part of that, I have a custom AutoMapper type converter that takes in some constructor parameters so the converter can read configuration values. I’m using Autofac for dependency injection (naturally). Historically, I’ve been able to hook AutoMapper into dependency injection using the ConstructServicesUsing method and some sort of global dependency resolver, like: Mapper.Initialize(cfg => { cfg.ConstructServicesUsing(t => DependencyResolver.Current.GetService(t)); cfg.CreateMap(); }); That works great in MVC or in other applications where there's a global static like that. In those cases, the...

How to Get a TFS 2012 Build Agent to Build With Visual Studio 2013 Tools

Let me say up front, I’m no TFS guru. I’m sure there’s something simple I’m probably overlooking. I just feel like this was far more complicated than it had to be so I can’t get over the idea I’m missing a simple switch flip here. Anyway. We have a bunch of TFS 2012 build agents. They all have VS 2012 installed and they build VS 2012 solutions really well. But we’d like to start working in VS 2013, using VS 2013 tools, so I undertook the adventure of figuring this out. I thought just installing Visual Studio...

MVC, Task.Run, and HttpContext.Current

For reasons I won’t get into, we recently ended up with a scenario in MVC where we needed to use RenderAction to get some data into a view. Some of the data was exposed via async calls to services. The challenge is that RenderAction doesn’t support asynchronous controller actions. To accomplish the task, we ended up with a synchronous controller action that used Task.Run to get data from certain async calls. And, yeah, I know that’s not really the greatest thing in the world but there wasn’t a great way around that. That landed us with a new...

NDepend 5 - New UI, New Features

I’ve been using NDepend for a while – since version 2.7 – and each release is always noticeably better than the last. Version 4 last year brought with it some of the best stuff with CQLinq and seemed to focus a lot on enhancing the internals and technical usefulness. The latest version, version 5, focuses on the UI and the general user experience. The NDepend site actually has a great overview of the new features, and Patrick Smacchia has a sort of case study explaining the UI enhancements, so I suggest you check those out. The UI enhancements...

posted @ Monday, October 28, 2013 3:04 PM | Feedback (0) | Filed Under [ .NET ]

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

DateTime Calculation and Unit Tests

I just had an interesting unit test failure I haven't seen before due to DateTime calculation. The code under test was roughly like this: public Lifetime GetTokenLifetime(int seconds) { return new Lifetime(DateTime.UtcNow, DateTime.UtcNow.AddSeconds(seconds)); } The unit test was also pretty straightforward: [Test] public GetTokenLifetime_ExpectedLifetime() { var provider = new Provider(); var lifetime = provider.GetTokenLifetime(120); Assert.AreEqual(120, lifetime.Seconds); } The test was intermittently failing... and the error pretty much explained why: Expected: 120 Actual: 120.0000063d There was just a tiny fraction of a second happening between the first DateTime.UtcNow call and the second DateTime.UtcNow call. To fix it, I stored the DateTime in a local variable. public Lifetime GetTokenLifetime(int...

Setting DependentUpon File Properties on NuGet Package Install

In working on some NuGet packages, one thing I wanted to do was set up some configuration files in preparation for SlowCheetah integration. Instead of seeing a folder structure like this in the project… ~/Config MyConfig.config MyConfig.Debug.config MyConfig.Release.config I wanted to see the file dependencies set up like you usually get with Web.config: ...