public void LoadWithWrongAssemblyOrType_ReturnsNull(string data)
        {
            var loader = new ServerFactoryLoader(new ServerFactoryActivator(ServicesFactory.Create()));
            IServerFactoryAdapter serverFactory = loader.Load(data);

            Assert.Null(serverFactory);
        }
Ejemplo n.º 2
0
        public void RunServer(StartOptions options)
        {
            if (options == null)
            {
                return;
            }

            // get existing loader factory services
            string appLoaderFactories;

            if (!options.Settings.TryGetValue(typeof(IAppLoaderFactory).FullName, out appLoaderFactories) ||
                !string.IsNullOrEmpty(appLoaderFactories))
            {
                // use the built-in AppLoaderFactory as the default
                appLoaderFactories = typeof(AppLoaderFactory).AssemblyQualifiedName;
            }

            // prepend with our app loader factory
            options.Settings[typeof(IAppLoaderFactory).FullName] =
                typeof(AppLoaderWrapper).AssemblyQualifiedName + ";" + appLoaderFactories;

            DefaultHost host = null;

            if (options.Settings.ContainsKey("devMode"))
            {
                host = new DefaultHost(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, watchFiles: true);

                host.OnChanged += () =>
                {
                    Environment.Exit(250);
                };
            }
            else
            {
                host = new DefaultHost(AppDomain.CurrentDomain.SetupInformation.ApplicationBase);
            }

            using (_hostContainer.AddHost(host))
            {
                WriteLine("Starting with " + GetDisplayUrl(options));

                // Ensure the DefaultHost is available to the AppLoaderWrapper
                IServiceProvider container = ServicesFactory.Create(options.Settings, services =>
                {
                    services.Add(typeof(ITraceOutputFactory), () => new NoopTraceOutputFactory());
                    services.Add(typeof(DefaultHost), () => host);
                });

                IHostingStarter starter = container.GetService <IHostingStarter>();
                using (starter.Start(options))
                {
                    WriteLine("Started successfully");

                    WriteLine("Press Enter to exit");
                    Console.ReadLine();

                    WriteLine("Terminating.");
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Start the component
        /// </summary>
        public void Start()
        {
            if (string.IsNullOrWhiteSpace(this.configuration.BackendAddress))
            {
                throw new ArgumentException("Unable to start DashboardBackend when no BackendUrl is specified");
            }

            var services = (ServiceProvider)ServicesFactory.Create();
            var options  = new StartOptions
            {
                Urls =
                {
                    this.configuration.BackendAddress
                },
                AppStartup = typeof(Startup).FullName
            };

            // Pass through the IJobbrServiceProvider to allow Startup-Classes to let them inject this dependency
            services.Add(typeof(IJobbrServiceProvider), () => this.dependencyResolver);

            var hostingStarter = services.GetService <IHostingStarter>();

            this.webHost = hostingStarter.Start(options);

            Logger.InfoFormat($"Started OWIN-Host for DashboardBackend at '{this.configuration.BackendAddress}'.");
        }
Ejemplo n.º 4
0
        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 serverFactory = new AzureServiceBusOwinServerFactory(config);
            var services      = ServicesFactory.Create();
            var engine        = services.GetService <IHostingEngine>();
            var context       = new StartContext(options)
            {
                ServerFactory = new ServerFactoryAdapter(serverFactory),
                Startup       = startup
            };

            _started = engine.Start(context);
        }
Ejemplo n.º 5
0
        public static void RunServer(StartOptions options)
        {
            if (options == null)
            {
                return;
            }

            string boot;

            if (!options.Settings.TryGetValue("boot", out boot) ||
                string.IsNullOrWhiteSpace(boot))
            {
                options.Settings["boot"] = "Domain";
            }

            ResolveAssembliesFromDirectory(
                Path.Combine(Directory.GetCurrentDirectory(), "bin"));

            WriteLine("Starting with " + GetDisplayUrl(options));

            IServiceProvider services = ServicesFactory.Create();
            var         starter       = services.GetService <IHostingStarter>();
            IDisposable server        = starter.Start(options);

            WriteLine("Started successfully");

            WriteLine("Press Enter to exit");
            Console.ReadLine();

            WriteLine("Terminating.");

            server.Dispose();
        }
Ejemplo n.º 6
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;
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Configures the OWIN pipeline.
        /// </summary>
        /// <param name="startup">Startup function used to configure the OWIN pipeline.</param>
        /// <param name="options">Settings to control the startup behavior of an OWIN application</param>
        protected void Configure(Action <IAppBuilder> startup, StartOptions options)
        {
            // Compare with WebApp.StartImplementation
            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 TestServerFactory();
            IServiceProvider services = ServicesFactory.Create();
            var engine  = services.GetService <IHostingEngine>();
            var context = new StartContext(options);

            context.ServerFactory = new ServerFactoryAdapter(testServerFactory);
            context.Startup       = startup;
            _started = engine.Start(context);
            _next    = testServerFactory.Invoke;
        }
Ejemplo n.º 8
0
        static int Main(string[] args)
        {
            if (args.Length >= 2)
            {
                Usage();
                return(0);
            }

            string arg;

            if (args.Length == 1)
            {
                arg = args[0];
                if (arg == "help" || arg == "-help" || arg == "--help")
                {
                    Usage();
                    return(0);
                }
            }
            else
            {
                var portStr = Environment.GetEnvironmentVariable("PORT");
                int port;
                if (portStr != null && int.TryParse(portStr, out port))
                {
                    arg = string.Format("http://localhost:{0}/", port);
                }
                else
                {
                    arg = "http://localhost:5000/";
                }
            }

            try
            {
                var options = new StartOptions(arg);
                options.Settings["boot"] = "Domain";
                using (ServicesFactory.Create().GetService <IHostingStarter>().Start(options))
                {
                    Console.WriteLine(arg);

                    var resetEvent = new ManualResetEvent(false);
                    Console.CancelKeyPress += (sender, e) =>
                    {
                        e.Cancel = true;
                        resetEvent.Set();
                    };

                    resetEvent.WaitOne();
                    Console.WriteLine("Disposing...");
                }
                return(0);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return(1);
            }
        }
Ejemplo n.º 9
0
        public static ServiceProvider Create()
        {
            var provider = (ServiceProvider)ServicesFactory.Create();

            provider.Add(typeof(ITraceOutputFactory), typeof(OwinTraceOutputFactory));

            return(provider);
        }
Ejemplo n.º 10
0
 private static IServiceProvider BuildServices(StartOptions options)
 {
     if (options.Settings != null)
     {
         return(ServicesFactory.Create(options.Settings));
     }
     return(ServicesFactory.Create());
 }
Ejemplo n.º 11
0
        private static IDisposable Start(StartContext context, IListen listener)
        {
            IServiceProvider services = ServicesFactory.Create();
            var engine = services.GetService <IHostingEngine>();

            context.ServerFactory = new ServerFactoryAdapter(new CustomListenerHostFactory(listener));
            return(engine.Start(context));
        }
Ejemplo n.º 12
0
        public override Func <Type, object> CreateContainer()
        {
            IServiceProvider services = ServicesFactory.Create(reg => reg
                                                               .Add <IAppLoaderFactory, TestAppLoader1>()
                                                               .Add <IAppLoaderFactory, TestAppLoader2>());

            return(services.GetService);
        }
Ejemplo n.º 13
0
        private IServiceProvider CreateServiceFactory()
        {
            var provider = (ServiceProvider)ServicesFactory.Create();

            provider.Add(typeof(ITraceOutputFactory), typeof(OwinTraceOutputFactory));

            return(provider);
        }
Ejemplo n.º 14
0
        public void LoadWithDefaults_LoadAssemblyAndDiscoverFactory(string data)
        {
            var loader = new ServerFactoryLoader(new ServerFactoryActivator(ServicesFactory.Create()));
            IServerFactoryAdapter serverFactory = loader.Load(data);

            Assert.NotNull(serverFactory);
            IAppBuilder builder = new AppBuilder();

            serverFactory.Initialize(builder);
            Assert.IsType <OwinHttpListener>(builder.Properties[typeof(OwinHttpListener).FullName]);
        }
Ejemplo n.º 15
0
        public void LoadWithAssemblyAndFullTypeName_Success(string data, string expected)
        {
            var loader = new ServerFactoryLoader(new ServerFactoryActivator(ServicesFactory.Create()));
            IServerFactoryAdapter serverFactory = loader.Load(data);

            Assert.NotNull(serverFactory);
            IAppBuilder builder = new AppBuilder();

            serverFactory.Create(builder);
            Assert.Equal(expected, builder.Properties["create.server"]);
        }
Ejemplo n.º 16
0
        public void LoadWithAssemblyName_DiscoverDefaultFactoryName()
        {
            var loader = new ServerFactoryLoader(new ServerFactoryActivator(ServicesFactory.Create()));
            IServerFactoryAdapter serverFactory = loader.Load("Microsoft.Owin.Hosting.Tests");

            Assert.NotNull(serverFactory);
            IAppBuilder builder = new AppBuilder();

            serverFactory.Create(builder);
            Assert.Equal("Microsoft.Owin.Hosting.Tests.OwinServerFactory", builder.Properties["create.server"]);
        }
Ejemplo n.º 17
0
        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));
        }
