private void Configure(AzureServiceBusOwinServiceConfiguration config, Action<IAppBuilder> startup)
        {
            if (startup == null)
            {
                throw new ArgumentNullException("startup");
            }

            var options = new StartOptions();
            if (string.IsNullOrWhiteSpace(options.AppStartup))
            {
                // Populate AppStartup for use in host.AppName
                options.AppStartup = startup.Method.ReflectedType.FullName;
            }

            var testServerFactory = new AzureServiceBusOwinServerFactory(config);
            var services = ServicesFactory.Create();
            var engine = services.GetService<IHostingEngine>();
            var context = new StartContext(options)
            {
                ServerFactory = new ServerFactoryAdapter(testServerFactory),
                Startup = startup
            };
            _started = engine.Start(context);
            _next = testServerFactory.Invoke;
        }
        private static int DeterminePort(StartContext context)
        {
            StartOptions options = context.Options;
            IDictionary <string, string> settings = context.Options.Settings;

            if (options != null && options.Port.HasValue)
            {
                return(options.Port.Value);
            }

            string portString;
            int    port;

            if (settings != null &&
                settings.TryGetValue(Constants.SettingsPort, out portString) &&
                !string.IsNullOrWhiteSpace(portString) &&
                int.TryParse(portString, NumberStyles.Integer, CultureInfo.InvariantCulture, out port))
            {
                return(port);
            }

            portString = Environment.GetEnvironmentVariable(Constants.EnvPort, EnvironmentVariableTarget.Process);
            if (!string.IsNullOrWhiteSpace(portString) &&
                int.TryParse(portString, NumberStyles.Integer, CultureInfo.InvariantCulture, out port))
            {
                return(port);
            }

            return(Constants.DefaultPort);
        }
Beispiel #3
0
        private void ResolveApp(StartContext context)
        {
            context.Builder.Use(typeof(Encapsulate), context.EnvironmentData);

            if (context.App == null)
            {
                IList <string> errors = new List <string>();
                if (context.Startup == null)
                {
                    string appName = DetermineApplicationName(context);
                    context.Startup = _appLoader.Load(appName, errors);
                }
                if (context.Startup == null)
                {
                    throw new EntryPointNotFoundException(Resources.Exception_AppLoadFailure
                                                          + Environment.NewLine + " - " + string.Join(Environment.NewLine + " - ", errors));
                }
                // 把Startup中的内容加到AppBuiler中
                context.Startup(context.Builder);
            }
            else
            {
                context.Builder.Use(new Func <object, object>(_ => context.App));
            }
        }
Beispiel #4
0
        public IDisposable CreateApp(List<string> urls)
        {
            var services = CreateServiceFactory();
            var engine = services.GetService<IHostingEngine>();

            var options = new StartOptions()
            {
                ServerFactory = "Microsoft.Owin.Host.HttpListener"
            };

            urls.ForEach(options.Urls.Add);

            var context = new StartContext(options) { Startup = BuildApp };


            try
            {
                return engine.Start(context);
            }
            catch (TargetInvocationException ex)
            {
                if (ex.InnerException == null)
                {
                    throw;
                }

                if (ex.InnerException is HttpListenerException)
                {
                    throw new PortInUseException("Port {0} is already in use, please ensure NzbDrone is not already running.", ex, _configFileProvider.Port);
                }

                throw ex.InnerException;
            }
        }
Beispiel #5
0
        private static string DetermineOwinServer(StartContext context)
        {
            StartOptions options = context.Options;
            IDictionary <string, string> settings = context.Options.Settings;

            string serverName = options.ServerFactory;

            if (!string.IsNullOrWhiteSpace(serverName))
            {
                return(serverName);
            }

            if (settings != null &&
                settings.TryGetValue(Constants.SettingsOwinServer, out serverName) &&
                !string.IsNullOrWhiteSpace(serverName))
            {
                return(serverName);
            }

            serverName = Environment.GetEnvironmentVariable(Constants.EnvOwnServer, EnvironmentVariableTarget.Process);
            if (!string.IsNullOrWhiteSpace(serverName))
            {
                return(serverName);
            }

            return(Constants.DefaultServer);
        }
