C# MD5 Hash

< 1 min read Quick script for generating an MD5 Hash

private string ComputeHash(string input)
{
  using (var md5 = System.Security.Cryptography.MD5.Create())
  {
    var data = md5.ComputeHash(Encoding.UTF8.GetBytes(input));
    var sb = new StringBuilder();
    foreach (var c in data) {
        sb.Append(c.ToString("x2"));
    }
    return sb.ToString();
  }
}

RavenDB Load Balancing

< 1 min read Somewhat counter-intuitive, this behavior is set at the client level, not the server. When servers are setup for replication, they create system documents of the other servers involved in replication. When the client accesses the primary server, it downloads and caches the replication information, so the client can “fail over” properly.
To set this up, you need to assign the FailoverBehavior convention in your DocumentStore.

RavenDB Lucene Query

< 1 min read How to build a Lucene query using extension methods, and not have the request go out until ToList() is called.

private List GetMembers(string nickname)
{
  var query = DocumentSession.Advanced.LuceneQuery(AllMembersIndex.Name);
  // Search for nickname
  if (!nickname.IsNullOrWhiteSpace())
    query = query.Search("Nickname", nickname);
  // Execute query
  return query.ToList();
}

RavenDB Create static index

2 min read

/// 
/// Gets the document store.
/// 
/// The document store.
///
/// Do this only once per AppDomain load. It's very expensive.
private static IDocumentStore GetDocumentStore()
{
    // Create the DocumentStore (expensive operation).
    IDocumentStore documentStore = new DocumentStore
    {
        ConnectionStringName = "RavenDB",
        Credentials = System.Net.CredentialCache.DefaultNetworkCredentials // For "trusted connections": see comments at http://ravendb.net/docs/client-api/connecting-to-a-ravendb-datastore
    };
    // Read from and write to all servers.
    documentStore.Conventions.FailoverBehavior = FailoverBehavior.ReadFromAllServers
        | FailoverBehavior.AllowReadsFromSecondariesAndWritesToSecondaries;
    // Initialize the store (must be done before creating indexes)
    documentStore = documentStore.Initialize();
    // Create static indexes
    CreateRavenStaticIndexes(documentStore);
    // Return document store
    return documentStore;
}

Background tasks in MVC and IIS

2 min read As you might’ve noticed, keeping threads running after a request returns, for processing post operational tasks (such as performing analytics on a file that was uploaded, etc) don’t always complete in a web project.
There are several issues with spawning threads in the context of an ASP.NET project. Phil Haack’s post explains the issues in more detail. The following classes solve the problem of IIS killing threads before they complete.

Value types, reference types and practical uses

2 min read C# has two different types of variables: value types and reference types. While in C and C++ primitive types can contain values or references and certain complex types (arrays, objects) can only be used via reference, in C# the line between the two types is very clear. Numeric types(int, decimal, double, etc.), bool and structs access the values directly. Class, object, interface, delegate string and dynamic are only accessed and used via reference. Because of all the awkwardness with referencing and dereferencing in C and C++, C# uses the ‘ref’ keyword only for those cases where you want to modify a value type outside of your current scope.

If you’ve worked with C# a bit you’re probably used to always working with copies in any situation outside of simple assignment (=). If you loop over a collection, inside the loop you’re working with copies. If you pass something to a function, that something is a copy. However the difference between value types and reference types often means a copy isn’t always a copy. Knowing when this is not the case can help you spot troublesome bugs, and easily solve problems that might have taken you much longer.

Mapping database objects to models – the Lambda way

< 1 min read Updated with statements that can be executed or deferred.

// Example models
public class SomeModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List Items { get; set; }
}
public class SomeOtherModel
{
    public int Id { get; set; }
    public string Text { get; set; }
}
//Expression mapper - not executed
public Expr<Func> Map_SomeTable_SomeModel_Expr()
{
    return x => new SomeModel
    {
        Id = x.Id,
        Name = x.Name,
        Items = db.SomeOtherTable.Where(y => y.SomeTableId == x.Id).Select(Map_SomeOtherTable_SomeOtherModel_Expr()).ToList()
    }
}

