Blog posts


  • 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\".

    filelist.lst
    D:\Projects\MyProject\Source\

    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.

    exlude.lst
    D:\Projects\MyProject\Source\_ReSharper.MyProject

    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).

    Backup.bat
    @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

    a
    Add files to archive

    -ag[format]
    Generate archive name using the current date

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

    r
    Recurse subdirectories

    m<0..5>
    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) + "...";
    }
    }

  • Unknown server tag 'asp:LinqDataSource'

    June 18, 2012 | Tags: ASP.NET, Database, C#, Troubleshooting, LINQ
    If you're getting an error saying that the asp:LinqDataSource is unknown, you most likely is missing the declaration in your web.config file. To fix this:

    1. Open your web.config file and locate the <controls> block.
    2. Add the following element: <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.UI.WebControls" tagprefix="asp"> into the <controls&gt; block.
    3. Reload the page and see it working :)