Running Scheduled Console Apps with .Net Core and Docker

I am a big fan of .Net Core at the moment. Mostly because I can do development natively on my Mac and the fact I can run them in Docker containers (did I mention I love Docker?).

With .Net Core you can build any app you can currently build in vanilla .Net, such as console apps, Web APIs, MVC apps. As you know, console apps generally come in 2 flavours, run all the time and run on a schedule. Typically the scheduled console apps are just hooked up to Windows Task Scheduler and forgotten about (until they break).

Given .Net Core is supposed to be cross platform, what is the cross platform equivalent for Windows Task Scheduler? I had a look at building in a timer loop or using a framework such as FluentScheduler but this just felt like adding bulk to an otherwise small console application.

Continue reading

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

6 Steps to Speed Up WordPress and Improve Google Rankings

People think that Google is some mysterious organisation with a mythical algorithm that searches the web and gives you the answers you need. The reality is Google’s algorithm isn’t that difficult to decode. At least the intentions of it aren’t. I will be going into detail in a future post how Google ranks websites, for now I am going to focus on one aspect, page speed.

Google’s sole objective is to find the website that best answers your query. Google tracks which results you click on, how long you spent on the site and whether you find the site so interesting that you stick around. The results are the output of Google’s product and therefore bad results make Google look bad.

So if your site takes a long time to load, visitors will get bored of waiting and try another link. As a result your website moves down the rankings as Google doesn’t want to show slow pages to it’s users.

You could write the best content in the world but if your page takes 10 seconds to load, no one is going to read it.

Continue reading

How to Start a Blog in 15 minutes or less

I remember when I created my first website back in 2001. A very simple site with a few flash games and links to useful pages. Getting a website up and running then was a lot harder than it is today. I wish I had a guide like this to show me how to start a blog.

There were few resources for how to actually do it, WordPress was still a couple of years away from being developed and everything had to be set up manually. I actually bought a book, a physical book, as the Kindle hadn’t been invented yet. This took me through the steps on registering a domain and uploading my files via FTP to a webserver.

Things are a lot simpler these days with 1-Click installs, a wide variety of content systems to choose from and cheap web hosting. However, even though things are easier people still put off creating their own blog. I get asked all the time by people and fellow software developers how to start a blog. Many saying “I really should get round to doing that” but never do. A blog can be a great way to get exposure as a software developer, give back to the community and even make money.

So on that note, stop what you are doing and bookmark this page now. If you have half an hour spare, stay here and we will go through the steps now and you will have a website setup by the time you finish this post.

Continue reading

14 SQL Query Performance Tuning Tips

I am going to start this post with a bit of a disclaimer. At heart, I am a .NET developer and not a SQL expert so if there is anything below you disagree with, I welcome the constructive criticism in the comments! Hopefully these SQL query performance tuning tips will be helpful all the same.

These SQL tips and tricks have come from experience of optimising SQL queries over the last year. In several cases these changes have taken queries taking hours down to a few minutes or even seconds.

SQL optimisation is a bit of an art. What will work on one server and data set might not always work on another system, so please take the below with a pinch of salt and do your own analysis.

Cursors/Loops

Loops are a stable construct in any programmers tool box. SQL can do loops as well however they should be used with caution. In .NET we are used to looping over data. Dealing with data a row at a time can also make the logic simpler.

SQL has While loops and Cursors for this process. However, in most cases where I have seen cursors used they can be replaced with set based queries and joins. Loops and Cursors are fine for small one off tasks or batch operations but in my opinion, they have no place in a high performance systems.

If you are doing a large number of updates or inserts I would always recommend using a loop (see batching below).

Functions in the Where clause

Sometimes you need to transform the data before you can specify your filter criteria. This is sometimes done by using a function in the where clause. The most common place I have seen this is around dates. In some cases you aren’t bothered at what time something happened, just that it happened that day. So you will see something like this:

SELECT *
FROM dbo.SomeTable
WHERE CONVERT(DATE, CreatedDate) = '2017-01-10'

This is a problem. Especially if you are using user defined functions, as SQL can’t interpret what the value is going to be until after it calls the function. Dependent on your other criteria this could cause SQL to do a full table scan. It is therefore much better to use a date range in these cases.

SELECT *
FROM dbo.SomeTable
WHERE CreatedDate >= '2017-01-10' AND CreatedDate < '2017-01-11'

