问题
I am wanting to host my WCF Service Library in a windows service. Although it will be communicating to another wcfservice over a local network.
I am having great difficulty in finding recent, up-to-date documentation or help to configure the solution for this purpose. Can anyone advise:
a) What the preferred endpoint for this kind of connection? (The other WCF service is hosted using a basicHttpBinding) - this in itself lends itself well to configuring the Castle container through hijacking the global.asax. However, hosting this solution in a windows service means i no longer have access to a global.asax!
b) How to configure Castle Windsor to use DI with this solution? Currently I've looked into hooking it into the AppInitilize() method of App_Code, and some other, no longer valid solutions.
Current Solution architecture:
*Core (C# Class Library)
*Services (C# Class Library)
*WCF Servics (WCF Service Library)
*Windows Service (Windows Service Project)
Sample Code for AppInitilize() [ which doesnt currently seem to be working ]:
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:
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.
来源:https://stackoverflow.com/questions/10064586/wcf-service-library-hosted-as-a-windows-service-using-castle-windsor-3-0-issue