WCF Service Library hosted as a Windows Service using Castle.Windsor 3.0 issue

淺唱寂寞╮ 提交于 2019-12-04 19:13:20

To answer question a (sort of) - I do not think there is a "preferred" approach - it really just depends how you want your service to be available. I've hosted net.tcp (I suspect this is the most common in a windows service environment but I am guessing), webhttp, basichttp and wshttp services all fine from inside a windows service. Onto b...

So the way I do this (and maybe this is just a style thing but it works for me) is that I have a regular windows service and in the program main I bootstrap the container (so stuff that you would do inside App_Start in global asax) and I have installers for the different parts of the application I want to install (incidentally, generally in WCF I split the contracts out into their own assembly and implement them inside the windows service assembly, or in a separate assembly if I need to host the same service in multiple places).

Once the container is bootstrapped, I then resolve the ServiceBase from the container and give it to the ServiceBase.Run static method. This way my service can have dependencies on whatever else is registered in the container (this is the only place I would ever call Resolve on the container but I think it is justified in this circumstance).

I don't know what else you want your service to do other than hosting WCF services, maybe nothing, but this is a bit of framework to get you going so here it is...

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

NOTE: The MyService class is just a regular Windows service class (you can use the one visual studio generates for you when you go file | new | windows service if you like) - I have not shown it because it can just be an empty implementation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!