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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:38:01+00:00 2026-05-28T13:38:01+00:00

I got a usual WCF service set up like this: private ServiceHost serviceHost =

  • 0

I got a usual WCF service set up like this:

private ServiceHost serviceHost = null;

protected override void OnStart(string[] args)
{
    if (serviceHost != null)
        serviceHost.Close(); 

    Uri[] baseAddress = new Uri[]{
    new Uri("net.pipe://localhost")};

    string PipeName = "DatabaseService";

    serviceHost = new ServiceHost(typeof(Kernel), baseAddress); // Kernel implements IDatabase

    serviceHost.AddServiceEndpoint(typeof(IDatabase), new NetNamedPipeBinding(), PipeName);

    serviceHost.Open();
}

protected override void OnStop()
{
   if (serviceHost != null && serviceHost.State != CommunicationState.Closed)
    {
        serviceHost.Close();
        serviceHost = null;
    }
} 

From this code, i guess, one instance of “Kernel” is created, because I got this service running only once.

I create a proxy Object using the ChannelFactory like this:

pipeFactory = new ChannelFactory<IDatabase>(new NetNamedPipeBinding(), new EndpointAddress("net.pipe://localhost/DatabaseService"));
m_Database = pipeFactory.CreateChannel();

I have to say, that my Kernel instance access a local file, and therefore it’s very important I got only once physical instance of this class. I want my service to take care of that but here come’s my problem.

While the service is running and a single channel is created and active, a second client comes up and wants to create a channel too. That works properly but if I start using the proxy Object a FaultException is thrown because a second instance of my Kernel class is created.

Therefore I’m guessing that an instance of the Kernel class is created by every CreateChannel call.

Is it possible to avoid the creation of a new instance and return always a reference to a single Kernel class instance when CreateChannel is called?

Regards,
inva

  • 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-28T13:38:02+00:00Added an answer on May 28, 2026 at 1:38 pm

    Yes, by default, WCF uses the per-session or per-call calling convention, e.g. each incoming service request from a client gets a new, separate instance of your service (implementation) class.

    You can control this, of course, using things like the InstanceContextMode (PerSession is the default – at least on bindings that support it -, PerCall the recommended best practice, and Single is the Singleton) and the ConcurrencyMode settings on your service.

    You can define these either in config, or directly on your service class.

    [ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)] 
    public class CalculatorService : ICalculatorInstance 
    { 
        ...
    }
    

    See the MSDN documentation on WCF Sessions, Instancing and Concurrency for a great and extensive explanation of all details. Also read the excellent MSDN Magazine article Discover Mighty Instance Management Techniques For Developing WCF Apps by Juval Lowy, a great resource always!

    If you do switch your service class to be a singleton (InstanceContextMode=InstanceContextMode.Single), you need to be aware of the two trade-offs:

    • either you define the ConcurrencyMode to also be Single, which effectively means only one single request can ever be handled at once; requests will be serialized, that is, if handling the request takes a fairly long time, subsequent requests will have to start waiting and might end up timing out

    • the other option is to set the ConcurrencyMode to Multiple, then your singleton service class can handle multiple requests at once; but this also means, you have to write your service class in a fully thread-safe manner and you need to synchronize and protect any concurrent access to shared data members – typically a very tricky and hard-to-do-right programming exercise

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

Sidebar

Related Questions

The usual definition for a specialization of a template function is something like this:
We've got a new server with TFS installed. The current environment is very usual,
Got some code that is not mine and its producing this warning atm: iehtmlwin.cpp(264)
IE6+ got some problems with transition as usual but i know ways to get
I've got a table which has the usual ParentID, ChildID as it's first two
I recompiled my classes as usual, and suddenly got the following error message. Why?
I've got a custom CalloutButton component in a package called uiComponents. This custom CallOutButton
I have a WCF web service that works perfectly with an http address, but
I've got the following problem: I am sending a AJAX request to a service
I've got this really annoying space after the li tags of my unordered list

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.