Unit Testing Web API Controllers with Headers

I couldn’t find a good example of how to unit test a controller with

[code langauge=”csharp”] var headerValue = Request.Headers.GetValues("HEADER");[/code]

in the controller. Not too common, but happens enough it can be expected. Also the controller inheirts “ApiController” which doesn’t inheirt from “ControllerBase” so the usual controller context call won’t work.

Here is how I coded the unit test

[code language=”csharp”]
var config = new HttpConfiguration();
//add in the actual route or method if your are looking at it in your code
var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/random");
//DefaultApi maps to your web api route setup
var route = config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}");
//add in the extra data, similar to when you call @Url.Route
var routeData = new HttpRouteData(route, new HttpRouteValueDictionary { { "controller", "products" } });
var controller = new MyProject.Controllers.Api.ExampleController();
controller.ControllerContext = new HttpControllerContext(config, routeData, request);
controller.Request = request;

In the controller, do a

[code langauge=”csharp”] var headerValue = Request.Headers.GetValues("HEADER");[/code]

do not do

[code langauge=”csharp”]
var headerValue = HttpContext.Current.Request.Headers.GetValues("HEADER");[/code]

This will hit the old IIS style objects and your unit test will fail.

If you don’t have Resharper, you might need these using statements.

[code langauge=”csharp”]

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Routing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;


Custom Cabinet – Part II

Now the face frame is next. You can use pocket screws everywhere you see a Domino instead. I cut the eight holes for the four Dominos. The Dominos have a specific glue requirements, mainly because they are not like normal biscuits.

First, dump some glue in the hole. The domino cutter cuts a little extra gab at the bottom. Using a Fastcap Glue-Bot, easier than the usual wood glue bottles.


Next, put some glue on the Domino on both sides. Fancy glue brush in all, Bench Dog Silicon Glue Brush, its like $8 but glue never gets stuck on it :).


Dropping glue to the other side. Bad news, this glue will fall out.


Start clamping, the triangle is for alignment and to tell which parts go where. The tiny bit of glue means you used just enough, you should worry when you don’t see any glue.


Final check, run a tape measure to check the diagonals. This lets you know if it is square or not or how off they are. This one turned out to be less than 1/16 out of square thanks to the domino system and I bet the error is all on my using the tape measure.

If it is out of square, run a clamp down the diagonal to squeeze it back to square. Back pipe clamps are useful and cheap for this task.


Next up, putting peg holes in place for the shelf, and attaching the face frames.

Roubo Build Update

All pegged up and the bench tops have been set on base. Unlike my thin saw horses, it didn’t budge at the extra weight. I have some quick sanding to do for another project. So the tops might stay like this a while. The height is great.

Roubo Bench


Custom Cabinet – Part 1

We have this awkward closet in the basement that was serving no purpose. We took plywood, put it at counter top level, then nailed down some red oak flooring. It look great, and soon we will pull the tv off, stain it with walnut, polyurethane it and it will match the stairs. We have to address the big hole in the middle first, CUSTOM CABINET TIME!

Lessons learned.

  • To cut two pieces the same size, do it at the same time on a table saw
  • Clamp when using pocket screws and it goes better
  • Don’t forget the face frame overhang



Step 1, go buy most of the cabinets. Building cabinets and drawers is tough. Which is why I’m building just one :). I’m following these plans from Ana White – Build a 18″ Kitchen Cabinet Drawer Base. Mine won’t have the drawers and her plan is a lot simpler than the Kreg Jig plans!

Step 2, buy some plywood. I bought three 3/4″x24″x48″ Oak Plywood  pieces, two for the sides, one as a spare. You can never have enough plywood.

Step 3, cut the plywood. You cut the sides to height, do not adjust the table saw during this time. If you do, trim both pieces to keep them even.  I kept the offcuts to use as the bottom and top supports.


Step 4, cut the bottom to width. Cabinets have a 1/4″ overhang of the face frame plus the 3/4″ width of the plywood, and count it twice. So subtract 2″ to from the total width. This is my second cut.


Step 5, cut the supports to width. If you have not adjusted your table saw, the measurements will match.

Step 6, cut to height, which you will be doing 5″ wide, not confusing at all.  These are for the bottom. When setting your fence, sight it down with one eye at the blade (blade off of course). Also push on your tape measure, the play at the end is the tape measure adjusting for the hook’s width.

