Getting Started - ASP.NET and Winter Integration
You can download our free binary package, which also includes samples. Or you can just go through this short getting started article that describes how to integrate Winter IoC container with any ASP.NET application.
Step 1: Define Winter configuration section in Web.config
It's very natural to place components definition used in ASP.NET application into Web.config file,
so in simplest case your web.config should look like:
<configuration>
<configSections>
<section name="components" type="NI.Winter.XmlComponentsConfig, NI.Winter" />
</configSections>
<components>
</components>
<system.web>
</system.web>
</configuration>
After that you can obtain IComponetsConfig instance by calling 'System.Configuration.ConfigurationSettings.GetConfig("components")'.
Step 2: Configure container creation in Global.asax
Because container will be used almost for every request to your ASP.NET application good idea to place container initialization code to Global.asax file:
<%@ Import namespace="System.ComponentModel" %>
<%@ Import namespace="System.Configuration" %>
<%@ Import namespace="NI.Common" %>
<%@ Import namespace="NI.Winter" %>
<script language="C#" runat="server">
public IComponentsConfig AppComponentsConfig {
get {
if (Application["Config"]==null) {
Application["Config"] = ConfigurationSettings.GetConfig("components");
}
return Application["Config"] as IComponentsConfig;
}
}
public ApplicationContainer AppContainer {
get { return Context.Items["ApplicationContainer"] as ApplicationContainer; }
set { Context.Items["ApplicationContainer"] = value; }
}
protected virtual void Application_BeginRequest(Object sender, EventArgs e) {
// create components container
ApplicationContainer appContainer = new ApplicationContainer();
// inject service provider
appContainer.ServiceProvider = new NI.Winter.ServiceProvider( AppComponentsConfig );
// save reference to components container
AppContainer = appContainer;
}
protected virtual void Application_AcquireRequestState(Object sender, EventArgs e)
{
// if request handler is IComponent, add it to application container (System.Web.UI.Page actually is IComponent)
if (Context.Handler is IComponent)
AppContainer.Add( Context.Handler as IComponent );
}
protected virtual void Application_EndRequest(Object sender, EventArgs e)
{
// dispose components container
if (AppContainer!=null) AppContainer.Dispose();
}
</script>
In this code we define property AppComponentsConfig for accessing IComponentsConfig instance;
in its getter we store instance obtained from ConfigurationSettings in application context to avoid excessive config parsing on every HTTP request.
'BeginRequest' event handler contains code that initialized application container and service provider for current request
(so container instance will be created on every request - this means that you should care about thread-safety of your components).
'AcquireRequestState' event handler adds current HTTP request handler to our container (if it implements IComponent interface).
In most cases handler is an instance of System.Web.UI.Page class (which is implements IComponent) and will be added to the container.
This means that you can use Page.Site anywere inside pages/user controls to access your components by Service Locator pattern.
Step 3: Place you components definitions
Just add necessary definitions to 'components' section of your Web.config file:
<components>
<component name="datetimenow" type="NI.Winter.StaticPropertyInvokingFactory,NI.Winter" singleton="false" lazy-init="true">
<property name="TargetType"><type>System.DateTime,Mscorlib</type></property>
<property name="TargetProperty"><value>Now</value></property>
</component>
<component name="datetimenow-3days" type="NI.Winter.MethodInvokingFactory,NI.Winter" singleton="false" lazy-init="true">
<property name="TargetObject"><ref name="datetimenow"/></property>
<property name="TargetMethod"><value>AddDays</value></property>
<property name="TargetMethodArgTypes"><list><entry><type>System.Double,Mscorlib</type></entry></list></property>
<property name="TargetMethodArgs"><list><entry><value>-3</value></entry></list></property>
</component>
</components>
Syntax is similar to SpringFramework (main difference is that 'bean' tag replaced with 'component' tag); for complete schema you may
refer to 'Docs/ComponentsConfigSchema.xsd' in the package.
Step 4: Use components from ASP.NET Page
Any instance from your Winter configuration can be easily accessed in following manner:
<html>
<body>
<script language="c#" runat="server">
protected NI.Common.INamedServiceProvider NamedServiceProvider {
get { return Page.Site.GetService(typeof(NI.Common.INamedServiceProvider)) as NI.Common.INamedServiceProvider; }
}
</script>
component: datetimenow =
<%=NamedServiceProvider.GetService("datetimenow") %><br/>
component: datetimenow-3days =
<%=NamedServiceProvider.GetService("datetimenow-3days") %><br/>
</body>
</html>
These samples should not be considered commercial quality applications. They are just intended to illustrate how to use some particular feature, or group of features in Winter4Net. Please feel free to polish them into applications of your own, or extract
sections to use in your own code.