// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.Diagnostics.CodeAnalysis; using System.Diagnostics.Contracts; using System.Net.Http; using System.Web.Http.Controllers; using System.Web.Http.Dispatcher; using System.Web.Http.ExceptionHandling; using System.Web.Http.Hosting; using System.Web.Http.WebHost; using System.Web.Http.WebHost.Routing; using System.Web.Routing; namespace System.Web.Http { /// /// Provides a global for ASP.NET applications. /// public static class GlobalConfiguration { private static Lazy _configuration = CreateConfiguration(); private static Lazy _defaultHandler = CreateDefaultHandler(); private static Lazy _defaultServer = CreateDefaultServer(); /// /// Gets the global . /// public static HttpConfiguration Configuration { get { return _configuration.Value; } } /// /// Gets the default message handler that will be called for all requests. /// public static HttpMessageHandler DefaultHandler { get { return _defaultHandler.Value; } } /// /// Gets the global . /// public static HttpServer DefaultServer { get { return _defaultServer.Value; } } /// /// Performs configuration for and ensures that it is /// initialized. /// /// The callback that will perform the configuration. public static void Configure(Action configurationCallback) { if (configurationCallback == null) { throw new ArgumentNullException("configurationCallback"); } configurationCallback.Invoke(Configuration); Configuration.EnsureInitialized(); } internal static void Reset() { _configuration = CreateConfiguration(); _defaultHandler = CreateDefaultHandler(); _defaultServer = CreateDefaultServer(); } [SuppressMessage("Microsoft.Reliability", "CA2000", Justification = "It does not appear possible for this construction code to throw.")] private static Lazy CreateConfiguration() { return new Lazy(() => { HttpConfiguration config = new HttpConfiguration(new HostedHttpRouteCollection(RouteTable.Routes)); ServicesContainer services = config.Services; Contract.Assert(services != null); services.Replace(typeof(IAssembliesResolver), new WebHostAssembliesResolver()); services.Replace(typeof(IHttpControllerTypeResolver), new WebHostHttpControllerTypeResolver()); services.Replace(typeof(IHostBufferPolicySelector), new WebHostBufferPolicySelector()); services.Replace(typeof(IExceptionHandler), new WebHostExceptionHandler(services.GetExceptionHandler())); return config; }); } private static Lazy CreateDefaultHandler() { return new Lazy(() => new HttpRoutingDispatcher(_configuration.Value)); } private static Lazy CreateDefaultServer() { return new Lazy(() => new HttpServer(_configuration.Value, _defaultHandler.Value)); } } }