Beispiel #6
0
        /// <summary>
        /// Initialize and start a web application.
        /// Major Steps:
        /// - Find and initialize the ServerFactory
        /// - Find and initialize the application
        /// - Start the server
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public IDisposable Start(StartContext context)
        {
            ResolveOutput(context);
            InitializeBuilder(context);
            EnableTracing(context);
            IDisposable disposablePipeline = EnableDisposing(context);

            ResolveServerFactory(context);
            InitializeServerFactory(context);
            ResolveApp(context);
            IDisposable disposableServer = StartServer(context);

            return(new Disposable(
                       () =>
            {
                try
                {
                    // first stop processing requests
                    disposableServer.Dispose();
                }
                finally
                {
                    // then inform the pipeline of app shutdown
                    disposablePipeline.Dispose();
                    // Flush logs
                    context.TraceOutput.Flush();
                }
            }));
        }
Beispiel #7
0
        private void Configure(Action<IAppBuilder> startup, StartOptions options = null)
        {
            if (startup == null)
            {
                throw new ArgumentNullException("startup");
            }

            options = options ?? new StartOptions();
            if (string.IsNullOrWhiteSpace(options.AppStartup))
            {
                // Populate AppStartup for use in host.AppName
                options.AppStartup = startup.Method.ReflectedType.FullName;
            }

            var testServerFactory = new OwinEmbeddedServerFactory();
            IServiceProvider services = ServicesFactory.Create(
                serviceProvider => serviceProvider.AddInstance<ITraceOutputFactory>(new NullTraceOutputFactory())
                );
            var engine = services.GetService<IHostingEngine>();
            var context = new StartContext(options)
            {
                ServerFactory = new ServerFactoryAdapter(testServerFactory),
                Startup = startup
            };
            _started = engine.Start(context);
            _next = testServerFactory.Invoke;
        }
        public IDisposable Start(StartContext context)
        {
            ResolveOutput(context);
            InitializeBuilder(context);
            EnableTracing(context);
            IDisposable disposablePipeline = EnableDisposing(context);
            ResolveServerFactory(context);
            InitializeServerFactory(context);
            ResolveApp(context);
            IDisposable disposableServer = StartServer(context);

            return new Disposable(
                () =>
                {
                    try
                    {
                        // first stop processing requests
                        disposableServer.Dispose();
                    }
                    finally
                    {
                        // then inform the pipeline of app shutdown
                        disposablePipeline.Dispose();
                    }
                });
        }
Beispiel #9
0
        private static IDisposable EnableDisposing(StartContext context)
        {
            var cts = new CancellationTokenSource();

            context.Builder.Properties[Constants.HostOnAppDisposing] = cts.Token;
            context.EnvironmentData.Add(new KeyValuePair <string, object>(Constants.HostOnAppDisposing, cts.Token));
            return(new Disposable(() => cts.Cancel(false)));
        }
Beispiel #10
0
        private void ResolveOutput(StartContext context)
        {
            if (context.TraceOutput == null)
            {
                string traceoutput;
                context.Options.Settings.TryGetValue("traceoutput", out traceoutput);
                context.TraceOutput = _traceOutputFactory.Create(traceoutput);
            }

            context.EnvironmentData.Add(new KeyValuePair <string, object>(Constants.HostTraceOutput, context.TraceOutput));
        }
Beispiel #11
0
        private static int DeterminePort(StartContext context)
        {
            int port;

            if (!TryDetermineCustomPort(context.Options, out port))
            {
                port = DefaultPort;
            }

            return(port);
        }
Beispiel #12
0
        private void InitializeBuilder(StartContext context)
        {
            if (context.Builder == null)
            {
                context.Builder = _appBuilderFactory.Create();
            }

            var addresses = new List <IDictionary <string, object> >();

            foreach (var url in context.Options.Urls)
            {
                string scheme;
                string host;
                string port;
                string path;
                if (DeconstructUrl(url, out scheme, out host, out port, out path))
                {
                    addresses.Add(new Dictionary <string, object>
                    {
                        { Constants.Scheme, scheme },
                        { Constants.Host, host },
                        { Constants.Port, port.ToString(CultureInfo.InvariantCulture) },
                        { Constants.Path, path },
                    });
                }
            }

            if (addresses.Count == 0)
            {
                int port = DeterminePort(context);
                addresses.Add(new Dictionary <string, object>
                {
                    { Constants.Port, port.ToString(CultureInfo.InvariantCulture) },
                });
            }

            context.Builder.Properties[Constants.HostAddresses] = addresses;

            if (!string.IsNullOrWhiteSpace(context.Options.AppStartup))
            {
                context.Builder.Properties[Constants.HostAppName] = context.Options.AppStartup;
                context.EnvironmentData.Add(new KeyValuePair <string, object>(Constants.HostAppName, context.Options.AppStartup));
            }

            // This key lets us know the app was launched from Visual Studio.
            string vsVersion = Environment.GetEnvironmentVariable("VisualStudioVersion");

            if (!string.IsNullOrWhiteSpace(vsVersion))
            {
                context.Builder.Properties[Constants.HostAppMode] = Constants.AppModeDevelopment;
                context.EnvironmentData.Add(new KeyValuePair <string, object>(Constants.HostAppMode, Constants.AppModeDevelopment));
            }
        }