Ejemplo n.º 18
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;
        }
Ejemplo n.º 19
0
        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);
        }
Ejemplo n.º 20
0
        public static IDisposable Start <TStartup>(IListen listener)
        {
            var options = new StartOptions();

            options.AppStartup = typeof(TStartup).AssemblyQualifiedName;
            var context = new StartContext(options);

            context.ServerFactory = new ServerFactoryAdapter(new CustomListenerHostFactory(listener));
            IServiceProvider services = ServicesFactory.Create();
            var engine = services.GetService <IHostingEngine>();

            return(engine.Start(context));
        }
Ejemplo n.º 21
0
        /// <summary>
        /// Configures the OWIN pipeline.
        /// </summary>
        /// <typeparam name="TStartup">Class containing a startup function used to configure the OWIN pipeline.</typeparam>
        /// <param name="options">Settings to control the startup behavior of an OWIN application.</param>
        protected void Configure <TStartup>(StartOptions options)
        {
            // Compare with WebApp.StartImplementation
            options            = options ?? new StartOptions();
            options.AppStartup = typeof(TStartup).AssemblyQualifiedName;

            var webSocketHttpServerFactory = new OwinHttpServerFactory();
            var services = ServicesFactory.Create();
            var engine   = services.GetService <IHostingEngine>();
            var context  = new StartContext(options);

            context.ServerFactory = new ServerFactoryAdapter(webSocketHttpServerFactory);
            _started = engine.Start(context);
        }
