Sensible .NET REST APIs with AttributeRouting.NET

I have a lot of problems with the default .NET Web API that Microsoft pushes for people to use. You can only easily have two GETs, and one PUT, POST and DELETE route per controller. There is no easy way to create more complicated nested routes, i.e. GET /v1/person/:id/contact/:id, or routes that deviate from the norm. The routing syntax (not just specific for the Web API but other .NET services too) is horrible and unwieldy, and all routes must be defined in a separate file. Route constraints, precedence and validation are also tricky.

All of this may turn you off of writing an API in .NET, but there is an easy solution to all of these problems in the form of AttributeRouting.NET. This NuGet package allows you to define routes directly on controller methods, with out of the box support for route constraints, prefixes, precedence etc. It is compatible with the Web API and also .NET MVC. (more…)

DocuSign Connect XML Schema

For those of you that have developed for the DocuSign E-Signature platform, you’ll have probably used their DocuSign Connect service to listen for document and recipient events so you don’t have to long-poll their servers, which they strongly discourage. The example XML in their Connect documentation frustratingly does not have all of the possible values and fields that could come through with each request.

I decided to investigate this and came upon this StackOverflow Q&A that had a handy answer, which linked to the complete DocuSign Connect XML Schema as an XSD. You can find the XSD below:

DocuSign Connect XML Schema (XSD)

Print GitHub Markdown Bookmarklet

I was looking for a way to print GitHub markdown documents without all of the GitHub navigation, UI and other extraneous information showing up on the page. Mainly because I like GitHub’s clean markdown styles, and it’s very useful to be able to print documentation for certain projects.

All you need to do is add the following bookmarklet to your browser:

Here is a demonstration of what it does with the intridea/grape repo’s README.md file (before and after):

Print Github Before

Print Github After

Could not successfully convert UCS-2 string to UTF-8

We were performing a massive data migration at work from MSSQL to MySQL using the Migration Wizard in MySQL Workbench. When performing the step that copied the data from one server to the other, we got the following error for some nvarchar columns:

Could not successfully convert UCS-2 string to UTF-8

Or alternatively, we also got Error during charset conversion of wstring: No error. This infuriating error took us an hour and a half to diagnose and work around. I finally stumbled upon this link to a bug on the MySQL bugs website relating to MySQL Workbench http://bugs.mysql.com/bug.php?id=69629.

The problem, it turns out, lays with Microsoft’s ODBC Drivers:

This bug is triggered due to improper UCS-2 strings being generated by Microsoft’s ODBC drivers.

And the solution is to select the FreeTDS ODBC Drivers in the first step of the migration wizard of MySQL Workbench. The migration then works perfectly with no further encoding complaints. I hope this helps someone else out with this frustrating problem!

FreeTDS ODBC Drivers

The SMS That Started It All

Today I want to show you the SMS that started it all and began my career as a software developer.

IMG_0462

One day in October 2010 my manager and I sat down to discuss a database solution for our company. We had unsuccessfully tried to use another company’s solution for our business needs but found them lacking, and we were already running an Access database for some of our data. At this time I had no experience with databases whatsoever, not even the basics such as tables, columns and primary keys. Jacob wanted to gauge my interest in learning about databasing and Visual Basic so we could develop our own solution. Needless to say I was very keen. (more…)

Failed to build gem native extension on OSX Mavericks

When installing certain gems on Max OSX Mavericks you may run into some issues where either the gem install or bundle install command throws the error “Failed to build gem native extension”, usually in extconf.rb. This has happened specifically to me with the json and mysql2 gems. Some gems may work exactly as expected.

The reason for this odd behaviour is because in the latest release of XCode 5.1, Apple are treating any unknown command line options as errors. The XCode 5.1 release notes read:

“The Apple LLVM compiler in Xcode 5.1 treats unrecognized command-line options as errors. This issue has been seen when building both Python native extensions and Ruby Gems, where some invalid compiler options are currently specified. Projects using invalid compiler options will need to be changed to remove those options. To help ease that transition, the compiler will temporarily accept an option to downgrade the error to a warning: -Wno-error=unused-command-line-argument-hard-error-in-future”

What this means that if any gems use invalid command line arguments in their build or install process, the error “Failed to build gem native extension” will be thrown. As a temporary fix, you can try running either bundle install or gem install with the following argument, which downgrades the error to a warning:

This should clear up most errors. The fix is temporary because Apple have said they will be removing support for the argument in the future, which means any gems with invalid arguments will need to update their build commands.

mysql2

This fix did not work when installing the mysql2 gem, I got the same error but it required an extra step to fix. I had to run the command brew install mysql. Afterwards I could just run bundle install and the problem was fixed!

Another Year, Another Redesign

We are in the future of 2014 and since it’s been about a year since I’ve changed this blog’s design, I thought time would be ripe to do it again! It’s taken me quite while longer than I would have like this time, but I have a good excuse for that in the form of the birth of my son Patrick Noel Allen Brennan!

I’ve based this design on themes I’ve seen for Octopress, with a large area to place more importance on the content of the blog. I’ve also added a sidebar back in because the site is now 100% width, with a focus on search. I’ve been using a great search plugin called Relevanssi that replaces the default WordPress search, and adds features like indexing, result weighting and search hit highlighting.

For the sidebar icons I used the Simple Icons at http://simpleicons.org/ and once again I’ve used the excellent highlightjs for syntax highlighting of code snippets, for example on the JavaScript I used to highlight the currently selected page!

Let me know what you think! Here is a comparison of old vs. new!

New

New Design

Old
Old Design

ContentResult or ViewResult Response Based on Context

In .NET MVC there are times where you may want to return either a ContentResult or ViewResult depending on the situation. For example you may want to return an application/json result for an API request but a structured view with timing information (such as when using MiniProfiler) when visiting the route in the browser.

I had to find out how to construct the different types of results separately first. The main properties that need to be set for a new ViewResult object are the ViewName and the ViewData. ViewData has a Model property where you can set the model that you would like to return with the view. The SimpleResponse model is a simple model that just has a JSON property that I use to display the JSON output of an API route in a nicely-formatted way.

For a ContentResult, you only need to set the content type and the content of the response. Because the API I am developing uses JSON responses, the content type is application/json; charset=UTF-8.

Now, we can put this all together into a practical example. Inside a class called APIResponse I have a method called Resolve.