Skip to content

Instantly share code, notes, and snippets.

@rdingwall
Created September 1, 2011 10:08
Show Gist options
  • Save rdingwall/1185878 to your computer and use it in GitHub Desktop.
Save rdingwall/1185878 to your computer and use it in GitHub Desktop.
OpenRasta InMemoryHost that allows setting a custom resolver. Workaround for openrasta-stable issue 24
using System;
using System.Linq;
using System.Reflection;
using OpenRasta.Configuration;
using OpenRasta.DI;
// ReSharper disable CheckNamespace
namespace OpenRasta.Hosting.InMemory
// ReSharper restore CheckNamespace
{
// Hack to enable OpenRasta to use a custom IDependencyResolver.
// See https://github.com/openrasta/openrasta-stable/issues/24
public class InMemoryHostWithCustomResolver : InMemoryHost
{
// Can't pass the resolver via a constructor parameter because
// RaiseStart() is called before our ctor (in base ctor) - i.e.
// resolverInstance cannot be an instance field. So it has to be
// a static field initialized before InMemoryHost()'s ctor is called.
static IDependencyResolver resolverInstance;
public InMemoryHostWithCustomResolver(IConfigurationSource configuration) : base(configuration)
{
}
public static void SetResolver(IDependencyResolver resolver)
{
if (resolver == null) throw new ArgumentNullException("resolver");
resolverInstance = resolver;
}
protected override void RaiseStart()
{
if (resolverInstance == null)
throw new InvalidOperationException("Please call SetResolver() first.");
var resolverField = typeof(InMemoryHost)
.GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
.Single(f => f.FieldType.Equals(typeof(IDependencyResolver)));
resolverField.SetValue(this, resolverInstance);
base.RaiseStart();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment