Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4381620
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T12:40:19+00:00 2026-05-21T12:40:19+00:00

I’ve created a WCF service library with a simple ‘hello world’ test service and

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T12:40:20+00:00Added an answer on May 21, 2026 at 12:40 pm

    I found another question about using a custom ServiceHostFactory to perform bootstrapping, which is set up via a *.svc file. *.svc files 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.Config file, 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 the App.config file in the service library and edited to suit).

    For example:

    <system.serviceModel>
      <services>
        <service behaviorConfiguration="MyServiceBehavior" name="BelfryImages.QueryService.Implementation.HelloWorld">
          <endpoint address="HelloWorld" binding="wsHttpBinding" name="HelloWorld" contract="BelfryImages.QueryService.Contracts.IHelloWorld" />
        </service>
      </services>
    
      <behaviors>
        <serviceBehaviors>
          <behavior name="MyServiceBehavior">
            <serviceMetadata httpGetEnabled="true" />
            <serviceDebug includeExceptionDetailInFaults="true" />
          </behavior>
        </serviceBehaviors>
      </behaviors>
    
      <bindings>
      </bindings>
    </system.serviceModel>
    

    Note that the BelfryImages.QueryService.Implementation.HelloWorld is 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.dll is the WCF Service Library.

    I added a new blank .svc file to the service application, named to match the endpoint, such as HelloWorld.svc. Usually, adding a WCF Service to a WCF service application results in a .svc file and a .cs codebehind file, which contains the implementation of the service. I manually added just the .svc file with no codebehind and pointed it to the service library implementation:

    <%@ ServiceHost Language="C#" Debug="true" Service="BelfryImages.QueryService.Implementation.HelloWorld" %>
    

    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/HelloWorld to http://localhost:XXXXX/HelloWorld.svc/HelloWorld.

    I then created the ServiceHostFactory to 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 purpose CustomHostFactory class:

    public class CustomHostFactory
        : ServiceHostFactory
    {
        protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {
            // perform initialisation:
            ...
    
            var serviceHost = base.CreateServiceHost(serviceType, baseAddresses);
            return serviceHost;
        }
    }
    

    This is then bound to the service within the .svc file by adding a Factory attribute:

    <%@ ServiceHost Language="C#" Debug="true" Service="BelfryImages.QueryService.Implementation.HelloWorld" Factory="BelfryImages.WcfService.CustomHostFactory" %>
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
In order to apply a triggered animation to all ToolTip s in my app,
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.