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

  • SEARCH
  • Home
  • 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 533859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:33:07+00:00 2026-05-13T09:33:07+00:00

I have WCF services structured like suggested by Miguel Castro . This means that

  • 0

I have WCF services structured like suggested by Miguel Castro. This means that I have set everything up manually, and have a console application hosting my services using ServiceHost objects.

I want to keep my service classes thin, and they are currently just passing on calls to behavior classes. My problem now is unit testing the service classes. I want to inject something to the classes as a constructor parameter such that I can mock this away and write proper isolated unit tests. The ServiceHost class doesn’t seem to accept arguments, so my question is how I can inject data to the service classes – or can’t I?

  • 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-13T09:33:08+00:00Added an answer on May 13, 2026 at 9:33 am

    WCF supports Constructor Injection, but you have to jump through a few hoops to get there. The key lies in writing a custom ServiceHostFactory. While that, too, must have a default constructor, you can use it to wire up all the correct behaviors.

    As an example, I recently wrote one that uses Castle Windsor to wire up dependencies for the service implementation. The implementation of CreateServiceHost simply does this:

    return new WindsorServiceHost(this.container, serviceType, baseAddresses);
    

    where this.container is a configured IWindsorContainer.

    WindsorServiceHost looks like this:

    public class WindsorServiceHost : ServiceHost
    {
        public WindsorServiceHost(IWindsorContainer container, Type serviceType, params Uri[] baseAddresses)
            : base(serviceType, baseAddresses)
        {
            if (container == null)
            {
                throw new ArgumentNullException("container");
            }
    
            foreach (var cd in this.ImplementedContracts.Values)
            {
                cd.Behaviors.Add(new WindsorInstanceProvider(container));
            }
        }
    }
    

    and WindsorInstanceProvider looks like this:

    public class WindsorInstanceProvider : IInstanceProvider, IContractBehavior
    {
        private readonly IWindsorContainer container;
    
        public WindsorInstanceProvider(IWindsorContainer container)
        {
            if (container == null)
            {
                throw new ArgumentNullException("container");
            }
    
            this.container = container;
        }
    
        #region IInstanceProvider Members
    
        public object GetInstance(InstanceContext instanceContext, Message message)
        {
            return this.GetInstance(instanceContext);
        }
    
        public object GetInstance(InstanceContext instanceContext)
        {
            var serviceType = instanceContext.Host.Description.ServiceType;
            return this.container.Resolve(serviceType);
        }
    
        public void ReleaseInstance(InstanceContext instanceContext, object instance)
        {
            this.container.Release(instance);
        }
    
        #endregion
    
        #region IContractBehavior Members
    
        public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
        {
        }
    
        public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
        }
    
        public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
        {
            dispatchRuntime.InstanceProvider = this;
        }
    
        public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
        {
        }
    
        #endregion
    }
    

    This may look like a lot, but notice that it’s reusable, general-purpose code that has a rather low cyclomatic complexity.

    You can follow the same coding idiom to implement Dependency Injection with another DI Container or by using Poor Man’s DI.

    Here’s an older writeup of this idiom that uses Poor Man’s DI.

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

Sidebar

Related Questions

I have six wcf services that I'm hosting in a windows service. Everything works
I have a series of WCF services that invoke methods on the client again
I have a large number of C# WCF services that are being called by
I have a WCF solution that consists of the following class libraries: Exercise.Services: Contains
I have a WPF application that uses a WCF services to perform operations on
I have a web application that exposes web services using WCF and wsHttpBindings. It
I have some WCF services that are hosted by IIS with ASP.Net compatibility turned
I have several wcf services which are hosted using ServiceHost class. Now , I
I have 2 WCF services, (A and B), where A calls B. WCF Service
I have a wcf services projects and a second project for consuming these services

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.