A repository of useful PowerShell scripts

I have had to generate a lot of new files lately, and because I am allergic to repetitive work, I looked for a way to dynamically generate and process files. And for my case, PowerShell scripting comes pretty close to what I needed. Just like a few of my other posts (the best of StackOverflow and Things I always have to Google, I decided it would be a good idea to centralize these scripts and share them with you. Most of these scripts were made for one specific case so they’re not generic - but upon request I’ll try to make these as generic as possible. This is going to be a post that I’ll hope to update on regular occasions, so make sure to pay a visit every now and then if you’re working with PowerShell too.

Read More

Custom properties in log4net logging

There are few developers who haven’t used log4net, it’s just one of those essential utilities that every self respecting professional should know of. In this post, I’ll talk about customizing the logging process with custom properties.

Read More

ASP.NET MVC and HTML5 Web Workers

Although the technology has been around for a while now, HTML5 Web Workers didn’t draw my attention until a few days ago. As I’m building a client facing SaaS application for my client, performance is crucial. Having worked with asynchronous methods in C# for years, I thought of using this concept in JavaScript. As it turns out, multithreading in Javascript isn’t really part of the package. You had to work with timeouts and callbacks to implement some kind of asynchronous architecture, but splitting some heavy work into multiple threads (and even background work for that matter) wasn’t really supported out of the box - until HTML5 Web Workers were introduced. In this article, I’ll explore the capabilities in two different ways in the context of a ASP.NET MVC 5 application. What I’m not going to cover is the theoretical aspects of web workers.

Read More

Best of Stack Overflow

Just as in my other post about topics I have to “re-Google” often, this post serves as a catalog of interesting Stack Overflow articles. Because the concept of browser favorites doesn’t work quite well in my world, this blog post does the job of keeping a simple and convenient list very well. To keep things as simple as possible, I have divided the post in categories, comparable to the tags in Stack Overflow. One thing I’ve noticed is the amount of JavaScript articles, which is a bit surprising since I’m rather a back-end kind of a guy.

Read More

Custom configuration sections in C#

This is the best article I’ve read about this topic. Very clear and lots of screenshots to show the correlation between the config file and the programatical equivalent. Only drawback is that the code samples can’t be copied pasted because the author took screenshots of the code snippets rather than inserting them as code excerpts.

Read More

Things I always have to Google

Every now and then, I have to Google for something that I’ve Googled before numerous times because I forgot. Because this is a time consuming activity, I’ll bundle them all into one post. In other words, this is a post that will be often updated.

Read More

Create lambda expressions on the fly in C#

For the last couple of weeks I have been working with client-side frameworks such as Kendo UI and Sencha ExtJS a lot. One of the requirements of my clients was server-side filtering, grouping and ordering of several grids and other components. The challenge here is that you don’t know exactly what information might be coming in: which field do you have to filter or sort, which direction, what operator do you have to use, etc. There is only one proper way to do this in C#: Expressions.

Read More

C# and the Strava Web API

For those eager to share their extraordinary bike rides (or other activities) from Strava, there’s an easy way to use Web API which I will explain in this post. A live working example of the Strava Web API can be seen here and here. The Strava Web API is rather easy to use for the most of it because there is little security involved in the process. First, you need to create an app on the Strava Developer site. Once you’ve registered your application, you’ll receive your Client ID and Client Secret for when you want to access your private data. For public data such as your (public) rides, you won’t need this as the data is already public. There’s one piece of data that you will need to identify your user account to the API: the public access key. Once you’ve got that, you’re ready to use the Web API. For instance, the following URL retrieves my last 200 activities:

Read More

New features in C# 6.0

With the release of Visual Studio 2015 last week, I thought it would be a good time to inspect the new features that come with the newest version of C#. Although C# 6.0 is certainly not a major release from a developer’s perspective, there are some items that every developer will use in the coming years. In this post, I’ll show some examples on how to use these new features. The Visual Studio 2015 (compatible with at least VS2013) solution with code samples can be downloaded here. The following topics will be covered in this post:

Read More

How to automatically load navigation properties in Entity Framework

Anyone who has worked with Entity Framework will have had to manage with loading related entities. I believe many developers will agree the way that system works is difficult to reconcile with design patterns such as the Repository pattern. In this post, I’ll provide one quick and easy way to automatically load all related properties from the database.

Read More

ASP.NET MVC fixing error 'Multiple types were found that match the controller named' when routing is not the problem

I just came across following error message while I was converting one of my ASP.NET MVC applications:

Multiple types were found that match the controller named 'Home'. 
This can happen if the route that services this request ('{controller}/{action}/{id}') does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter.

The request for 'Home' has found the following matching controllers:
hbulens.WebUI.POC.Controllers.HomeController
hbulens.POC.WebUI.HomeController

**Beschrijving: **Er is een onverwerkte uitzondering opgetreden tijdens het uitvoeren van de huidige webaanvraag. Raadpleeg de stacktracering voor meer informatie over deze fout en de oorsprong ervan in de code.

**Details van uitzondering: **System.InvalidOperationException: Multiple types were found that match the controller named 'Home'. This can happen if the route that services this request ('{controller}/{action}/{id}') does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter.

The request for 'Home' has found the following matching controllers:
hbulens.WebUI.POC.Controllers.HomeController
hbulens.POC.WebUI.HomeController

Normally, you would expect something is wrong with the routing engine, The error messages on this subject are quite clear:

[InvalidOperationException: Multiple types were found that match the controller named 'Home'. This can happen if the route that services this request ('{controller}/{action}/{id}') does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter.
Read More

How to prepare for Microsoft exam 70-483 programming in C#

Today I passed the 70-483 Programming in C# exam. As any other Microsoft exam, the passing score is 700 on a total score of 1000. I scored way above my expectations, so I’m very happy that my efforts are rewarded with a MCP certificate. I can highly recommended experienced consultants and developers to take this exam. For beginning programmers ( < 1 year) this exam will probably be too difficult as there are many topics in scope of the exam. In order to motivate anyone who wants to take this exam, here is how I experienced the exam.

Read More

SharePoint pattern for allowing unsafe updates

As I said in my last post, I see some pieces of (ugly) code recur far too often in SharePoint. Probably most pervasive is the one to allow and not allow unsafe updates. In this post, I’ll provide a proper solution for this matter. The code excerpt that will be familiar to many of you:

Read More

Managing shared information with IDisposable in C# and SharePoint

Every once in a while you’ll have a situation in which you temporarily have to use different (shared) settings and reset them if all work is done. An example of this is often seen in SharePoint, for instance disabling events when updating items in event receivers, or allowing unsafe updates. A workaround that I have seen a lot is that developers add a TRY/CATCH/FINAL block for each method they want to reset some objects. In this post, I’ll talk about an alternative way of achieving this behavior by the implementation if the IDisposable interface. For many SharePoint developers, these two code excerpts will look quite familiar:

Read More

Writing the output from console applications to text file

Console applications provide a quick and easy way (and also a little bit dirty) to test or fix parts of your application. It enables you to focus on the business logic rather than the GUI. Sometimes the output to the window is quite large - larger than the console window can show. In those cases you want to be able to persist the output to text files. In this post, I’ll show you how to do this. It is really easy to achieve this, just follow these steps sequentially. Note: you can tailor this process to the way you want to, this is merely a possible way how to do it. If you are more comfortable of writing code, that is equally as well but it’ll require more effort. Nonetheless, here’s how you can do it:

Read More

Item-based exception handling in LINQ

Last week I rewrote some of my client’s code after having discovered a number of bugs. More specifically, a lot of errors came up in LINQ methods such as Where and Select. In order to capture what exactly was going wrong there, I have added a new extension method to the IEnumerable interface, allowing item-based exception handling on collections and LINQ statements.

Read More

Developing timerjobs in C# with Quartz.NET

At some point, any developer will have to write some code that must be executed at regular intervals. In SharePoint for instance, timerjobs were (!) very easy to create. In .NET, you could choose between scheduled tasks and windows services. While I was doing research on the ideal replacement for timerjobs in the new SharePoint app model, I came across Quartz.NET. In this post, I will show you which code you need to create timerjobs Before you can use Quartz.NET, you need to download their assembly. In NuGet, this is very easy. In the package manager console, you execute this command:

Read More

Replacing file extensions from uppercase to lowercase

Here’s a very simple utility that recursively finds uppercase file extensions and makes those who are lowercase. For once, it’s not written in C# but in good old DOS. If you paste this code in a CMD file, you’ll have a working program. If you execute this cmd, it will take the current folder as the root and it will process all files and subfolders.

Read More

Retrieving blog posts using C# and the Wordpress REST API

As part of my new professional website that I hope to release later this year, I thought it would be a good idea to dedicate a part of my site to this blog. Instead of choosing the rather traditional way of integrating blogs (e.g. embedded HTML snippets), I have chosen to take a look at its Web API. In this article, I will focus on one scenario: working with blog posts.

Read More

Tutorial on creating loosely coupled applications with MEF

When I first read about MEF (Managed Extensibility Framework), I thought it was a great concept to create plugin-based applications, allowing customers to customize some behavior of the application for instance. Putting the whole concept into reality is something different, so in this article I will give a brief overview of the steps to take to get started with MEF. I suggest to create three different assemblies to distinguish between their functionality:

Read More

C# trigger after database changes with SQLDependency

I found the following amazing article with some code that works without any modifications. With SQLDependency, you can listen to database changes with a predefined query. If something happens to this result set (INSERT/DELETE/UPDATE) an event will be triggered, allowing you to take action. Assume following console application:

Read More

Multi-tenancy with ASP.NET MVC and Entity Framework

Until recently I have never had the time to concentrate on cloud application development. All of the projects (non-SharePoint related) I have ever worked on were dedicated to only one customer, so the multi tenancy challenge never came up. Although my own website is hosted on Azure, I realized I had to create my own project to gain some knowledge on this area. There are many books and web pages available regarding cloud development, but for a very good reason none of those give really specific examples: it all depends on what you want to achieve. In this article, I will cover one scenario that is quickly to implement with the knowledge many .NET developers probably still have, which is developing a **ASP.NET MVC application with Entity Framework which ensures storage in one database with multiple schemas.

Read More

Parallel looping and returning 1 collection in C#

An interesting feature in .NET 4.5 is the ability to process collection in parallel. Along with the async/await feature, you can really improve the performance of your application with a few simple steps. Whereas both components are documented really well, I thought this would be a walk in the park. However, you should pay attention when you want to return a collection that was modified in the parallel block. If you don’t, you might be missing a few items in the list. You can easily prevent this from happening if you implement LOCKING, as the following example shows:

Read More

C# AND THE SPOTIFY WEB API - PART II (playlists)

In my previous post, I talked about the authorization process of the Spotify Web API. In this post, I will dig deeper into retrieving Spotify playlists. The authorization process that I explained in my previous post is necessary for retrieving Spotify playlist data. Here’s the code that gets a list of Spotify Playlists:

Read More

C# and the Spotify Web Api -part I (authorization)

Curious about the possibility of displaying Spotify playlists on my website, I investigated the Spotify Web API. Initially, I thought everything seemed quite easy, but then I got to the authentication and authorization section. As it turned out, it is not that simple but after trial and error, I got what I wanted. The result can be found here. Here’s what it looks like: playlists

Read More

Repository pattern in SharePoint

A few months ago, I had an assignment for a multinational beverage company to implement a publication system for their products across Europe. Along with a few other consultants and managers from another unit in my company, they had decided to go for a SharePoint solution as this seemed to meet all business requirements: centralized master data management with the ability to generate XML - and Word (or other human friendly) documents. I got on the project a few months later only to discover that the suggested approach could never work: the entire data model (entirely relational and normalized) was made with many SharePoint lists and the data was retrieved with JSOM. With a limited budget and a tight deadline, the best thing to do was to make a proper design with a solid architecture and minimal amount of code and complexity. The best alternative here was to build a custom SharePoint solution with a custom ORM layer. I considered LINQ 2 SharePoint but in this project where nothing was certain and everything changed rapidly, I quickly decided that a custom ORM layer was the most suitable. If I had the choice to start from scratch, I definitively would have chosen to go for the combination EF + BCS. As this MSDN article explains, the repository pattern is pretty much the way to go when it comes to building a generic, high quality and lightweight data access layer. This also counts for SharePoint solutions, as Chris O’Brien confirms in an old article. Although many articles cover this topic, I never was quite satisfied with the technical solution that was provided.

Read More