Beispiel #13
0
        public void Open(Action<IAppBuilder> startup, StartOptions options)
        {
            var testAppLoaderProvider = new TestAppLoaderFactory(startup);
            var testServerFactory = new TestServerFactory();

            IServiceProvider services = ServicesFactory.Create(container => container.AddInstance<IAppLoaderFactory>(testAppLoaderProvider));
            var engine = services.GetService<IHostingEngine>();
            var context = new StartContext(options ?? new StartOptions());
            context.ServerFactory = new ServerFactoryAdapter(testServerFactory);
            _started = engine.Start(context);
            _invoke = testServerFactory.Invoke;
        }
        public virtual IDisposable Start(StartOptions options)
        {
            StartContext context = new StartContext(options);

            IServiceProvider services = ServicesFactory.Create(context.Options.Settings);

            IHostingEngine engine = services.GetService<IHostingEngine>();

            IDisposable disposable = engine.Start(context);

            return new Disposable(disposable.Dispose);
        }
        public virtual void Start(StartOptions options)
        {
            var context = new StartContext(options);

            IServiceProvider services = ServicesFactory.Create(context.Options.Settings);

            var engine = services.GetService<IHostingEngine>();

            _runningApp = engine.Start(context);

            _lease = (ILease)RemotingServices.GetLifetimeService(this);
            _lease.Register(this);
        }
Beispiel #16
0
        private void ResolveServerFactory(StartContext context)
        {
            if (context.ServerFactory != null)
            {
                return;
            }

            string serverName = DetermineOwinServer(context);

            context.ServerFactory = _serverFactoryLoader.Load(serverName);
            if (context.ServerFactory == null)
            {
                throw new MissingMemberException(string.Format(CultureInfo.InvariantCulture, Resources.Exception_ServerNotFound, serverName));
            }
        }
        private static void EnableTracing(StartContext context)
        {
            // string etwGuid = "CB50EAF9-025E-4CFB-A918-ED0F7C0CD0FA";
            // EventProviderTraceListener etwListener = new EventProviderTraceListener(etwGuid, "HostingEtwListener", "::");
            var textListener = new TextWriterTraceListener(context.TraceOutput, "HostingTraceListener");

            Trace.Listeners.Add(textListener);
            // Trace.Listeners.Add(etwListener);

            var source = new TraceSource("HostingTraceSource", SourceLevels.All);

            source.Listeners.Add(textListener);
            // source.Listeners.Add(etwListener);

            context.Builder.Properties[Constants.HostTraceOutput] = context.TraceOutput;
            context.Builder.Properties[Constants.HostTraceSource] = source;
        }
        public void InitializeAndCreateShouldBeCalledWithProperties()
        {
            var serverFactoryAlpha = new ServerFactoryAlpha();
            var startInfo = new StartContext(new StartOptions());
            startInfo.ServerFactory = new ServerFactoryAdapter(serverFactoryAlpha);
            startInfo.App = new AppFunc(env => Task.FromResult(0));

            var engine = ServicesFactory.Create().GetService<IHostingEngine>();

            serverFactoryAlpha.InitializeCalled.ShouldBe(false);
            serverFactoryAlpha.CreateCalled.ShouldBe(false);
            IDisposable server = engine.Start(startInfo);

            serverFactoryAlpha.InitializeCalled.ShouldBe(true);
            serverFactoryAlpha.CreateCalled.ShouldBe(true);
            serverFactoryAlpha.InitializeProperties.ShouldBeSameAs(serverFactoryAlpha.CreateProperties);
            server.Dispose();
        }