Doing the above makes it easy for SQL to interpret your query and therefore use the correct index. Note, I have used ‘2017-01-11’ instead of ‘2017-01-10 23:59:59’ as the later will miss off the last second of the day.

Continue reading

Why I switched from Windows to a Mac, and won’t go back

As a .NET developer I have spent most of my computing career with Microsoft at the helm. So here is my journey to this point and why I can’t see myself going back to Windows.

I started coding when I was 10, with a Windows 3.1 machine that my Dad had brought home from work (I assume they were already obsolete at this point). Since then I have used pretty much every Windows incarnation to date. With the revelation that the start menu made with Windows 95 and the stability brought from Windows 98. Windows ME (:shudder:), Windows 2000, XP, 8, 8.1 and now Windows 10.

To be honest, I quite like Windows 10 but the annoyances that existed with the previous versions are still there. Plus, I have had a few unfortunate circumstance over the last few years that have put me off Windows.

It started with Windows 8. The first version of Windows where I have had to Google how to do a shutdown. You can imagine my relief when they made the genius decision to put the shutdown button back on the start screen. However like many, I didn’t have the seamless upgrade to Windows 8.1 that I had hoped. I had some faith, that after Windows had installed the 170 updates I would be up and running with the new version.

Continue reading

Creating a mandatory Anti-Forgery token

One of the things I love about ASP.Net, is that a lot of the hard work that is required for a creating a secure website has already been done for you. It usually only takes a couple of lines of code to add these features in which means there are no excuses for missing off important security measures.

Anti-Forgery Token

One of these features is the Anti-Forgery token and it can be added to your MVC website with just 2 lines of code. So what is an anti-forgery token? As the name suggests it is a token to prevent forgery! In the same way that someone might forge a signature to pretend to be someone else, it is possible for a malicious person to forge a request to your website without the request coming from your website.

So how is this done I hear you say? Well lets say you have a form on your website for changing user details such as name and email address, and a hacker wanted to change these to something else.

The hacker could create a form on another website which matches the request your website is expecting and post to the same URL. The entire form could be in hidden fields and posted via an Ajax request on page load making it invisible to the user.

If the user is already logged in to your website when the other website posts the form, your website treats it as a valid request and will change the user details to whatever the hacker wants.

So how do we get around this?

Well in the same way that 2 factor authentication works on something you know and something you have. The anti-forgery token works as the something you have (sorry about the poor analogy). The server places a hidden field with a populated anti-forgery token into your form. When a request is made to your website, the server checks for the presence of the anti-forgery token and if it doesn’t exist or doesn’t match the expected value an exception occurs.

As the hackers malicious form doesn’t know what the Anti-Forgery token is the request fails.

Continue reading

Why can’t all programming books look like this?

I have read quite a lot of programming books over the years. A lot of them have been really useful in learning a new programming language and some of them have been less than helpful. Unfortunately, the one thing all these books had in common was how dull they were to read. It’s not necessarily the fault of the author, programming is more of a practical subject and reading pages on pages of code can get a bit dry after a while. Some books do try and add a bit of humour to break up the monotony but even this can get a bit annoying after a while (yes, I mean you Head First).

Continue reading

Problems with VirtualBox + Vagrant on Windows 10

I am big fan of Vagrant. I first discovered Vagrant when I was looking for ways of creating a development environment that I could transfer between various computers (I had a desktop and laptop I regularly worked on).  I even toyed with the idea of installing a Linux distro on a fast USB 3 stick to carry round. It was then, while in my search for the perfect development environment that I discovered Vagrant. However I haven’t got Vagrant on Windows 10 working until now.

Vagrant was working fine the last time I used it, mainly for WordPress theme development. I haven’t touched it for about 6 months now but after typing vagrant up and waiting I was soon greeted with this:

Continue reading

Best Practices for a RESTful API

Nowadays the web is powered by APIs. With applications being used on desktop and mobile, APIs are essential in allowing the code in backend systems to be reused. The most popular APIs from companies such as Facebook, Google, and Twitter use the RESTful API pattern.

Unlike other parts of your web site or app, your API should be designed to be used by programmers, like you. If you have ever used a badly designed API you will know how frustrating it can be to try and integrate with it. So what are some things you can do to make a good RESTful API.

Continue reading