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 4346262
In Process

The Archive Base Latest Questions

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

I want to use a Ninject.Wcf extension to create a parametrized service host instance.

  • 0

I want to use a Ninject.Wcf extension to create a parametrized service host instance.

For example I have a class MyWCFHandler with the only following constructor:

public MyWCFHandler(UserManager manager)
{
  _manager = manager;
}

But when I write var myServiceHost = new ServiceHost(typeof(MyWCFHandler)); I have no way to pass the dependency object to a constructor.

I don’t want to mess with the custom ServiceHost like offered in
How do I pass values to the constructor on my wcf service?

I decided to go with the Ninject way, but not able to fully understand how to act in my situation.

Here is what I understand WCF Extension in Ninject works:

  1. Reference Ninject and Ninject.Extensions.WCF in my project.
  2. Create a class that inherits Ninject module and write something like:

    internal class ServiceModule : NinjectModule
    {
    public override void Load()
    {
    Bind<IUserManager>().To<UserManager>().WithConstructorParameters(myUserManagerIwantToUseInWCFHandler);
    }
    }

  3. Add a Kernel initialized with new ServiceModule() to a KernelContainer.

  4. Use the NinjectServiceHost like this:

    var service = KernelContainer.Kernel.Get<IMyWCFHandler>();
    _host = new NinjectServiceHost( service );

And there I should have my host ready to be opened.

The questions are:

How should I pass my constructor parameters into a NinjectModule? Should I create an instance of a NinjectModule right when I am ready to bind a parameter to it? How do I pass them to Get method?

Unfortunately there is not one example around to simple show the parametrized ServiceHost start. I don’t even care if it is Ninject I use. Whichever solution has a good example – I am fine with it, since I am just deciding what IoC container to use.

  • 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:09:01+00:00Added an answer on May 21, 2026 at 12:09 pm

    Regarding ninject. The answer is it depends whether you want a singleton service or a new instance per request. With a singleton service you can do the following:

    public class TimeServiceModule : NinjectModule
    {
        /// <summary>
        /// Loads the module into the kernel.
        /// </summary>
        public override void Load()
        {
            this.Bind<ITimeService>().To<TimeService>();
    
            this.Bind<ServiceHost>().ToMethod(ctx => ctx.Kernel.Get<NinjectServiceHost>(new ConstructorArgument("singletonInstance", c => c.Kernel.Get<ITimeService>())));
        }
    }
    
    internal static class Program
    {
        private static void Main()
        {
            var kernel = new StandardKernel(new TimeServiceModule());
    
            var serviceHost = kernel.Get<ServiceHost>();
            serviceHost.AddServiceEndpoint(typeof(ITimeService), new NetTcpBinding(), "net.tcp://localhost/TimeService");
            try
            {
                serviceHost.Open();
            }
            finally
            {
                serviceHost.Close();
            }
        }
    }
    

    Per request approach:

    public interface IServiceTypeProvider
    {
        /// <summary>
        /// Gets the service types.
        /// </summary>
        /// <value>The service types.</value>
        IEnumerable<Type> Types { get; }
    }
    
    Func<Type, ServiceHost> serviceHostFactory
    
            foreach (Type serviceType in this.ServiceTypeProvider.Types)
            {
                // I do some magic here to query base contracts because all our service implement a marker interface. But you don't need this. But then you might need to extend the type provider interface.
                IEnumerable<Type> contracts = QueryBaseContracts(serviceType );
    
                var host = this.CreateHost(serviceType);
    
                foreach (Type contract in contracts)
                {
                    Binding binding = this.CreateBinding();
                    string address = this.CreateEndpointAddress(contract);
    
                    this.AddServiceEndpoint(host, contract, binding, address);
                }
    
                host.Description.Behaviors.Add(new ServiceFacadeBehavior());
    
                this.OpenHost(host);
    
                this.serviceHosts.Add(host);
            }
    
        protected virtual ServiceHost CreateHost(Type serviceType )
        {
            return this.serviceHostFactory(serviceType );
        }
    
    public class YourWcfModule : NinjectModule
    {
        /// <summary>
        /// Loads the module into the kernel.
        /// </summary>
        public override void Load()
        {
    
            this.Bind<Func<Type, ServiceHost>>().ToMethod(
                ctx =>
                (serviceType) =>
                ctx.Kernel.Get<NinjectServiceHost>(new ConstructorArgument("serviceType", serviceType), new ConstructorArgument("baseAddresses", new Uri[] { })));
        }
    }
    

    Have fun

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

Sidebar

Related Questions

I have a WCF web service in which I want to use my Repositories
I write some C# class libary and I want to use Ninject to provide
I want to use Ninject to use in a non MVC ASP.NET 3.5 web
I want to use Ninject in a project which combines ASP.Net webforms and ASP.Net
Here is the scenario. I have a WCF service, when this service is called
I want use A-Z buttons on a html page like shown below (only sample
Below is my stored procedure. I want use stored procedure select all row of
I want to use the mouse scrollwheel in my OpenGL GLUT program to zoom
I want to use Powershell to write some utilities, leveraging our own .NET components
I want to use the functions exposed under the OpenGL extensions. I'm on Windows,

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.