Beispiel #19
0
        private static string DetermineApplicationName(StartContext context)
        {
            StartOptions options = context.Options;
            IDictionary <string, string> settings = context.Options.Settings;

            if (options != null && !string.IsNullOrWhiteSpace(options.AppStartup))
            {
                return(options.AppStartup);
            }

            string appName;

            if (settings.TryGetValue(Constants.SettingsOwinAppStartup, out appName) &&
                !string.IsNullOrWhiteSpace(appName))
            {
                return(appName);
            }

            return(null);
        }
Beispiel #20
0
        private void InitializeBuilder(StartContext context)
        {
            if (context.Builder == null)
            {
                context.Builder = _appBuilderFactory.Create();
            }

            var addresses = new List <IDictionary <string, object> >();

            foreach (string url in context.Options.Urls)
            {
                string scheme;
                string host;
                string port;
                string path;
                if (DeconstructUrl(url, out scheme, out host, out port, out path))
                {
                    addresses.Add(new Dictionary <string, object>
                    {
                        { Constants.Scheme, scheme },
                        { Constants.Host, host },
                        { Constants.Port, port.ToString(CultureInfo.InvariantCulture) },
                        { Constants.Path, path },
                    });
                }
            }

            if (addresses.Count == 0)
            {
                int port = DeterminePort(context);
                addresses.Add(new Dictionary <string, object>
                {
                    { Constants.Port, port.ToString(CultureInfo.InvariantCulture) },
                });
            }

            SignatureConversions.AddConversions(context.Builder);
            context.Builder.Properties[Constants.HostAddresses] = addresses;
            context.Builder.Properties[Constants.HostAppName]   = context.Options.AppStartup;
            context.EnvironmentData.Add(new KeyValuePair <string, object>(Constants.HostAppName, context.Options.AppStartup));
        }
Beispiel #21
0
        private void ResolveApp(StartContext context)
        {
            context.Builder.Use(typeof(Encapsulate), context.EnvironmentData);

            if (context.App == null)
            {
                if (context.Startup == null)
                {
                    string appName = DetermineApplicationName(context);
                    context.Startup = _appLoader.Load(appName);
                }
                if (context.Startup == null)
                {
                    throw new EntryPointNotFoundException(Resources.Exception_MissingApplicationEntryPoint);
                }
                context.Startup(context.Builder);
            }
            else
            {
                context.Builder.Use(new Func <object, object>(_ => context.App));
            }
        }
Beispiel #22
0
        /// <summary>
        /// Initialize and start a web application.
        /// Major Steps:
        /// - Find and initialize the ServerFactory
        /// - Find and initialize the application
        /// - Start the server
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public IDisposable Start(StartContext context)
        {
            ResolveOutput(context);
            // 实例化AppBuilder,并存到context.Builder中
            InitializeBuilder(context);
            // 添加写Logger的类
            EnableTracing(context);
            // 添加一个资源释放器,意义不明
            IDisposable disposablePipeline = EnableDisposing(context);

            // 实例化Service监听层,默认是 Constants.DefaultServer = Microsoft.Owin.Host.HttpListener
            // 可在context.Options.ServerFactory进行指定, 或 context.Options.Settings 中指定(Key = Constants.SettingsOwinServer = owin:Server)
            ResolveServerFactory(context);

            InitializeServerFactory(context);

            // 将StartUp中middlewave添加到管道中, 通过调用
            ResolveApp(context);

            // 开始HttpListener监听
            IDisposable disposableServer = StartServer(context);

            return(new Disposable(
                       () =>
            {
                try
                {
                    // first stop processing requests
                    disposableServer.Dispose();
                }
                finally
                {
                    // then inform the pipeline of app shutdown
                    disposablePipeline.Dispose();
                    // Flush logs
                    context.TraceOutput.Flush();
                }
            }));
        }
        public IDisposable Start(AspNetStarterProxy proxy, StartOptions options)
        {
            _proxy = proxy;
            try
            {
                HostingEnvironment.RegisterObject(this);
            }
            catch
            {
                // Notification not always supported
            }

            var context = new StartContext(options);

            IServiceProvider services = ServicesFactory.Create(context.Options.Settings);

            var builderFactory = services.GetService<IAppBuilderFactory>();
            context.Builder = new AppBuilderWrapper(builderFactory.Create());

            IHostingEngine engine = services.GetService<IHostingEngine>();

            return engine.Start(context);
        }
        private static int DeterminePort(StartContext context)
        {
            StartOptions options = context.Options;
            IDictionary<string, string> settings = context.Options.Settings;

            if (options != null && options.Port.HasValue)
            {
                return options.Port.Value;
            }

            string portString;
            int port;
            if (settings != null &&
                settings.TryGetValue(Constants.SettingsPort, out portString) &&
                !string.IsNullOrWhiteSpace(portString) &&
                int.TryParse(portString, NumberStyles.Integer, CultureInfo.InvariantCulture, out port))
            {
                return port;
            }

            portString = Environment.GetEnvironmentVariable(Constants.EnvPort, EnvironmentVariableTarget.Process);
            if (!string.IsNullOrWhiteSpace(portString) &&
                int.TryParse(portString, NumberStyles.Integer, CultureInfo.InvariantCulture, out port))
            {
                return port;
            }

            return Constants.DefaultPort;
        }
