Created
September 1, 2011 10:08
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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