Content
ASP.NET WebForms / MVC Integration
You will find all you need within the assembly LightCore.Integration.Web.dll.
LightCore.Integration.Web Namespace
The namespace LightCore.Integration exposes basic functions that are needed for using LightCore in ASP.NET Webforms applications.
ContainerAccessorNotImplementedException
If you added the DepenencyInjectionModule but forgotten to implement the IContainerAccessor-interface on the Global.asax,
the exception ContainerAccessorNotImplementedException will be thrown.
DependencyInjectionModule
The DependencyInjectionModule represents a HttpModule which is responsible doing Dependency Injection in ASP.NET WebForms applications.
On this process, the module uses property-injection, this is the only case where LightCore uses property-injection.
You have to register the HttpModule in your web.config. Consider: On IIS7 and integrated mode, you should register the module also in System.webServer section:
<add
name="LightCoreDependencyInjectionModule"
type="LightCore.Integration.Web.DependencyInjectionModule, LightCore.Integration.Web"/>
IContainerAccessor
On WebForms applications, the IContainerAccessor-interface must be implemented in
the application class (e.g. Global.asax.cs).
The interface exists for the DependencyInjectionModule to find the current IContainer-instance.
public interface IContainerAccessor
{
IContainer Container { get; }
}
Moreover you can use this interface on your own, to get instances in a service locator way.
This is e.g. the only way to use a DI-Container if you are in a ASP.NET MVC ActionFilter-attribute,
since you (and the framework) could not be responsible for the instantiation.
IContainerAccessor accessor = (IContainerAccessor)HttpContext.Current.ApplicationInstance;
IContainer container = accessor.Container;
IUserService userService = container.Resolve<IUserService>();
LightCore.Integration.Web.Lifecycle
HttpRequestLifecycle
This class represents a lifecycle for creating instances with LightCore.
If you declare this lifecycle in a registration, the instance are shared with a HttpRequest,
this is a per-request behaviour.
You can additionaly implement own strategies for a lifecycle, only by implement the ILifecycle interface.
LightCore.Integration.Web.Mvc
You will find LightCore support for ASP.NET MVC in this namespace.
ControllerFactory
To use dependency injection on ASP.NET MVC, you must register the ControllerFactory in your global.asax:
ControllerBuilder.Current.SetControllerFactory(new ControllerFactory(_container));
The ControllerFactory-constructor needs a IContainer-instance of your application (on top "_container").
ControllerRegistrationModule
To register ASP.NET MVC controllers in n assemblies automatically to make it ready for using with LightCore,
you must use the ControllerRegistrationModule:
var builder = new ContainerBuilder();
ControllerRegistrationModule controllerModule = new ControllerRegistrationModule(new[] { Assembly.GetExecutingAssembly() });
builder.RegisterModule(mvcModule);
_container = builder.Build();