开发者

Castle windsor registering component problem

开发者 https://www.devze.com 2023-01-18 05:27 出处:网络
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.

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 t开发者_JAVA技巧he 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>rahul.ragarwal@patni.com</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.


container.Install(Configuration.FromAppConfig());

More information in the docs about installers.

0

精彩评论

暂无评论...
验证码 换一张
取 消