Click here to Skip to main content
15,305,754 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
Hi,

Can anybody please let me know how to register interface using Castle.Winsdor in Application_Start Event of Global.asax & then how to access the registered interface on the web.page.

If there is another appropriate approach please let me know.

Any help will be highly appreciated.

Thanks In Advance.
Posted

   
Hi Maciej,

I implemented it in below fashion. Can you please let me know whether i implemented properly :

Register Windsor Container in Global.asax :-
-----------------------------------------------------
C#
private static WindsorContainer _container = null;

       /// <summary>
       /// Get Winsdor Container
       /// </summary>
       public static WindsorContainer RegisteredWinsdorContainer
       {
           get { return _container; }
       }

       protected void Application_Start(object sender, EventArgs e)
       {
           CreateAndRegisterMethods();
       }

       /// <summary>
       ///
       /// </summary>
       private static void CreateAndRegisterMethods()
       {
           _container = new WindsorContainer();
           _container.AddComponent("UserManager", typeof (IUserManager),
                                   typeof (UserManager));
           _container.AddComponent("AddressManager", typeof (IAddressManager),
                                   typeof (AddressManager));
       }



Create a generic class to find registered interfaces WindsorContainerDI.cs :-

XML
public class WindsorContainerDI<T>                                                             
    {
        /// <summary>
        ///
        /// </summary>
        /// <param name="winsdorInterface"></param>
        public static void GetWinsdorContainer(ref T winsdorInterface)
        {
            WindsorContainer container = Global.RegisteredWinsdorContainer;
            if(container!=null)
            {
                winsdorInterface = container.Resolve<T>();
            }
        }
    }                                                                              


Access the methods of interface in this fashion Default.aspx.cs

XML
/// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            IUserManager userManagerContainer = null;

            WindsorContainerDI<IUserManager>.GetWinsdorContainer(ref userManagerContainer);

            if(userManagerContainer!=null)
            {
                UserObject[] userObj = userManagerContainer.GetUsers();
            }

        }
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900