Angular 2 server side paging using ng2-pagination

31 March, 2016.

Angular 2 is not quite out of beta yet (Beta 12 at the time of writing) but I’m in the full flow of developing with it for production use. A common feature, for good or bad, is to have lists/tables of data that the user can navigate through page by page, or even filter, to help find something useful. Angular 2 doesn’t come with any out of the box functionality to support this, so we have to implement it ourselves. And of course what the means today is to use a third party package! To make this…

Continue reading

Create a RESTful API with authentication using Web API and Jwt

15 March, 2016.

Web API is a feature of the ASP .NET framework that dramatically simplifies building RESTful (REST like) HTTP services that are cross platform and device and browser agnostic. With Web API, you can create endpoints that can be accessed using a combination of descriptive URLs and HTTP verbs. Those endpoints can serve data back to the caller as either JSON or XML that is standards compliant. With JSON Web Tokens (Jwt), which are typically stateless, you can add an authentication and authorization…

Continue reading

ASP .NET 5 (vNext), first thoughts

21 August, 2015.

Microsoft ASP .NET 5 is a major shift from traditional ASP .NET methodologies. Whilst I am not actively developing ASP .NET 5 applications at the minute, .NET has always been my bread and butter technology. When I look at industry trends here in the UK, all I see is .NET .NET .NET, therefore it is important to have one eye on the future. I’ve watched all the introduction videos on the ASP .NET website , but I also wanted to take a look at what ASP .NET 5 means to me . This is not meant to be a…

Continue reading

WCF custom authentication using ServiceCredentials

03 April, 2015.

The generally accepted way of authenticating a user with WCF is with a User Name and Password with the UserNamePasswordValidator class. So common that even MSDN has a tutorial , and the MSDN documentation for WCF is seriously lacking at best. The username/password approach does what it says on the tin, you pass along a username and password credential from the client to the server, do your authentication, and only if there is a problem then you throw an exception. It’s a primitive approach…

Continue reading

Quick tip - Avoid 'async void'

30 August, 2014.

When developing a Web API application recently with an AngularJS front end, I made a basic mistake and then lost 2 hours of my life trying to figure out what was causing the problem … async void . Its pretty common nowadays to use tasks to improve performance/scalability when writing a Web API controller. Take the following code: At a high level, when is executed the call will be moved off onto another thread and the execution of the method will only continue once the operation is complete…

Continue reading

5 AutoMapper tips and tricks

23 August, 2014.

AutoMapper is a productivity tool designed to help you write less repetitive code mapping code. AutoMapper maps objects to objects, using both convention and configuration. AutoMapper is flexible enough that it can be overridden so that it will work with even the oldest legacy systems. This post demonstrates what I have found to be 5 of the most useful, lesser known features. Tip: I wrote unit tests to demonstrate each of the basic concepts. If you would like to learn more about unit testing…

Continue reading

How to create your own ASP .NET MVC model binder

11 August, 2014.

Model binding is the process of converting POST data or data present in the Url into a .NET object(s). ASP .NET MVC makes this very simple by providing the DefaultModelBinder . You’ve probably seen this in action many times (even if you didn’t realise it!), but did you know you can easily write your own? A typical ASP .NET MVC Controller You’ve probably written or seen code like this many hundreds of times; Where did Id come from? It probably came from one of three sources; the Url ( ), the…

Continue reading

Easy WCF Security and authorization of users

19 July, 2014.

There are several steps involved in making your WCF service secure, and ensure that clients consuming your service are properly authenticated. WCF uses BasicHttpBinding out-of-the-box, which generates SOAP envelopes (messages) for each request. works over standard HTTP, which is great for completely open general purpose services, but not good if you are sending sensitive data over the internet (as HTTP traffic can easily be intercepted). This post discusses how to take a basic WCF service…

Continue reading

Entity Framework - Use a Guid as the primary key

13 July, 2014.

Using a Guid as your tables primary key, when using Entity Framework, requires a little more effort than when using a integer. The setup process is straightforward, after you’ve read/been shown how to do it. The process is slightly different for the Code First and Database First approaches. This post discusses both techniques. Code First Using a Guid as the primary key when taking the code first approach is simple. When creating your entity, add the DatabaseGenerated attribute to your primary…

Continue reading

How to create a RESTful web service using WCF (Part 3 of 3)

04 April, 2014.

