Unity in ASP.NET using WEB.config

Jul 6, 2009 at 8:23 PM

I´m trying to use Unity with my web aplicaction. But i don´t get it to work.

I have two clases, one is an Interface of the other. (ILogger,Logger).

Here is my web.config and my CS file

The problem is that every time I run the app  y get the message "Referencia a objeto no establecida como instancia de un objeto."

When the application passes through this line ( Label4.Text = Logger.Log();)

Can any one help me?

--Default.aspx.cs

using System;
using System.Configuration;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;

namespace UI
{   

    public partial class _Default : System.Web.UI.Page
    {
        [Dependency]
        public ILogger Logger { get; set; }

        protected void Button3_Click(object sender, EventArgs e)
        {           

            Label4.Text = Logger.Log();
        }
    }
}

--ILoger

namespace UI
{
    public interface ILogger
    {
        string Log();
    }
}

-- Loger

namespace UI
{
    public class Logger : ILogger
    {
        public string Log()
        {
            return "It works!!";        
        }
    }
}
--web.config


<configuration>
  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=none">
      <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
        <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
        <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
          <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere" />
          <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
          <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
          <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
        </sectionGroup>
      </sectionGroup>
    </sectionGroup>
  </configSections>
  <dataConfiguration defaultDatabase="EjemploDB" />
  <connectionStrings>
    <add name="EjemploDB" connectionString="Data Source=(local);Initial Catalog=master;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
  <unity>
    <containers>
      <container>
        <types>
          <type
              type="UI.ILogger,UI"
              mapTo="UI.Logger,UI" />        
        </types>
      </container>
    </containers>
  </unity>
</configuration>

 

 

Jul 7, 2009 at 1:31 AM
Edited Jul 7, 2009 at 2:50 AM

Injecting dependency in web pages using unity is not as straightforward as with that of windows forms.  Check out David Hayden's screencast on how to inject dependency to web pages - http://www.pnpguidance.net/Post/UnityIoCASPNETScreencastDependencyInjectionWebPages.aspx

 

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.

entlib.support@avanade.com