// This software is part of the Autofac IoC container
// Copyright © 2011 Autofac Contributors
// http://autofac.org
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
using System.Web;
using System.Web.Hosting;
using System.Web.Mvc;
using System.Web.Routing;
namespace Autofac.Integration.Mvc
{
///
/// Dependency injection module that registers abstractions for common
/// web application properties.
///
///
///
/// This is primarily used during
/// application startup (in Global.asax) to register
/// mappings from commonly referenced contextual application properties
/// to their corresponding abstraction.
///
///
/// The following mappings are made:
///
///
///
/// Common Construct
/// Abstraction
///
/// -
/// HttpContext.Current
///
///
/// -
/// HttpContext.Current.Application
///
///
/// -
/// HttpContext.Current.Request
///
///
/// -
/// HttpContext.Current.Request.Browser
///
///
/// -
/// HttpContext.Current.Request.Files
///
///
/// -
/// HttpContext.Current.Request.RequestContext
///
///
/// -
/// HttpContext.Current.Response
///
///
/// -
/// HttpContext.Current.Response.Cache
///
///
/// -
/// HttpContext.Current.Server
///
///
/// -
/// HttpContext.Current.Session
///
///
/// -
/// HostingEnvironment.VirtualPathProvider
///
///
///
///
/// In addition, the type is registered
/// for construction based on the current .
///
///
/// The lifetime for each of these items is one web request.
///
///
public class AutofacWebTypesModule : Module
{
///
/// Registers web abstractions with dependency injection.
///
///
/// The in which registration
/// should take place.
///
///
///
/// This method registers mappings between common current context-related
/// web constructs and their associated abstract counterparts. See
/// for the complete
/// list of mappings that get registered.
///
///
protected override void Load(ContainerBuilder builder)
{
builder.Register(c => new HttpContextWrapper(HttpContext.Current))
.As()
.InstancePerHttpRequest();
// HttpContext properties
builder.Register(c => c.Resolve().Request)
.As()
.InstancePerHttpRequest();
builder.Register(c => c.Resolve().Response)
.As()
.InstancePerHttpRequest();
builder.Register(c => c.Resolve().Server)
.As()
.InstancePerHttpRequest();
builder.Register(c => c.Resolve().Session)
.As()
.InstancePerHttpRequest();
builder.Register(c => c.Resolve().Application)
.As()
.InstancePerHttpRequest();
// HttpRequest properties
builder.Register(c => c.Resolve().Browser)
.As()
.InstancePerHttpRequest();
builder.Register(c => c.Resolve().Files)
.As()
.InstancePerHttpRequest();
builder.Register(c => c.Resolve().RequestContext)
.As()
.InstancePerHttpRequest();
// HttpResponse properties
builder.Register(c => c.Resolve().Cache)
.As()
.InstancePerHttpRequest();
// HostingEnvironment properties
builder.Register(c => HostingEnvironment.VirtualPathProvider)
.As()
.InstancePerHttpRequest();
// MVC types
builder.Register(c => new UrlHelper(c.Resolve()))
.As()
.InstancePerHttpRequest();
}
}
}