Ejemplo n.º 22
0
        static void Main(string[] args)
        {
            var iocBootstrap = new IocBootstrap();

            using (var container = iocBootstrap.Install())
            {
                using (var lifetimeScope = container.BeginLifetimeScope())
                {
                    var appSettings = lifetimeScope.Resolve <IAppSettings>();

                    if (args.Any())
                    {
                        var command = string.Join(" ", args);
                        var result  = TrySendCommandAsync(appSettings.BaseAddress, command).Result;
                        if (result)
                        {
                            return;
                        }
                    }

                    Log.Info("Starting new instance...");

                    var player = lifetimeScope.Resolve <IPlayer>();
                    player.Run(appSettings);

                    player.SendCommand(new LoginCommand {
                        Username = appSettings.Username, Password = appSettings.Password
                    });

                    var services = (ServiceProvider)ServicesFactory.Create();
                    var options  = new StartOptions(appSettings.BaseAddress);

                    services.AddInstance <ILifetimeScope>(lifetimeScope);

                    var starter = services.GetService <IHostingStarter>();

                    using (starter.Start(options))
                    {
                        if (args.Any())
                        {
                            var command = string.Join(" ", args);
                            TrySendCommandAsync(appSettings.BaseAddress, command).Wait();
                        }

                        Log.Info("Waiting for input to quit");
                        Console.ReadKey(true);
                    }
                }
            }
        }
Ejemplo n.º 23
0
        public static IDisposable Start(Action <IAppBuilder> startup, IListen listener)
        {
            var options = new StartOptions();

            options.AppStartup = startup.Method.ReflectedType.FullName;
            var context = new StartContext(options);

            context.Startup       = startup;
            context.ServerFactory = new ServerFactoryAdapter(new CustomListenerHostFactory(listener));
            IServiceProvider services = ServicesFactory.Create();
            var engine = services.GetService <IHostingEngine>();

            return(engine.Start(context));
        }
Ejemplo n.º 24
0
        static void Main(string[] args)
        {
            var arguments = args.Select(t => t.Split('=')).ToDictionary(spl => spl[0].Trim('-'), spl => spl[1]);

            Console.Clear();
            Console.Title = "Ethereum Self-hosted API - Ver. " + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;

            var settings = GetSettings(arguments);

            if (settings == null)
            {
                Console.WriteLine("Press any key to exit...");
                Console.ReadKey();
                return;
            }


            try
            {
                CheckSettings(settings);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine("Press any key to exit...");
                Console.ReadKey();
                return;
            }

            var services = (ServiceProvider)ServicesFactory.Create();

            services.AddInstance <IBaseSettings>(settings);

            var url = $"http://*:{arguments["port"]}";

            var options = new StartOptions(url);

            var starter = services.GetService <IHostingStarter>();

            using (starter.Start(options))
            {
                Console.WriteLine($"Web Server is running - {url}");
                Console.WriteLine("Press 'q' to quit.");

                while (Console.ReadLine() != "q")
                {
                    continue;
                }
            }
        }
