Last active
December 22, 2015 17:59
-
-
Save grumpydev/6509962 to your computer and use it in GitHub Desktop.
Nancy asp.net hosting katana startup test
This file contains hidden or 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
| namespace AutoStartupTest | |
| { | |
| using Owin; | |
| public interface INancyOwinStartup | |
| { | |
| void Configuration(IAppBuilder builder); | |
| } | |
| } |
This file contains hidden or 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
| 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