IMG_0319 IMG_0318

Step 7, push the fence closer for the top supports. Wide enough for two pocket screws, I picked somewhere around 3-4″ wide. No sense to waste wood no one will see or use.

Step 8, lower the table saw blade and use it as the surface to assemble the cabinet.  Then start drilling the pocket holes. Keep in mind the finish side versus the ugly side of plywood.

Using the Kreg Jig® R3


Step 9, you need to cut the toe kick. Ever try to stand against a wall, the toe kick lets you bump up against the cabinet. I cheated and used my jigsaw and hand held a combination square. If you clamp a square, it squeezes the metal like a dent in your car door.

Straight and perfect


Step 10, clamp while screwing the pocket screws. I used the coarse threaded Kreg Screws, there is some internet debate on which to use. Considering pocket screws can hold 600lb of force, it doesn’t matter which. The cabinet will have downward force from the counter top, or lateral force from the cabinet hinge. A touch of glue on the face sides, a double dose for the end grain sides. End grain soaks up glue, so you have to saturate it to get any to stay on the surface. (End grain glue joints also have no strength, hence pocket screws)




I slightly boxed myself in with my long clamps.




I might need more room


To align things, I just smack it with my trusty hammer. It has a wide flat face, which keeps you from slamming something to far away.


Step 11, make some shelves. I decided to go with adjustable shelves since this will hold an Xbox, DVD player, and a APC UPS for battery backup and surge protection. To better paint the shelves, and add strength, add a piece of solid wood to the front. First attempt was with a biscuit joiner. Biscuits are not for alignment, more adding strength.

Clamping a biscuit jointed board
Clamping a biscuit jointed board

Festool Domino 500 to the rescue! It produces a mortise plunge cut, and provides precisely matched tenons to glue in place.

Festool Domino 500
New tool halfway through the process

All put in place, and ligned up ready for glue.

Dominos in the edge banding

Way less clamping needed, and no seem between the plywood and the piece of oak.

Domino jointed edge clamped

Coming in part two, cut the pin holes for the shelving, prime, paint and face frames with the Festool Domino!







Six Board Blanket Chest – Build

For the Christmas season I decided to be ambitious and build not one, but two, Six Board Blanket Chests. One for my mother and the other for my mother in law, they both have a lot of blankets.

Traditionally, a six board chest is made with six boards jointed for each panel and planned down. Along with the boards being one inch thick. My version of this follows that with a few minor changes.

Materials Needed

  • Edge Glued Panels from your local home improvement store
    • (2)  3/4 x 18-in x 72-in Aspen Panels – Front/Back Pieces
    • (2)  3/4 x 24-in x 48-in Aspen Panels – Side Pieces
    • (4)   1 x 24-in x 48-in Stain grade Spruce Panels – Top/Bottom Pieces
  • Piano Hinge, 1 1/2 inch or 1 1/16″
  • Cut Nails, more on this later
  • Painters Tape

Tools Needed

  • Table saw or the ability to cut a straight line (panel saw, circular saw)
  • Block Plane
  • Hammer
  • Chisel

Plan loosely followed
Fine Woodworking – 18th-Century Six-Board Chest (PDF)

First step is to cut the large panel down to size. Using the 72″ panel, you can make the front panels for two blanket chest at the same time. One will be slightly smaller, but most won’t notice or have anything to compare it too. Make sure to apply painters tape cut line, this will prevent tear out on softer woods like aspen or pine. 36 7/8″ x 17″ final dimension for chest 1, ~35″ x 17 for the front/back of chest two.




Time to cut the sides. There will be a small cut-off in the middle that can be used as scrap or later for the battons for the top. Final Measurements, 17″ x 24″. You should have a small strip less than an inch if you accounted for the saw kerf.


Second side cut, you can see the painters tape worked and stayed on.


Mark off the side that will accept the stopped dado for the side. This is really important as you can’t just switch them them around as the front has the stopped dado, not both. Also start marking the pieces, S2 is for Side of the 2nd blanket chest, the underscore is in case you flipped it around.





Switch to your dado blade and make all the dado cuts. Dado blades are a pain to change and setup. Feather board and push block used for protection.



My new/old Ridgid saw can’t accept a large dado blade, plus I setup the dado for the grooved bottom. A block plane can clean it up very nicely and quickly. No one will see this side of the joint.