Beispiel #25
0
        public void CreateShouldBeProvidedWithAdaptedAppIfNeeded()
        {
            var serverFactoryBeta = new ServerFactoryBeta();
            var startInfo = new StartContext(new StartOptions());
            startInfo.ServerFactory = new ServerFactoryAdapter(serverFactoryBeta);
            startInfo.App = new AppFunc(env => TaskHelpers.Completed());

            var engine = ServicesFactory.Create().GetService<IHostingEngine>();
            serverFactoryBeta.CreateCalled.ShouldBe(false);
            IDisposable server = engine.Start(startInfo);
            serverFactoryBeta.CreateCalled.ShouldBe(true);
            server.Dispose();
        }
Beispiel #26
0
        public void PropertiesShouldHaveExpectedKeysFromHost()
        {
            var serverFactory = new ServerFactoryAlpha();
            var startInfo = new StartContext(new StartOptions());
            startInfo.ServerFactory = new ServerFactoryAdapter(serverFactory);
            startInfo.App = new AppFunc(env => TaskHelpers.Completed());

            var engine = ServicesFactory.Create().GetService<IHostingEngine>();
            serverFactory.InitializeCalled.ShouldBe(false);
            serverFactory.CreateCalled.ShouldBe(false);
            IDisposable server = engine.Start(startInfo);

            serverFactory.InitializeProperties.ShouldContainKey("host.TraceOutput");
            serverFactory.InitializeProperties.ShouldContainKey("host.Addresses");

            serverFactory.InitializeProperties["host.TraceOutput"].ShouldBeTypeOf<TextWriter>();
            serverFactory.InitializeProperties["host.Addresses"].ShouldBeTypeOf<IList<IDictionary<string, object>>>();

            server.Dispose();
        }
        private static void EnableTracing(StartContext context)
        {
            // string etwGuid = "CB50EAF9-025E-4CFB-A918-ED0F7C0CD0FA";
            // EventProviderTraceListener etwListener = new EventProviderTraceListener(etwGuid, "HostingEtwListener", "::");
            var textListener = new TextWriterTraceListener(context.TraceOutput, "HostingTraceListener");

            Trace.Listeners.Add(textListener);
            // Trace.Listeners.Add(etwListener);

            var source = new TraceSource("HostingTraceSource", SourceLevels.All);
            source.Listeners.Add(textListener);
            // source.Listeners.Add(etwListener);

            context.Builder.Properties[Constants.HostTraceOutput] = context.TraceOutput;
            context.Builder.Properties[Constants.HostTraceSource] = source;
        }