RESTful (Representational State Transfer) web services use HTTP verbs to map CRUD operations to HTTP methods. RESTful web services expose either a collection resource (representational of a list) or an element resource (representational of a single item in the list). Other parts in this series: How to create a RESTful web service using WCF (Part 1 of 3) How to create a RESTful web service using WCF (Part 2 of 3) Testing the WCF service using Fiddler If you haven’t come across Fiddler before…

Continue reading

How to create a RESTful web service using WCF (Part 2 of 3)

03 April, 2014.

RESTful (Representational State Transfer) web services use HTTP verbs to map CRUD operations to HTTP methods. RESTful web services expose either a collection resource (representational of a list) or an element resource (representational of a single item in the list). Create the WCF service contract Every WCF service begins with a service contract. A service contract defines what operations are supported/provided by the service. An operation contract is the definition of a method that can be…

Continue reading

How to create a RESTful web service using WCF (Part 1 of 3)

02 April, 2014.

RESTful (Representational State Transfer) web services use HTTP verbs to map CRUD operations to HTTP methods. RESTful web services expose either a collection resource (representational of a list) or an element resource (representational of a single item in the list).HTTP verbs are used as follows; Create (POST) > create a new resource. Read (GET) > retrieve one or many resources. Update (PUT) > update an existing resourπce. Delete (DELETE) > delete an existing resource. This tutorial…

Continue reading

How to add SignalR to the telephone directory sample application

15 March, 2014.

As part of a recent mini series we created a very simple telephone directory web page that showed some features of Bootstrap, KnockoutJS, jQuery, Web API and the Entity Framework. Just for fun, I figured it would be cool to add SignalR so that we could see how simple it is to add full duplex, real time communications to your website. The first parts of the mini series can be found here; /.net/create-a-telephone-directory-with-bootstrap-knockoutjs-jquery-web-api-and-entity-framework-part-1-of-…

Continue reading

Quick look - Add indexes to tables with Entity Framework 6.1 Beta 1

26 February, 2014.

At the time of writing, beta 1 of Entity Framework 6.1 has recently been released. This is mostly a maintenance release , however, there are several new feature of note, including the ability to add indexes using a new data annotation when developing using the code first approach. Add indexes using the Index attribute To add an index, simply add the Index attribute as required; A quick look in SQL Server Management studio reveals that the index has been added for us. The key created was non…

Continue reading

Create custom C# attributes

22 February, 2014.

You have probably added various attributes to your ASP .NET MVC applications, desktop applications, or basically any software you have developed using C# recently. Attributes allow you to provide meta data to the consuming code, but have you ever created and consumed your own attributes? This very quick tutorial shows how to create your own attribute, apply it to your classes, and then read out its value. Sample Project To demonstrate this concept, I have created a Console application and added…

Continue reading

Use T4 Templates to create enumerations from your database lookup tables

15 February, 2014.

T4 (Text Template Transformation Toolkit) has been around for a while now… its been a part of Visual Studio since the 2005 release . In case you don’t know, T4 can be used to automatically generate files based on templates. You create a text template, which is then transformed (interpreted) by Visual Studio into a working file. T4 can be used to create C# code files, and indeed it forms the basis of the current scaffolding templates you have probably used when creating ASP .NET web applications…

Continue reading

Resilient Connection for Entity Framework 6

05 February, 2014.

Disclaimer : I cannot take a shred of credit for the code you are about to see. I simply went over to the Entity Framework source code repository on CodePlex , copied some code, butchered it, hammered it, and rolled it until it fitted nicely into the slot that was my requirements. Please direct your thanks to the Entity Framework team . I had a requirement whereby I needed a resilient Entity Framework connection, and by that I mean a connection that could retry a certain number of times…

Continue reading

Check TFS Online service status using C#

10 January, 2014.

If you use TFS Online you may have experienced some unexpected downtime over the last few months. Whilst the service is getting better and better all the time, downtime is still an issue. I have written a little screen scraping tool based on the HTML Agility Pack that will scrape the service status page and report back the current status. Add the following class to your project; The usage for this code is as follows; I hope you find this little helper useful. Please leave a comment below.

Continue reading

C# Create a custom XML configuration section

21 December, 2013.

It is common when developing either Desktop or Web based applications to need to persist settings in an easily updateable location. Developers often choose to add normal application settings in the form of key value pairs, as shown below, and this is a great approach when you only have a small number of settings. However, as your applications configuration becomes more complicated, this approach soon becomes hard for the developer and end user alike. This blog post looks at how you can create a…

Continue reading