You can see the dado cut on the side panel below. Showing the same dado cut twice and I forget to get that picture. Same setup as before.

Stopped Dado – A normal dado that stops to a 90 degree angle. You can’t finish it just on the table saw, but it can help. You see i stop right before the stop point and the blade is fully raised to try and get as close to 90 as possible with a round blade.



Time to clean up the stopped dado. First, lets get rid of all the waste. Make sure to put a waste block under to help tear-out and you don’t want to bang a chisel into a table saw.




All done, just a few blows with the mallet.




If you have followed the instructions well, you should have this clean cut. The side will hold the bottom board, and the front/back will rest on top of the piece.





The chest is held together with 20 “cut nails”. They will be visible, so you can use a “Divider” to evenly space them. Sharpen the point, and push in, rotate, push in, repeat. They look like a middle school compass missing the pencil.



Soft woods and nails near the edge tend to split wood. Drill a small pilot hole to help prevent this.


Cut nails are not round like a wire nail. Make sure to align the long portion with the grain, again, prevents splitting. Cut nails are what was used to build the chest long ago, they are hard to find. I buy from *insert tremont*. Cut nails are sold by the pound and in increments of 3d, 4d, 5d, 6d, 8d. We are using 3/4 material, so that is 6/8th, so we use a 6d nail. I am purposefully ignoring the data we put into it.



You should have a small overhang, and that is good! An overhang helps you to flush up the edges, and a block plane helps to do that. If you set it for a large shaving, you will need to put a lot of force in it. You are going against end-grain, so it is acting like a chisel slicing all the wood fibers.


Paint and you are a step away from done!








Entity Framework MSSQL Sequence Numbers

I had an application where I wanted to have a sequence key that spanned multiple tables. Mainly to provide better logging support, so the log search would be “Find Object X” instead of “Find object X with type Y”. Then add in namespacing and the logs start to get wide, IE “Find Object 12 with type Company.Product.Service.Model.ModelName” repeated hundreds of times.

Step 1, make sure your object in Entity Framework is set to a StoreGeneratedPattern of “None”. (Right click table, select properties, go to the property window)

Entity Framework Properties

Change your Entities partial class to include this override to save changes.

[gist id=”400bb4513848ef85bf3a” synhi=true lang=”csharp”]

And done!


Failed to start shard

I have a project that uses ElasticSearch for searching and all the goodness that it has. When I test a new technology, I like to break it a few times.  Like adding a bunch of data, hitting the stop button on the service or killing the task in task manager.

So I did this with ElasticSearch, and I received this error message.

failed to recover shard]; nested: ElasticsearchIllegalArgumentException[No version type match

Everywhere that I read included some details about how the disk filled up, or was low on space.



Simple solution, flush the index. ElasticSearch has an internal and on disk transaction log.  Being used to SQL’s ACID based consistency, you can’t shutdown the service without ill-effects. 8-7-2014 10-58-01 AM


More details on the flush command and how to access it from the API versus the HEAD plugin, http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-flush.html



S.L.A.B. To ElasticSearch

I always like to find new ways to not write the same code twice. Logging has been one of those features that every application needs, and every application does it differently. Do you log to SQL, a text file, for how long, what format?

The Semantic Logging Application Block supports SQL, Azure, ElasticSearch or any other “Sink” you want to write. It supports buffering, retry for when a Sink isn’t available. Awesome, all the stuff I want but don’t need to write my own version of. More details on SLAB here, Semantic Logging Application Block @ CodePlex

So how do you use this wonderful tool?

1. Install ElasticSearch. It is more like an XCopy deploy then an MSI Install. Installing ElasticSearch @ ElasticSearch.org

2.  Install the template from ElasticSearch Sink to help ElasticSearch understand your log messages.

3.Download the Nuget Packages,

PM > Install-Package EnterpriseLibrary.SemanticLogging.Elasticsearch

4. Put this Gist into your code somewhere and point it to your ElasticSearch server. Make sure your log type matches the one used in the ElasticSearch Template. Best to just use “etw” as below.

[gist id=”7e367d7bf5fbcab5129c” synhi=true lang=”csharp”]

5. Log an error


6. Done.

Next post I will show how to use two different ElasticSearch plugins to monitor and view your errors.