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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:41:18+00:00 2026-05-28T17:41:18+00:00

in the process of creating a WCF service I ran into a term that’s

  • 0

in the process of creating a WCF service I ran into a term that’s new to me. Basically when specifying the InstanceContextMode I have a few options, including; PerSession, PerCall and Single. Here’s the code from the sample I’m learning from:

[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
public class EvalService : IEvalService { ...

Now, he stated by doing this that only one instance of my service would be created at runtime. What does this mean? I thought that everytime a connection was made to the web service that it was treated as a seperate instance.

Does it persist, this instance of my service, for every request made? Judging by the other members mentioned in the docs, is it safe to assume this is the way it works?

  • 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-28T17:41:19+00:00Added an answer on May 28, 2026 at 5:41 pm

    Per the docs:

    Only one InstanceContext object is used for all incoming calls and is
    not recycled subsequent to the calls. If a service object does not
    exist, one is created.

    So there is only one instance, and it’s not cleaned up after a call is made. This is like a Singleton for your WCF service. So you need to be careful about shared memory and resources.

    To answer your question – yes, this is the way it works.

    UPDATE Added sample:
    I modified a few samples from MSDN to show the effects of InstanceContextMode.Single. You’ll see the operation count will continue to increment even though I use two different clients. If I change the InstanceContextMode to PerCall, the count will be different (it will be zero).

    self-hosted service:

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class CalculatorService : ICalculatorInstance
    {
        static Object syncObject = new object();
        static int instanceCount;
        int instanceId;
        int operationCount;
    
        public CalculatorService()
        {
            lock (syncObject)
            {
                instanceCount++;
                instanceId = instanceCount;
            }
        }
    
        public double Add(double n1, double n2)
        {
            operationCount++;
            return n1 + n2;
        }
    
        public double Subtract(double n1, double n2)
        {
            Interlocked.Increment(ref operationCount);
            return n1 - n2;
        }
    
        public double Multiply(double n1, double n2)
        {
            Interlocked.Increment(ref operationCount);
            return n1 * n2;
        }
    
        public double Divide(double n1, double n2)
        {
            Interlocked.Increment(ref operationCount);
            return n1 / n2;
        }
    
        public string GetInstanceContextMode()
        {   // Return the InstanceContextMode of the service
            ServiceHost host = (ServiceHost)OperationContext.Current.Host;
            ServiceBehaviorAttribute behavior = host.Description.Behaviors.Find<ServiceBehaviorAttribute>();
            return behavior.InstanceContextMode.ToString();
        }
    
        public int GetInstanceId()
        {   // Return the id for this instance
            return instanceId;
        }
    
        public int GetOperationCount()
        {   // Return the number of ICalculator operations performed 
            // on this instance
            lock (syncObject)
            {
                return operationCount;
            }
        }
    }
    
    public class Program
    {
    
        static void Main(string[] args)
        {
            Uri baseAddress = new Uri("http://localhost:12345/calc");
            using (ServiceHost host = new ServiceHost(typeof(CalculatorService), baseAddress))
            {
                // Enable metadata publishing.
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true;
                smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
                host.Description.Behaviors.Add(smb);
    
                // Open the ServiceHost to start listening for messages. Since
                // no endpoints are explicitly configured, the runtime will create
                // one endpoint per base address for each service contract implemented
                // by the service.
                host.Open();
    
                Console.WriteLine("The service is ready at {0}", baseAddress);
                Console.WriteLine("Press <Enter> to stop the service.");
                Console.ReadLine();
    
                // Close the ServiceHost.
                host.Close();
            }
            Console.WriteLine();
            Console.WriteLine("Press <ENTER> to terminate client.");
            Console.ReadLine();
        }
    }
    

    client:

    class Program
    {
        static void Main()
        {
            // Create a client.
            CalculatorInstanceClient client = new CalculatorInstanceClient();
            string instanceMode = client.GetInstanceContextMode();
            Console.WriteLine("InstanceContextMode: {0}", instanceMode);
            Console.WriteLine("client1's turn");
            Console.WriteLine("2 + 2 = {0}", client.Add(2, 2).ToString());
            Console.WriteLine("3 - 1 = {0}", client.Subtract(3, 1).ToString());
            Console.WriteLine("number of operations = {0}", client.GetOperationCount().ToString());
    
            // Create a second client.
            CalculatorInstanceClient client2 = new CalculatorInstanceClient();
    
            Console.WriteLine("client2's turn");
            Console.WriteLine("2 + 2 = {0}", client2.Add(2, 2).ToString());
            Console.WriteLine("3 - 1 = {0}", client2.Subtract(3, 1).ToString());
            Console.WriteLine("number of operations = {0}", client2.GetOperationCount().ToString());
    
            Console.WriteLine();
            Console.WriteLine("Press <ENTER> to terminate client.");
            Console.ReadLine();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm in the process of creating a web service that uses XML to handle
I am creating a WCF web service using wsHttpBinding and a corresponding application that
I'm trying to test a WCF service without creating a WCF client. I have
Have a WCF service (hosted in IIS 6.0) that takes an XML feed from
I have a WCF Web Service that has no concurrency configuration in the web.config,
I have a WCF host that listen to a topic and process incoming messages.
i am new to wcf. i have created and hosted simple wcf service on
I have a WCF service that occasionally causes a database deadlock. My service allows
Suppose that a process is creating a mutex in shared memory and locking it
I am in the process of creating an iPhone app that requires to interact

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.