Beispiel #28
0
        private void InitializeBuilder(StartContext context)
        {
            if (context.Builder == null)
            {
                context.Builder = _appBuilderFactory.Create();
            }

            var addresses = new List<IDictionary<string, object>>();

            foreach (var url in context.Options.Urls)
            {
                string scheme;
                string host;
                string port;
                string path;
                if (DeconstructUrl(url, out scheme, out host, out port, out path))
                {
                    addresses.Add(new Dictionary<string, object>
                    {
                        { Constants.Scheme, scheme },
                        { Constants.Host, host },
                        { Constants.Port, port.ToString(CultureInfo.InvariantCulture) },
                        { Constants.Path, path },
                    });
                }
            }

            if (addresses.Count == 0)
            {
                int port = DeterminePort(context);
                addresses.Add(new Dictionary<string, object>
                {
                    { Constants.Port, port.ToString(CultureInfo.InvariantCulture) },
                });
            }

            context.Builder.Properties[Constants.HostAddresses] = addresses;

            if (!string.IsNullOrWhiteSpace(context.Options.AppStartup))
            {
                context.Builder.Properties[Constants.HostAppName] = context.Options.AppStartup;
                context.EnvironmentData.Add(new KeyValuePair<string, object>(Constants.HostAppName, context.Options.AppStartup));
            }

            // This key lets us know the app was launched from Visual Studio.
            string vsVersion = Environment.GetEnvironmentVariable("VisualStudioVersion");
            if (!string.IsNullOrWhiteSpace(vsVersion))
            {
                context.Builder.Properties[Constants.HostAppMode] = Constants.AppModeDevelopment;
                context.EnvironmentData.Add(new KeyValuePair<string, object>(Constants.HostAppMode, Constants.AppModeDevelopment));
            }
        }
Beispiel #29
0
 private static void InitializeServerFactory(StartContext context)
 {
     context.ServerFactory.Initialize(context.Builder);
 }
        private void ResolveServerFactory(StartContext context)
        {
            if (context.ServerFactory != null)
            {
                return;
            }

            string serverName = DetermineOwinServer(context);
            context.ServerFactory = _serverFactoryLoader.Load(serverName);
            if (context.ServerFactory == null)
            {
                throw new MissingMemberException(string.Format(CultureInfo.InvariantCulture, Resources.Exception_ServerNotFound, serverName));
            }
        }
        private void ResolveOutput(StartContext context)
        {
            if (context.TraceOutput == null)
            {
                string traceoutput;
                context.Options.Settings.TryGetValue("traceoutput", out traceoutput);
                context.TraceOutput = _traceOutputFactory.Create(traceoutput);
            }

            context.EnvironmentData.Add(new KeyValuePair<string, object>(Constants.HostTraceOutput, context.TraceOutput));
        }
        private void ResolveApp(StartContext context)
        {
            context.Builder.Use(typeof(Encapsulate), context.EnvironmentData);

            if (context.App == null)
            {
                if (context.Startup == null)
                {
                    string appName = DetermineApplicationName(context);
                    context.Startup = _appLoader.Load(appName);
                }
                if (context.Startup == null)
                {
                    throw new EntryPointNotFoundException(Resources.Exception_MissingApplicationEntryPoint);
                }
                context.Startup(context.Builder);
            }
            else
            {
                context.Builder.Use(new Func<object, object>(_ => context.App));
            }
        }
        private void InitializeBuilder(StartContext context)
        {
            if (context.Builder == null)
            {
                context.Builder = _appBuilderFactory.Create();
            }

            var addresses = new List<IDictionary<string, object>>();

            foreach (string url in context.Options.Urls)
            {
                string scheme;
                string host;
                string port;
                string path;
                if (DeconstructUrl(url, out scheme, out host, out port, out path))
                {
                    addresses.Add(new Dictionary<string, object>
                    {
                        { Constants.Scheme, scheme },
                        { Constants.Host, host },
                        { Constants.Port, port.ToString(CultureInfo.InvariantCulture) },
                        { Constants.Path, path },
                    });
                }
            }

            if (addresses.Count == 0)
            {
                int port = DeterminePort(context);
                addresses.Add(new Dictionary<string, object>
                {
                    { Constants.Port, port.ToString(CultureInfo.InvariantCulture) },
                });
            }

            SignatureConversions.AddConversions(context.Builder);
            context.Builder.Properties[Constants.HostAddresses] = addresses;
            context.Builder.Properties[Constants.HostAppName] = context.Options.AppStartup;
            context.EnvironmentData.Add(new KeyValuePair<string, object>(Constants.HostAppName, context.Options.AppStartup));
        }
 private static IDisposable StartServer(StartContext context)
 {
     return context.ServerFactory.Create(context.Builder);
 }
 private static void InitializeServerFactory(StartContext context)
 {
     context.ServerFactory.Initialize(context.Builder);
 }
