Mike
Mike

Reputation: 3284

Configuring WCF dependencies

I'm trying to host my WCF service in a windows service host. For managing dependencies I'm using Castle WCF facility.

This is how my ContainerConfiguration(BootStrapper) looks like:

 public class ConfigureContainer : IConfigureContainer
        {
            private const string ServiceOne= "ServiceOne";
            private const string ServiceTwo      = "ServiceTwo";
            private const string ServiceThree = "ServiceThree";
            private const string CurrentAssembly      = "MyAssembly";

            private readonly IWindsorContainer container;

            public ConfigureContainer(IWindsorContainer container)
            {
                this.container = container;
            }


            public IWindsorContainer WindsorContainer { get { return container; } }


            public void AndRegisterComponents()
            {
                container.Register(AllTypes.FromAssemblyNamed(CurrentAssembly)
                       .Pick().If(type => type.GetInterfaces().Any(i => i.IsDefined(typeof(ServiceContractAttribute), true)))
                       .Configure(configurer => configurer
                                                    .Named(configurer.Implementation.Name)
                                                    .AsWcfService(
                                                        new DefaultServiceModel()
                                                            .AddEndpoints(
                                                                WcfEndpoint.FromConfiguration(ServiceOne),
                                                                WcfEndpoint.FromConfiguration(ServiceTwo),
                                                                WcfEndpoint.FromConfiguration(ServiceThree))
                                                                .PublishMetadata()))
                       .WithService.Select((type, baseTypes) => type.GetInterfaces()
                           .Where(i => i.IsDefined(typeof(ServiceContractAttribute), true))));

            }
        }

This is how I do my hosting inside the service host:

partial class DataServicesHost : ServiceBase
    {
        private IWindsorContainer windsorContainer;
        public DataServicesHost()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            var configure = new ConfigureContainer();
            windsorContainer = configure.WindsorContainer;
        }

        protected override void OnStop()
        {
           if(windsorContainer != null)
           {
             windsorContainer.Dispose();
             windsorContainer = null;
           }
        }
    }

My ServiceOne is implemented as follows:

[ServiceContract]
    internal interface IServiceOne
    {
        [OperationContract]
        void DoSomething();
    }

    public class ServiceOne : IServiceOne
    {
        private readonly IDependency dependency;

        public ServiceOne(IDependency dependency)
        {
            this.dependency = dependency;
        }

        public void DoSomething()
        {
            dependency.GetSomething();
            //do something
        }
    }

    public interface IDependency
    {
        void GetSomething();
    }

    public class Dependency : IDependency
    {
        public void GetSomething()
        {
            //GetSomeThing
        }
}

Now my question is: how do I pass the IDependency to the container? How will I configure it so that while hosting it, it does't complain about not letting the host know of the dependency and keeps looking and failing over the default constructor implementation?

Thanks, -Mike

Upvotes: 3

Views: 314

Answers (1)

Sixto Saez
Sixto Saez

Reputation: 12680

You need to spin up the Windsor container for the WCF service inside the WCF ServiceHost not the Windows service code. Look at these two blog posts on the Castle WCF facility and WCF in a Windows Service to see what you'll need to do. Basically, the Windows service code just spins up a WCF ServiceHostFactory that you get from the Castle WCF facility and that factory is what actually configures the Castle container.

Upvotes: 1

Related Questions