Ejemplo n.º 25
0
        public static int Run(string[] args)
        {
            StartOptions options = ParseArguments(args);

            if (options == null)
            {
                return(0);
            }

            bool traceVerbose = IsVerboseTraceEnabled(options);

            WriteLine(options, traceVerbose, "Verbose");

            string boot;

            if (!options.Settings.TryGetValue("boot", out boot) ||
                string.IsNullOrWhiteSpace(boot))
            {
                options.Settings["boot"] = "Domain";
            }

            // Have the auto-discovery algorithms include the location of this exe so we can detect server assemblies.
            string privateBin;

            if (!options.Settings.TryGetValue("privatebin", out privateBin) ||
                string.IsNullOrWhiteSpace(privateBin))
            {
                options.Settings["privatebin"] = Path.GetDirectoryName(typeof(Program).Assembly.Location);
            }

            ResolveAssembliesFromDirectory(
                Path.Combine(Directory.GetCurrentDirectory(), "bin"));

            WriteLine(options, traceVerbose, "Starting");

            IServiceProvider services = ServicesFactory.Create();
            var         starter       = services.GetService <IHostingStarter>();
            IDisposable server        = starter.Start(options);

            WriteLine(options, traceVerbose, "Started successfully");

            WriteLine(options, traceVerbose, "Press Enter to exit");
            Console.ReadLine();

            WriteLine(options, traceVerbose, "Terminating.");

            server.Dispose();
            return(0);
        }
        public void Open()
        {
            var services = (ServiceProvider)ServicesFactory.Create();
            var options  = new StartOptions(mUrl);

            services.Add(typeof(ISignalRConnectionListenerAdapter),
                         () => mAdapter);

            services.Add(typeof(ConnectionListenerSettings),
                         () => mSettings);

            var starter = services.GetService <IHostingStarter>();

            mDisposable = starter.Start(options);
        }
Ejemplo n.º 27
0
        public void CreateShouldBeProvidedWithAdaptedAppIfNeeded()
        {
            var serverFactoryBeta = new ServerFactoryBeta();
            var startInfo         = new StartContext(new StartOptions());

            startInfo.ServerFactory = new ServerFactoryAdapter(serverFactoryBeta);
            startInfo.App           = new AppFunc(env => Task.FromResult(0));

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

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

            serverFactoryBeta.CreateCalled.ShouldBe(true);
            server.Dispose();
        }
Ejemplo n.º 28
0
        public void MultipleUrlsSpecified()
        {
            var 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 => Task.FromResult(0));

            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);

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

            for (int i = 0; i < addresses.Count; i++)
            {
                IDictionary <string, object> addressDictionary = addresses[i];
                string[] 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();
        }
Ejemplo n.º 29
0
        public void Run(
            string rootDirectoryPath,
            string binPath,
            string currentDomainBin,
            int?port)
        {
            System.Console.WriteLine("Starting web server in '{0}'...", binPath);

            var currentDomain = this.CreateAppDomain(
                binPath,
                currentDomainBin);

            this.SetupAppDomain(currentDomain, rootDirectoryPath);

            IServiceProvider services = ServicesFactory.Create();

            IHostingStarter service = services.GetService <IHostingStarter>();

            var startOptions = new StartOptions
            {
                Port          = port.GetValueOrDefault(58080),
                ServerFactory = "Microsoft.Owin.Host.HttpListener",
                AppStartup    = typeof(Base2art.Soufflot.Http.Owin.Startup).FullName
            };

            try
            {
                this.item = service.Start(startOptions);
                Console.WriteLine("Started web server on port '{0}'...", startOptions.Port);
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed to start application");

                var targetInvocationException = e as TargetInvocationException;
                if (targetInvocationException != null && e.InnerException != null)
                {
                    Console.WriteLine(e.Message);
                    Console.WriteLine();
                    e = e.InnerException;
                }

                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
            }
        }
Ejemplo n.º 30
0
        /// <summary>
        /// Configures the OWIN pipeline.
        /// </summary>
        /// <typeparam name="TStartup">Class containing a startup function used to configure the OWIN pipeline.</typeparam>
        /// <param name="options">Settings to control the startup behavior of an OWIN application.</param>
        protected void Configure <TStartup>(StartOptions options)
        {
            // Compare with WebApp.StartImplementation
            options            = options ?? new StartOptions();
            options.AppStartup = typeof(TStartup).AssemblyQualifiedName;

            var testServerFactory     = new TestServerFactory();
            IServiceProvider services = ServicesFactory.Create();
            var engine  = services.GetService <IHostingEngine>();
            var context = new StartContext(options)
            {
                ServerFactory = new ServerFactoryAdapter(testServerFactory)
            };

            _started = engine.Start(context);
            _next    = testServerFactory.Invoke;
        }