Beispiel #36
0
        private void ResolveApp(StartContext context)
        {
            context.Builder.Use(typeof(Encapsulate), context.EnvironmentData);

            if (context.App == null)
            {
                IList<string> errors = new List<string>();
                if (context.Startup == null)
                {
                    string appName = DetermineApplicationName(context);
                    context.Startup = _appLoader.Load(appName, errors);
                }
                if (context.Startup == null)
                {
                    throw new EntryPointNotFoundException(Resources.Exception_AppLoadFailure
                        + Environment.NewLine + " - " + string.Join(Environment.NewLine + " - ", errors));
                }
                context.Startup(context.Builder);
            }
            else
            {
                context.Builder.Use(new Func<object, object>(_ => context.App));
            }
        }
        private static string DetermineOwinServer(StartContext context)
        {
            StartOptions options = context.Options;
            IDictionary<string, string> settings = context.Options.Settings;

            string serverName = options.ServerFactory;
            if (!string.IsNullOrWhiteSpace(serverName))
            {
                return serverName;
            }

            if (settings != null &&
                settings.TryGetValue(Constants.SettingsOwinServer, out serverName) &&
                !string.IsNullOrWhiteSpace(serverName))
            {
                return serverName;
            }

            serverName = Environment.GetEnvironmentVariable(Constants.EnvOwnServer, EnvironmentVariableTarget.Process);
            if (!string.IsNullOrWhiteSpace(serverName))
            {
                return serverName;
            }

            return null;
        }
        private static string DetermineApplicationName(StartContext context)
        {
            StartOptions options = context.Options;
            IDictionary<string, string> settings = context.Options.Settings;

            if (options != null && !string.IsNullOrWhiteSpace(options.AppStartup))
            {
                return options.AppStartup;
            }

            string appName;
            if (settings.TryGetValue(Constants.SettingsOwinConfig, out appName) &&
                !string.IsNullOrWhiteSpace(appName))
            {
                return appName;
            }

            return null;
        }
Beispiel #39
0
 private static IDisposable StartServer(StartContext context)
 {
     return(context.ServerFactory.Create(context.Builder));
 }
Beispiel #40
0
        public void MultipleUrlsSpecified()
        {
            StartOptions startOptions = new StartOptions();
            startOptions.Urls.Add("beta://localhost:3333");
            startOptions.Urls.Add("delta://foo/");
            startOptions.Urls.Add("gama://*:4444/");
            startOptions.Port = 1111; // Ignored because of Url(s)

            var serverFactory = new ServerFactoryAlpha();
            var startInfo = new StartContext(startOptions);
            startInfo.ServerFactory = new ServerFactoryAdapter(serverFactory);
            startInfo.App = new AppFunc(env => TaskHelpers.Completed());

            var engine = ServicesFactory.Create().GetService<IHostingEngine>();
            serverFactory.InitializeCalled.ShouldBe(false);
            serverFactory.CreateCalled.ShouldBe(false);
            IDisposable server = engine.Start(startInfo);

            serverFactory.InitializeProperties["host.Addresses"].ShouldBeTypeOf<IList<IDictionary<string, object>>>();

            var addresses = (IList<IDictionary<string, object>>)serverFactory.InitializeProperties["host.Addresses"];
            Assert.Equal(3, addresses.Count);

            string[][] expectedAddresses = new[]
            {
                new[] { "beta", "localhost", "3333", string.Empty },
                new[] { "delta", "foo", string.Empty, "/" },
                new[] { "gama", "*", "4444", "/" },
            };

            for (int i = 0; i < addresses.Count; i++)
            {
                var addressDictionary = addresses[i];
                var expectedValues = expectedAddresses[i];
                Assert.Equal(expectedValues.Length, addressDictionary.Count);
                Assert.Equal(expectedValues[0], (string)addressDictionary["scheme"]);
                Assert.Equal(expectedValues[1], (string)addressDictionary["host"]);
                Assert.Equal(expectedValues[2], (string)addressDictionary["port"]);
                Assert.Equal(expectedValues[3], (string)addressDictionary["path"]);
            }

            server.Dispose();
        }
 private static IDisposable EnableDisposing(StartContext context)
 {
     var cts = new CancellationTokenSource();
     context.Builder.Properties[Constants.HostOnAppDisposing] = cts.Token;
     context.EnvironmentData.Add(new KeyValuePair<string, object>(Constants.HostOnAppDisposing, cts.Token));
     return new Disposable(() => cts.Cancel(false));
 }
Beispiel #42
0
        private static int DeterminePort(StartContext context)
        {
            int port;
            if (!TryDetermineCustomPort(context.Options, out port))
            {
                port = DefaultPort;
            }

            return port;
        }