.NET

Working with the .NET framework.

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

FluentValidation and MVC - From Server to Client

We do a lot of interesting stuff with FluentValidation at work and more than a few times I've had to give the whiteboard presentation of how a server-side FluentValidation validator makes it to jQuery validation rules on the client and back. I figured it was probably time to just write it up so I can refer folks as needed. Let's start out with a simple model we want to validate. We'll carry these examples with us in the walkthrough so you have something concrete to tie back to. public class MyModel { public string Name { get; set;...

Portable Class Library Answers

Since we switched Autofac to be a Portable Class Library, it's been nice not having to maintain different builds for different platforms. It's also nice to not have preprocessor directives littered through the code. Props to Microsoft for making it part of VS2012. The problem is… not very many people are familiar with Portable Class Libraries and what they do. Probably once a week (give or take) we'll get a defect filed on Autofac that it's targeting the wrong version of .NET or something along those lines. To that end, I figured I'd blog the answers to some...

posted @ Friday, March 29, 2013 9:58 AM | Feedback (0) | Filed Under [ .NET ]

Gave Up Trying FxCop 11 Standalone

In FxCop 10 you could run code analysis without installing Visual Studio by either grabbing just the FxCop stuff out of Visual Studio or installing the Windows SDK. Not so, with FxCop 11. There's no longer a standalone installer - it's been removed from the Windows SDK. I tried grabbing the FxCop out of Visual Studio 2012 and it fails with the exception: System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.VisualStudio.CodeAnalysis.Interop.dll' or one of its dependencies. The specified module could not be found. Using Dependency Walker on Microsoft.VisualStudio.CodeAnalysis.Interop.dll, you can see a ton of missing assembly references:...

Adding Custom Files to an MSDeploy Package

As part of some of my web projects I have "plugin assemblies" that aren't directly referenced by the project but are things I want included in my deployment package. I tried following the instructions on this fairly popular blog entry, but it didn't seem to work - that blog entry tells you to modify a set of files during a stage in the packaging pipeline "CopyAllFilesToSingleFolderForPackageDependsOn" and that target never actually fired for me. In fact, I threw an <Error> call in there just to see if I could get the build to fail and… no luck. It also...

Can You Run Visual Studio in Azure?

UPDATE 6/1/2013: As of June 1, 2013, the licensing has been updated so you can run all of your MSDN software except Windows client and Windows Server on Azure. This is outlined in the licensing whitepaper. I'll leave the below discussion in place for posterity but the latest update has rendered it irrelevant. I was sitting at the car repair place on Saturday with my Surface RT thinking it'd be nice to have a Visual Studio instance to tinker with. Given I have an MSDN subscription that comes with a free Azure VM and Visual Studio, it made sense to me...