Ejemplo n.º 1
0
        /// <summary>
        /// Creates a new <see cref="Web"/> object using a given provider and listen address.
        /// </summary>
        /// <param name="blockProvider">Provider for blocks to use.</param>
        /// <param name="url">Address that the server listens on.</param>
        public WebServer(IBlockProvider blockProvider, IAnimalProvider animalProvider, IPlantProvider plantProvider, string url, string webRoot)
        {
            if (blockProvider == null)
                throw new ArgumentNullException(nameof(blockProvider));
            if (animalProvider == null)
                throw new ArgumentNullException(nameof(animalProvider));
            if (plantProvider == null)
                throw new ArgumentNullException(nameof(plantProvider));

            if (Instance != null)
                throw new InvalidOperationException(nameof(WebServer) + " is a singleton class and may not be instantiated multiple times.");

            Instance = this;

            // Set the providers
            BlockProvider = blockProvider;
            AnimalProvider = animalProvider;
            PlantProvider = plantProvider;

            try
            {
                // Start the server (/Katana/OWIN pipeline)
                var options = new StartOptions();
                options.Urls.Add(url);
                _webServer = WebApp.Start(url, (app) => new Web.Startup(webRoot).Configuration(app));
            }
            catch (System.Reflection.TargetInvocationException ex)
            {
                // Because those exceptions usually are not helpful ("an invocation target has something something"), we'll rather throw the inner exception
                if (ex.InnerException == null)
                    throw;

                System.Runtime.ExceptionServices.ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
            }

            // Get the hub contexts
            _mapHubContext = GlobalHost.ConnectionManager.GetHubContext<Web.MapHub>();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Disposes the server object.
        /// </summary>
        public void Dispose()
        {
            _webServer?.Dispose();
            _webServer = null;

            Instance = null;
        }