stylishCoder
stylishCoder

Reputation: 385

Issues in implementing Windsor Castle at app config level

I have written a dummy program that implements Windsor castle below.But my problem is that i want to write its components in my WEB CONFIG but i don't able to know where to write what..can any one helps me in correcting that

Please correct me writing is App.config level of behaviour i.e to make it configurable??? Dummy Programm:

namespace NnjectFramework
{

    class Program
    {
#region  windsor castle
        interface IFoo
        {
            void test();
            void test2();
        }
        public class Foo: IFoo
        {
            private readonly string _arg1;
            private readonly string _arg2;

            public Foo(string arg1, string arg2)
            {
                _arg1 = arg1;
                _arg2 = arg2;
            }


            public void test()
            {
                Console.WriteLine("Success method 1");
            }
            public void test2()
            {
                Console.WriteLine("Success method 2");
            }
        }
        class Bar
        {
            private Foo bar;

            public Bar(Foo bar)
            {
                this.bar = bar;
            }
        }
        static void Main(string[] args)
        {

            IWindsorContainer container = new WindsorContainer();
            container.Register(Component.For<IFoo>().ImplementedBy<Foo>().LifeStyle.Is(LifestyleType.Transient).Named("AFooNamedFoo"));

            IFoo foo = container.Resolve<Foo>("AFooNamedFoo", new { arg1 = "hello", arg2 = "world" });
            foo.test();
            foo.test2();
            Console.ReadKey();

        }

        #endregion
        }

} 

What i have tried in App.config:

<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
  <components>

    <component id="uniqueId"
    service="Acme.Crm.Services.INotificationService, Acme.Crm"
    type="Acme.Crm.Services.EmailNotificationService, Acme.Crm"
    inspectionBehavior="all|declaredonly|none"
    lifestyle="transient"
    customLifestyleType="type that implements ILifestyleManager"
    componentActivatorType="type that implements IComponentActivator"
    initialPoolSize="2" maxPoolSize="6"></component>
  </components></configuration>

Upvotes: 1

Views: 568

Answers (1)

BWA
BWA

Reputation: 5764

Dummy app (modyfied):

using System;
using Castle.Windsor;

namespace StackOwerflow
{
    public class Program
    {
        #region  windsor castle
        interface IFoo
        {
            void test();
            void test2();
        }
        public class Foo : IFoo
        {
            private readonly string _arg1;
            private readonly string _arg2;

            public Foo(string arg1, string arg2)
            {
                _arg1 = arg1;
                _arg2 = arg2;
            }


            public void test()
            {
                Console.WriteLine("Success method 1");
            }
            public void test2()
            {
                Console.WriteLine("arg1: {0}. arg2: {1}", _arg1, _arg2);
            }
        }
        class Bar
        {
            private Foo foo;

            public Bar(Foo foo)
            {
                this.foo = foo;
            }
        }
        static void Main(string[] args)
        {

            IWindsorContainer container = new WindsorContainer();            
            container.Install(Castle.Windsor.Installer.Configuration.FromAppConfig());            

            IFoo foo = container.Resolve<Foo>("AFooNamedFoo");
            foo.test();
            foo.test2();
            Console.ReadKey();

        }

        #endregion
    }
}

And app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="castle"
        type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <castle>
    <components>
      <component
          id="AFooNamedFoo"
          service="StackOwerflow.Program+IFoo, StackOwerflow"
          type="StackOwerflow.Program+Foo, StackOwerflow"
          lifestyle="Transient">
        <parameters>
          <arg1>hello</arg1>
          <arg2>world</arg2>
        </parameters>
      </component>
    </components>
    </castle>

</configuration>

My assembly name is StackOwerflow, so you should modify it to your assembly name.

Upvotes: 2

Related Questions