Blog posts

  • WCF and ASP.NET Compatibility Mode

    December 19, 2012 | Tags: ASP.NET, Troubleshooting, WCF
    Recently I had a troublesome error when developing a web hosted WCF service. The solution relied on some legacy components from an ASP.NET website - that is some cases throw errors, seemingly at random.

    After alot of trouble shooting I noticed that the problem only occured when - after building the project - I made the first request through a WCF client. The legacy components would then return errors until the project had been rebuilt. If I after rebuilding made a request to a test page within the WCF-project, the legacy components worked like a charm.

    Although I've done my fair share of web services in the past, this was my first WCF project. And there's a major difference between standard ASP.NET webservices and WCF.

    By default calls to the WCF service is run outside of the ASP.NET pipeline, in something called "Mixed Transports Mode". That means that it will bypass the ASP.NET pipeline and you'll loose things like HttpContext. Some of which the legacy components in my project apparently required.

    The solution was to switch to "ASP.NET Compability Mode". That means that the WCF request is treated as any other ASP.NET request.

    This is done by setting aspNetCompatibilityEnabled to true in the web.config:
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />

    And also decorate the WCF service class with the AspNetCompatibilityRequirements attribute:
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]

    public class UserService : IUserService {

    To learn more about this I recommend the following post about ASP.NET Compability Mode.

  • WCF: Error 404.17 The requested content appears to be script in IIS 7

    December 12, 2012 | Tags: Web hosting, IIS, ASP.NET, Troubleshooting, WCF
    If you've developed a WCF service and try to host it under IIS7 you most likely will be met with an error message saying "Error 404.17 The requested content appears to be script" from the static file handler.

    In order to solve this you need to install WCF components. This is done with the ServiceModelReg tool (more information about the tool). Start a cmd-prompt in administrator mode and execute the following:

    cd "%WINDIR%\Microsoft.Net\Framework\v3.0\Windows Communication Foundation"
    ServiceModelReg -i

  • Create backup archive using RAR

    September 25, 2012 | Tags: Tools, Snippet
    Sometimes it's quite handy to be able to archive a project or folder into a date stamped archive without manually doing the job each and every time. Enter RAR and a bat-script. If you don't already have, download WinRAR from here.

    Mission brief

    So lets set up a bat-script to archive a folder and date stamp the archive. The mission brief is to backup all files within the D:\Projects\MyProject\Source\ folder, but exclude temporary files created by the ReSharper plugin.

    Creating the required files

    First we create a text file containing all the paths we want to archive. Notice that you don't need to specify sub folders as we'll use the recursive function when archiving. We'll put the files in the same folder which will act as a base when storing the paths, we'll use "D:\Projects\".


    Now we create another text file, but this time with the paths within the one specified above that we don't want included in the archive, i e the ReSharper plugin working folder. If you want to exclude any other folders or files, here is where you want to add them.


    Okay, lets create the actual bat file. You might want to change the path towards your WinRAR installation or the date stamp pattern. I use YYYYMMDDHHMM which gives (without the plus signs) year+month+day+hour+minute using two digits for each value (except year which uses four).

    @echo off
    "C:\Program Files\WinRAR\rar.exe" a -agYYYYMMDDHHMM "MyBackupFolder\MyProject-Backup-" -ep1 -r -m5 -x@"exclude.lst" @"filelist.lst"

    Explanation of the parameters

    Add files to archive

    Generate archive name using the current date

    Exclude base directory from names (we don't need to archive D:\Projects\, we're happy with archiving the content as MyProject\Source\...).

    Recurse subdirectories

    Set compression level (0-store...3-default...5-maximal) (we're using 5 in the example above)

    The above script will create an archive in "MyBackupFolder" called "MyProject-Backup-201209251907.rar".

    Final words

    That's it! Now you have a nice archiving script that you either can trigger from the bat file or why not set it up as a scheduled job?

  • C#: Get elapsed time as text from post date

    June 19, 2012 | Tags: C#, Snippet
    This little snippet returns the elapsed time in a textual form from a specific date. Either as "x days ago", "x hours ago" or "x minutes ago" (using singularis for "1 minute ago" etc). Suitable for timelines.

    public static string GetSpan(DateTime date) {
    TimeSpan time = DateTime.Now - date;

    if (time.Days > 0) {
    return time.Days + (time.Days > 1 ? " days " : " day ") + "ago";
    else if (time.Hours > 0) {
    return time.Hours + (time.Hours > 1 ? " hours " : " hour ") + "ago";

    return time.Minutes + (time.Minutes > 1 ? " minutes " : " minute ") + "ago"; ;

  • C#: Limit number of characters in text to a certain length

    June 19, 2012 | Tags: C#, Snippet
    Sometimes just cutting a too long string will do the job, but sometimes you might want a more content aware cut.

    This little snippet takes two parameters: the text to (if too long) truncate and the required max length. If the text is within the max length nothing is done, the text is returned as is. Otherwise the code will try to break at the last stop character or after last word (depending on how the sentence is structured).
    public static string LimitCharacters(string text, int length) {
    if(string.IsNullOrEmpty(text)) {
    return string.Empty;

    // If text in shorter or equal to length, just return it
    if (text.Length <= length) {
    return text;

    // Text is longer, so try to find out where to cut
    char[] delimiters = new char[] { ' ', '.', ',', ':', ';' };
    int index = text.LastIndexOfAny(delimiters, length - 3);

    if (index > (length / 2)) {
    return text.Substring(0, index) + "...";
    else {
    return text.Substring(0, length - 3) + "...";