Localization in c#

2 min read Localization in C# is very straightforward. C# comes with Resource files, which are great because they can be easily translated, and C# can switch between them automatically.

First, add a new Resource File to your project. In my case, I called it SiteFields.resx. This will be the default language file, which in this case is English.

Next, we create a Localized Display Name Attribute, so we can leverage our ViewModels properly, instead of relying on text.

public class LocalizedDisplayNameAttribute : DisplayNameAttribute
{
    public LocalizedDisplayNameAttribute(string resourceId) : base(GetMessageFromResource(resourceId))
    {
    }
    private static string GetMessageFromResource(string resourceId)
    {
        CultureInfo culture = null;
        if (HttpContext.Current.Session["CultureInfo"] != null)
            culture = (CultureInfo)HttpContext.Current.Session["CultureInfo"];
        else
            culture = CultureInfo.CurrentCulture;
        return Resources.SiteFields.ResourceManager.GetString(resourceId, culture);
    }
}

Domain Routes and using subdomains in MVC as routes

< 1 min read I came across this when trying to build a website that takes advantage of sub domains in a MVC project.
You can route subdomains in MVC to a particular controller, and specific actions.

In order to do this, you must make your website listen directly on the IP Address, and point a wildcard A record for all subdomains.
In IIS 7, under Bindings, add a binding for http or https, set the IP to the dedicated IP for the site, and leave the Host name empty.

In your DNS Server (GoDaddy or Windows DNS Server), add the following A records

Host: @ or (same as parent)
IP address: (your dedicated IP)
Host: *
IP address: (your dedicated IP)
NOTE: Not all DNS Servers have wildcard options

This will make your website listen for any subdomains on your site.

Reference the classes attached below, and add the following routes to your project

_routes.Add("BlogRouteProfile", new DomainRoute("{url}.example.com", "", new { controller = "Blog", action = "ViewBlog", url = "" }));
_routes.Add("BlogLatestPosts", new DomainRoute("{url}.example.com", "latest-posts", new { controller = "Blog", action = "BlogLatestPosts", url = "" }));
_routes.Add("BlogPost1", new DomainRoute("{url}.example.com", "rss", new { controller = "Blog", action = "BlogRss", url = "" }));
_routes.Add("BlogPost3", new DomainRoute("{url}.example.com", "pages/{pageName}", new { controller = "Blog", action = "BlogPageView", url = "" }));

In your controller, you can now define Actions, just as you normally would for a regular route.

public class BlogController : Controller
{
    public ActionResult BlogPageView(string url, string pageName)
    {
        return View();
    }
}

Accessing indesign server from C# via COM

2 min read I’ve done quite a bit of searching on InDesign Server, and according to the people at Adobe, C# isn’t “dynamic” enough for use with their API. But obviously, they were looking at Java documentation mislabeled C# or at some very old books. Anyway, a lot of things in their documentation are misleading, so I will attempt to get you started on the right path to using InDesign Server CS6 from C# via COM objects.

InDesign Server registers it’s COM objects when it starts up, and only makes them available in the context in which it was started up. I have yet to figure out why they do this, or how to get around it, but note that if you’re logged onto the computer with the same user twice and start it from one of them, the second instance can’t access the first, nor can it start its own InDesign instance. This information is very important if you’re trying to access it from a service or from within IIS.

Asuming everything went alright, you can now begin to create your project. You first need to add a COM reference to InDesignServer and then import

using InDesignServer;

Now to create the application and make a few calls.

InDesignServer.Application app;
InDesignServer.Document doc;
try
{
    app = new InDesignServer.Application();
    try
    {
        doc = app.Open(Path.Combine(@"C:temp", "inDesignDoc.indd"),
        InDesignServer.idOpenOptions.idDefault);
        doc.Export(InDesignServer.idExportFormat.idPDFType, Path.Combine(@"C:temp",
        "indesign.pdf"), Type.Missing, false, Type.Missing, false);
        doc.Close();
        doc = null;
    }
    catch {}
}
catch {}