3

我正在尝试在 Windows 服务主机中托管我的 WCF 服务。为了管理依赖项,我使用 Castle WCF 工具。

这就是我的 ContainerConfiguration(BootStrapper) 的样子:

 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))));

            }
        }

这就是我在服务主机内进行托管的方式:

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;
           }
        }
    }

我的 ServiceOne 实现如下:

[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
        }
}

现在我的问题是:如何将 IDependency 传递给容器?我将如何配置它,以便在托管它时,它不会抱怨不让主机知道依赖关系并继续查找和故障转移默认构造函数实现?

谢谢,-迈克

4

1 回答 1

1

您需要在WCF ServiceHost而不是 Windows 服务代码中为 WCF 服务启动 Windsor 容器。查看有关Castle WCF 设施Windows 服务中的WCF 的这两篇博客文章,了解您需要做什么。基本上,Windows 服务代码只是启动您从 Castle WCF 工具获得的 WCF ServiceHostFactory,而该工厂是实际配置 Castle 容器的内容。

于 2012-06-15T12:53:54.790 回答