I’ve created a WCF service library with a simple ‘hello world’ test service and a properly configured App.config file, so that when I start my client application the WCF service is started via Visual Studio’s built-in host. The service as it stands is working fine with the external configuration in my client.
I need to run some initialisation code to set up DI, data access, logging etc. I’ve written a console host that can do that, and the service itself will eventually be deployed as a Windows service, but I want to use the built-in host so that I don’t have to manually restart the service during development.
Is there some way I can hook some code in the library to be called on startup?
I found another question about using a custom
ServiceHostFactoryto perform bootstrapping, which is set up via a*.svcfile.*.svcfiles are part of a WCF Service Application, and can’t be used directly by a WCF Service Library. I want to stick to using a service library for some flexibility with my implementation of the services and the eventual production hosting, but using a WCF Service Application would get the job done in a way that suits development (and would probably be easy enough to drop in to IIS for production hosting with an alternate set of configuration). So I figured that I just needed to create a WCF Service Application that acts as a host for the services in the service library, and performs the required initialisation.I first created a new WCF Service Application, dropped the default service files created by VS, and added a reference to the existing service library and other dependencies.
In the service app’s
Web.Configfile, under the<configSections>..</configSections>section (which is required to be the first node after the<configuration>tag), I added the<system.serviceModel>section for the service library (this can be extracted from theApp.configfile in the service library and edited to suit).For example:
Note that the
BelfryImages.QueryService.Implementation.HelloWorldis the FQN of the service implementation (implementing class) of the service contract (interface)BelfryImages.QueryService.Contracts.IHelloWorld. I actually have these in two separate assemblies;BelfryImages.QueryService.Contracts.dllis the WCF Service Library.I added a new blank
.svcfile to the service application, named to match the endpoint, such asHelloWorld.svc. Usually, adding a WCF Service to a WCF service application results in a.svcfile and a.cscodebehind file, which contains the implementation of the service. I manually added just the.svcfile with no codebehind and pointed it to the service library implementation:As it stood this was usable as a drop-in replacement for the VS-hosted service library, after first changing the client’s service URL from
http://localhost:XXXXX/QueryService/HelloWorldtohttp://localhost:XXXXX/HelloWorld.svc/HelloWorld.I then created the
ServiceHostFactoryto perform initialisation for the service. I found an MSDN article (Hosting and Consuming WCF Services) that explains how to do this (Listings 5-6 and 5-7) – for my purposes I just added a simpler, general purposeCustomHostFactoryclass:This is then bound to the service within the
.svcfile by adding aFactoryattribute:The initialisation at the top of the
CreateServiceHost()override is performed before the service is created as usual. This only seems to happen once per service, rather than once per call to the service, but there will be overhead for multiple services. That could probably made a one-off by using a static flag or similar. For the moment it seems to work fine.