How to create a new Outlook 2013 Email using C# in 3 simple steps

14 December, 2013.

It has traditionally been quite painful to interact with any part of the Microsoft Office product family from a C# application, but thanks to the introduction of dynamics and optional parameters over recent years, the process has dramatically improved. Step 1 - Prerequisites and Assembly References Before doing anything, it is important to note that you must have Microsoft Office 2013 installed for this to work. Seems obvious, but, its still worth mentioning. You also need two references…

Continue reading

In the spotlight - Demystifying IQueryable (Entity Framework 6)

24 November, 2013.

I’m not afraid to admit it. I can’t be the only one who has been confused by IQueryable when working with Entity Framework or LINQ to SQL. When using the web to research the topic, I have seen many conflicting blog posts and contradicting answers on StackOverflow. I have read various posts on MSDN about the subject, and I find them difficult to understand and incomplete. I seek to at least attempt to clarify the subject a little with this post. Entity Framework has become such a powerful…

Continue reading

Exploring logging in Entity Framework 6

13 November, 2013.

A project I am working on at the minute was recently upgraded to use Entity Framework 6.0.1 . There were many reasons why we wanted to do this, but one of the most compelling reasons was the new logging (SQL Interception) functionality that ships out of the box. A colleague demonstrated this new functionality and I immediately noticed a problem (limitation if you will) that I foresaw becoming a bigger issue when the time comes to performance tune the application. Implementing simple logging…

Continue reading

Effectively debugging WPF MVVM applications

13 September, 2013.

Being able to effectively debug any application is key to the success of an application. Every developer, no matter how good they are or how long they have been writing code, introduces bugs. Debugging is the process of identifying bugs and removing them. WPF applications can be particularly hard to debug, if you are not using the correct tools. This post attempts to help you utilise various free tools to help simplify the debugging process. Binding Errors Binding errors are very common in all…

Continue reading

How to fix 'Project failed to build' error when adding an Entity Framework migration (add-migration)

06 September, 2013.

I recently spent several hours tearing my hair out try to figure out why Entity Framework was preventing me from adding a migration to my project. I made some very simple changes to my existing model (I’m developing using the Entity Framework Code First approach) and when trying to add a migration, I was presented with the following error message; But how can this be? For you see, my project does build… My project not only builds, but it runs and works just fine … (up until the point where…

Continue reading

C# Writing unit tests with NUnit and Moq

30 August, 2013.

I’m assuming that you have seen the light, and that you are sold on the benefits (and indeed the drawbacks) of unit testing. I have read literally dozens of blog posts, watched many videos, and read several books about unit testing and the various approaches and frameworks. I have worked with several of these frameworks in both small and enterprise level applications, and I have found that (in my opinion at least) NUnit and Moq are great for helping to get simple tests written quickly and to a…

Continue reading

How to write more efficient and maintainable C# code

23 August, 2013.

When you are writing your C# applications, you are aware that you want to make your applications as efficient, maintainable, and as scalable as possible. These are some great keywords that look great on blog posts like this, but how do you actually set about achieving this goal? We will look at some simply guidelines which try to make this goal more achievable. Write single purpose methods Your methods should have a single purpose… a single task to perform. For example, a single method should…

Continue reading

WPF Entity Framework Code First migrations

16 August, 2013.

Recently I wrote a WPF application using Entity Framework Code First and released it into the wild. Shortly after, the business requirements changed and I had to make changes to the model. Thus, I was introduced to Code First migrations. I did a lot of research on Code First migrations and could only really find the most simple of examples, and almost robotic documentation on MSDN that provided minimal help. This post aims to provide a clearer view on migrations, and how to properly create…

Continue reading

Using StructureMap setter injection to inject repositories into your View Model

09 August, 2013.

This short post is a follow on from an earlier series of posts that discussed how to achieve MVVM purity in WPF using Structure Map. You may want to check out those posts first before reading this post. However, if you just generally want to know how to do setter injection using Structure Map, you are in the right place! A Scenario It is very common in modern applications to have a repository that does the job of retrieving and saving data from/to your database. Your repository may look like…

Continue reading

Getting started with SQLite and C#

07 August, 2013.

SQLite is a powerful, extremely light-weight transactional SQL database provider. SQLite is free and open source, and it does not require any server processes to handle it. SQLite is cross platform and runs on multiple architectures, making it ideal for use on the major operating systems such as Windows, Mac OS, Linux, and even lower powered devices such as PDAs, MP3 players etc. Prerequisites It is possible to get up and running with SQLite quickly in C#, by adding the System.Data.SQLite…

Continue reading

WPF MVVM IoC containers - Part 2 of 2

02 August, 2013.

The ultimate goal of MVVM is to achieve true separation of concerns, meaning that the various elements of your project know nothing about each other. It’s virtually impossible to achieve this in an elegant way in WPF without some outside help. You are reading the second part of this blog post where we will discuss how to use an IoC container properly in a WPF MVVM application. It is assumed you have either read the first post, or are familiar with the concept of IoC/dependency injection with…

Continue reading

Consuming a WCF service without adding a Service Reference

29 July, 2013.

A problem I have had with WCF since I first discovered it a few years ago is related to Service References. The fact that I have to rely on the ServiceModel Metadata Utility Tool (Svcutil.exe) to generate a proxy class on my behalf left a sour taste in my mouth. A good solution would be for me to be able to write code myself to take this pain away. A Simple Approach One approach to do this is to take over the responsibility of creating bindings and endpoints yourself in your consuming…

Continue reading

WPF MVVM IoC containers - Part 1 of 2

26 July, 2013.

The ultimate goal of MVVM is to achieve true separation of concerns, meaning that the various elements of your project know nothing about each other. It’s virtually impossible to achieve this in an elegant way in WPF without some outside help. For example, how to you tie the view model to a view? Here are some ideas that spring to mind; 1) You could set the data context directly in the view; You could set the data context in the views code behind file; Or you could set the data context when the…

