My CodePad

Fewer words, more code!

Thread synchronisation with SemaphoreSlim and async/await — June 26, 2018

Thread synchronisation with SemaphoreSlim and async/await

Running a parallel foreach what makes calls to async methods is pretty straight forward, but what about if you want to synchronise thread access to a resource?That can be a bit tricky, so here is a solution with SemaphoreSlim:

Continue reading

Integration Testing in ASP.Net Core 2.0 — April 22, 2018
C#: Simple website stress tool — December 30, 2014
ASP.NET MVC 5: Custom AuthorizeAttribute for custom authentication — May 17, 2014
ASP.NET MVC 5: Secure your web app — March 17, 2014

ASP.NET MVC 5: Secure your web app

Securing your MVC app is one of the tricky things when migrating, as an engineer, from classic ASP.NET to ASP.NET MVC.

By default, access is allowed to every controller and action of your web app, which is potentially unsafe. As an architect, I am sure you would like to enforce the security of your app and don’t rely on whether a developer will actually remember to add the [Authorize] attribute to a sensitive controller or action.
Continue reading

C#: Programmatically “Run As Administrator” — February 18, 2014

C#: Programmatically “Run As Administrator”

A few days ago I wanted to write to hosts, a HOSTS file in system32 used by Microsoft TCP/IP for Windows. Since this file is protected, you can edit it in administrative mode only. This means the user has to right click it and select “Run as administrator” for your project to run correctly. Fortunately there is a solution. The following code will request admin access from the user, and rerun it’s self in admin mode:

Continue reading

C#: Basic Combinatorics — December 13, 2013
How to Minify JavaScript with C#, a Google PageSpeed suggestion — December 5, 2013

How to Minify JavaScript with C#, a Google PageSpeed suggestion

Google sais “compacting JavaScript code can save many bytes of data and speed up downloading, parsing, and execution time” and that is obviously true (you can read about it here. And although there are many tools to minify javascript, like JSMin and YUI Compressor, it’s quite time consuming (and boring) to have to run these tools for each change your Web Designer decides to do for every JS file in your project. Except that, non of these tools compacts JS in one file to minimize browser requests.

The solution I came up goes like this (in my project this code runs only once in every application boot by using HttpContext.Current.Cache)

Step 1
Add a new Web Form to your project and name it js.aspx

Continue reading

Export MS SQL Database schema with C# — November 18, 2013

Export MS SQL Database schema with C#

A few days ago, I was searching for a way to export MS SQL schema of all databases of a server for backup and migration purposes. Although I found some tools, what I needed was a way to get stored procedures, functions, views, triggers and everything even if it was encrypted.

Since I couldn’t find a way to automate this (e.g. run it every night to backup schema of my server), I thought it would be a nice weekend project.

You can read more about it, or download project from!

Continue reading

JSON to C# objects — November 11, 2013

JSON to C# objects

.NET natively supports JSON through System.Web.Script.Serialization.JavaScriptSerializer.
In shorthand, it goes like this:

using System.Web.Script.Serialization;
var obj = new JavaScriptSerializer().Deserialize({YOUR JSON STRING});//String to Object

This, will de-serialize your JSON string to a generic object, but going from this to get a value well within the structure of a JSON string is quite messy:

Continue reading