使用Castle.Windsor 3.0问题作为Windows服务托管的WCF服务库

时间:2012-04-08 16:58:07

标签: c# wcf windows-services castle-windsor

我想在Windows服务中托管我的WCF服务库。虽然它将通过本地网络与另一个wcfservice进行通信。

我很难找到最新的,最新的文档或帮助为此目的配置解决方案。任何人都可以建议:

a)这种连接的首选端点是什么? (另一个WCF服务使用basicHttpBinding托管) - 这本身很适合通过劫持global.asax来配置Castle容器。但是,在Windows服务中托管此解决方案意味着我无法再访问global.asax!

b)如何配置Castle Windsor使用此解决方案的DI?目前我已经考虑将其挂钩到App_Code的AppInitilize()方法,以及其他一些不再有效的解决方案。

当前解决方案架构:

*核心(C#类库)

*服务(C#类库)

* WCF服务(WCF服务库)

* Windows服务(Windows服务项目)

AppInitilize()的示例代码[目前似乎无法正常工作]:

public class WindsorConfiguration
    {
        public static IWindsorContainer Container { get; private set; }

        public static void AppInitialize()
        {
            {
                Container = new WindsorContainer()
                    .AddFacility<WcfFacility>()
                    .Register(Component.For<IVirusCheckService>().ImplementedBy<VirusCheckService>()
                                  .LifeStyle.Transient
                                  .AsWcfService(new DefaultServiceModel()
                                                    .AddBaseAddresses("http://localhost:8080/MyService")
                                                    .AddEndpoints(WcfEndpoint.BoundTo(new BasicHttpBinding())
                                                                      .At("basic"))
                                                    .PublishMetadata(o => o.EnableHttpGet())))
                    .Register(Component.For<ILoggingService>().ImplementedBy<LoggingService>());
            }
        }

1 个答案:

答案 0 :(得分:3)

回答问题 a (有点) - 我认为没有“首选”方法 - 这实际上取决于您希望服务的可用性。我托管net.tcp(我怀疑这是Windows服务环境中最常见但我猜),webhttp,basichttp和wshttp服务都可以从windows服务中获得。在 b ...

所以我这样做的方式(也许这只是一个风格的东西,但它适用于我)是我有一个常规的Windows服务,在程序主要我引导容器(所以你会在App_Start内做的东西)在全局asax中)我有安装程序用于我想要安装的应用程序的不同部分(顺便说一句,通常在WCF中我将合同拆分成自己的程序集并在windows服务程序集中实现它们,或者在单独的程序集中如果我需要在多个地方托管相同的服务)。

一旦容器被引导,我就从容器中解析ServiceBase并将其提供给ServiceBase.Run静态方法。这样我的服务就可以依赖于在容器中注册的任何其他内容(这是我在容器上调用Resolve的唯一地方,但我认为在这种情况下这是合理的)。

我不知道除了托管WCF服务之外你还希望你的服务做什么,也许什么都没有,但这是一个让你走的框架,所以这里是......

static class Program
{
    static void Main()
    {
        ServiceBase.Run(CreateContainer().Resolve<ServiceBase>());
    }

    private static IWindsorContainer CreateContainer()
    {
        var container = new WindsorContainer();
        container.Install(FromAssembly.This());
        return container;
    }
}

public class ServicesInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container
            .AddFacility<WcfFacility>(f =>
                                          {
                                              f.CloseTimeout = TimeSpan.Zero;
                                          })
            .Register(
                Component
                    .For<IVirusCheckService>()
                    .ImplementedBy<VirusCheckService>()
                    .LifeStyle.Transient
                    .AsWcfService(new DefaultServiceModel()
                                      .AddBaseAddresses("http://localhost:8080/MyService")
                                      .AddEndpoints(WcfEndpoint.BoundTo(new BasicHttpBinding())
                                                        .At("basic"))
                                      .PublishMetadata(o => o.EnableHttpGet())),
                Component
                    .For<ServiceBase>()
                    .ImplementedBy<MyService>());
    }
}

注意:MyService类只是一个常规的Windows服务类(如果你喜欢的话,你可以使用一个visual studio为你生成文件| new | windows服务) - 我没有显示它因为它可以只是一个空的实现。