views:

49

answers:

4

Hi I've been using Castle project for the first time and facing a problem in registering a component with the container in a console application. Following is the castle.config file:

<configuration>
  <configSections>
    <section name="castle"
        type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />
  </configSections>
  <castle>
    <components>
      <component id="messageSender"
               type="CastleTest.SecretMessageSender, CastleTest">
        <parameters>SecretMessageSender
          <from>[email protected]</from>
          <encoder>${encoder.null}</encoder>
        </parameters>
      </component>

      <component id="encoder.silly"
                service="CastleTest.IEncoder, CastleTest"
                type="CastleTest.SillyEncoder, CastleTest" />

      <component id="encoder.null"
                service="CastleTest.IEncoder, CastleTest"
                type="CastleTest.NullEncoder, CastleTest" />

    </components>
  </castle>

</configuration>

Following is main class where I'm trying to register my component:

namespace CastleTest
{
    class testNewCastle
    {
        static void Main(string[] args)
        {
            IWindsorContainer container = new WindsorContainer();

            **Tried various methods to register components here**

            SecretMessageSender sender = container.Resolve<SecretMessageSender>("messageSender");

            sender.SendMessage("Rahul", "Testing using Castle!");
            Console.Read();
        }
    }
}

Following is SecretMessageSender class:

namespace CastleTest
{
    public interface IEncoder
    {
        string Encode(string source);
    }

    public class SecretMessageSender
    {
        private readonly IEncoder _encoder;
        private readonly string _from;

        public SecretMessageSender(string from, IEncoder encoder)
        {
            _from = from;
            _encoder = encoder;
        }

        public void SendMessage(string to, string body)
        {
            Console.WriteLine("to: {0}\r\nfrom: {1}\r\n\r\n{2}", to, _from, _encoder.Encode(body));
        }
    }
}

Please help me in running this code.

Thanks.

+1  A: 
container.Install(Configuration.FromAppConfig());

More information in the docs about installers.

Mauricio Scheffer
A: 

VS intellisense is not showing any keyword starting with Configuration inside container.Install. Instead it is showing ConfigurationSettings or ConfigurationException. Please guide me to the correct output.

Rahul
don't trust everything intellisense says. You need to import the right namespace.
Krzysztof Koźmic
A: 

How can I use container.Resolve(Type service) in the above code instead of 'container.Resolve'. I need this as I have to pass an object to the resolve function from another function at run time. Please guide me. Thanks.

Rahul
A: 

Now my requirement is to add all my working classes to a class library project & then create a dll for that & then using this library reference I need to call the "sendmessage" method in another project.

Rahul