Continue reading

Entity Framework Code First In 15 Minutes

12 July, 2013.

Entity Framework is an Object Relational Mapper (ORM), created by Microsoft and built on top of the popular ADO.NET framework. Entity framework was first released in the second half of 2008. In 2011, with the release of version 4.1, a new feature was introduced… known as “Code First”. Code first allows us to write code without having to care (to a certain extent) about the make-up of the database, its tables and their relationships. The idea is that Entity Framework figures all this out for us…

Continue reading

WPF MVVM For WinForms Devs - Part 5/5

06 July, 2013.

The purpose of this series of tutorials is to introduce the Model-View-ViewModel (MVVM) design pattern, and look at how to quickly and simply implement it in a Windows Presentation Foundation (WPF) application. This series is targeted at developers of all levels, but especially at developers who are looking to make the transition from Windows Forms to WPF. This final part of the series assumes you have at least read the previous parts, or have a good working knowledge of WPF and the MVVM design…

Continue reading

WPF MVVM For WinForms Devs - Part 4/5

29 June, 2013.

The purpose of this series of tutorials is to introduce the Model-View-ViewModel (MVVM) design pattern, and look at how to correctly implement it in an Windows Presentation Foundation (WPF) application. This series is targeted at developers of all levels, but especially at developers who are looking to make the transition from Windows Forms to WPF. Events in Windows Forms applications Assuming you have come from a Windows Forms background, regardless of what design patterns (if any?) you used…

Continue reading

WPF MVVM For WinForms Devs - Part 3/5

22 June, 2013.

The purpose of this series of tutorials is to introduce the Model-View-ViewModel (MVVM) design pattern, and look at how to correctly implement it in a Windows Presentation Foundation (WPF) application. This series is targeted at developers of all levels, but especially at developers who are looking to make the transition from Windows Forms to WPF. Capturing user input In the previous walkthrough we; set up our view model, bound it to the view, and added a property for our model class. We then…

Continue reading

WPF MVVM For WinForms Devs - Part 2/5

15 June, 2013.

The purpose of this series of tutorials is to introduce the WPF MVVM (Model-View-ViewModel) design pattern, and look at how to correctly implement it in an Windows Presentation Foundation (WPF) application. This series is targeted at developers of all levels, but especially at developers who are looking to make the transition from Windows Forms to WPF. How to quickly implement MVVM in your C# WPF application Start by firing up an instance of your favourite version of Visual Studio (free or paid…

Continue reading

WPF MVVM For WinForms Devs - Part 1/5

08 June, 2013.

The purpose of this series of tutorials is to introduce the WPF MVVM (Model-View-ViewModel) design pattern, and look at how to quickly and simply implement it in a Windows Presentation Foundation (WPF) application. This series is targeted at developers of all levels, but especially at developers who are looking to make the transition from Windows Forms to WPF. The first part of this series will focus on; What exactly is MVVM, and why should I care? What problem does the MVVM design pattern…

Continue reading