Skip to content

Instantly share code, notes, and snippets.

@grumpydev
Last active December 22, 2015 17:59
Show Gist options
  • Save grumpydev/6509962 to your computer and use it in GitHub Desktop.
Save grumpydev/6509962 to your computer and use it in GitHub Desktop.
Nancy asp.net hosting katana startup test
namespace AutoStartupTest
{
using Owin;
public interface INancyOwinStartup
{
void Configuration(IAppBuilder builder);
}
}
namespace AutoStartupTest
{
using System;
using System.Linq;
using Nancy.Bootstrapper;
using Owin;
public class NancyAutoStartup
{
public void Configuration(IAppBuilder builder)
{
var userConfigs = GetUserConfigs();
if (userConfigs.Length > 1)
{
throw new InvalidOperationException(string.Format("Discovered {0} implementations of INancyOwinStartup ({1}), unable to continue.", userConfigs.Length, userConfigs.Select(t => t.FullName).Aggregate((n1, n2) => n1 + ", " + n2)));
}
if (userConfigs.Length == 0)
{
this.BuildDefaultConfig(builder);
}
else
{
this.BuildUserConfig(builder, userConfigs[0]);
}
}
private static Type[] GetUserConfigs()
{
return AppDomainAssemblyTypeScanner.TypesOf<INancyOwinStartup>().ToArray();
}
private void BuildDefaultConfig(IAppBuilder builder)
{
builder.UseNancy();
}
private void BuildUserConfig(IAppBuilder builder, Type userConfigType)
{
var userConfig = ((INancyOwinStartup)Activator.CreateInstance(userConfigType));
userConfig.Configuration(builder);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment