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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:36:17+00:00 2026-06-06T00:36:17+00:00

In PerSession how do I get Dispose() on the service to fire? In the

  • 0

In PerSession how do I get Dispose() on the service to fire? In the code below Dispose() does not get called. Neither when I call .Close() nor if I let the session time out.

If I change the service to PerCall Dispose() is called (with each method call). With PerSession I am getting a session (tested with serviceStartTime).

Service

[ServiceBehavior (InstanceContextMode=InstanceContextMode.PerSession)]
public class MagicEightBallService : IEightBall, IDisposable
{
    private DateTime serviceStartTime;
    public void Dispose()
    {
        Console.WriteLine("Eightball dispose ... " + OperationContext.Current.SessionId.ToString());
    }
    public MagicEightBallService()
    {
        serviceStartTime = DateTime.Now;
        Console.WriteLine("Eightball awaits your question " + OperationContext.Current.SessionId.ToString() + " " + serviceStartTime.ToLongTimeString());
    }
    public string ObtainAnswerToQuestion(string userQuestion)
    {
        return "maybe " + OperationContext.Current.SessionId.ToString() + " " + serviceStartTime.ToLongTimeString();
    }

Client

    using (EightBallClient ball = new EightBallClient())
    {    
        while (true)
        {
            Console.Write("Your question: ");
            string question = Console.ReadLine();
            if (string.IsNullOrEmpty(question)) break;
            try
            {
                string answer = ball.ObtainAnswerToQuestion(question);
                Console.WriteLine("8-ball says: {0}", answer);
            }
            catch (Exception Ex)
            {
                Console.WriteLine("ball.ObtainAnswerToQuestion exception " + Ex.Message);
            }               
        }
        ball.Close();
     }

Service-Contract

[ServiceContract (SessionMode = SessionMode.Required)]
public interface IEightBall
{
    [OperationContract]
    string ObtainAnswerToQuestion(string userQuestion);

    [OperationContract]
    sDoc GetSdoc(int sID);

    DateTime CurDateTime();
}

Host

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="WSHttpBinding_ISampleService" 
                 closeTimeout="00:01:00" openTimeout="00:01:00" 
                 receiveTimeout="00:10:00" sendTimeout="00:10:00">
          <security mode="Message" />
          <reliableSession ordered="true"
                   inactivityTimeout="00:10:00"
                   enabled="true" />
        </binding>
      </wsHttpBinding>
    </bindings>
    <services>
      <service name="MajicEightBallServiceLib.MagicEightBallService"
               behaviorConfiguration="EightBallServiceMEXBehavior" >
        <endpoint address=""
                  binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ISampleService"
                  contract="MajicEightBallServiceLib.IEightBall">
        </endpoint>
        <endpoint address="mex"
                  binding ="mexHttpBinding"
                  contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8000/MagicEightBallService"/>
          </baseAddresses>
        </host>             
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="EightBallServiceMEXBehavior">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

namespace MagicEightBallServiceHost
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("**** Console Based WCF Host *****");

            using (ServiceHost serviceHost = new ServiceHost(typeof(MagicEightBallService)))
            {
                serviceHost.Open();
                Console.WriteLine("The service is running");
                Console.ReadLine();
            }
        }
    }
}
  • 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-06-06T00:36:18+00:00Added an answer on June 6, 2026 at 12:36 am

    Dispose() method will be fired. The only question question is “When?”.

    Answer to that question depends on the service configuration.

    There are several possible scenarios:

    1. Session is not supported by binding
    2. Normal session
    3. Reliable session

    Dispose() is fired when session is closed for PerSession context mode. So we need to check how long does session live in different scenarios.

    For some configurations (for example default BasicHttpBinding) session is not started at all. In case of session-less configuration PerCall and PerSession context modes have no difference and Dispose method will be called very soon after your main method executed.

    When Session is Enabled it can be closed explicitly by client or by timeout. Normally it is controlled by client. Client initiates session before making first call to service and closes it when Client object is closed.

    ServiceClient proxy = new ServiceClient();
    Console.WriteLine(proxy.GetData(123));
    proxy.Close();  
    

    proxy.Close() method above closes the session in server what in turn executes Dispose().

    Session management is a big performance driver because it requires additional calls between client and server to be performed.

    So normally Dispose will be called when Client wants to close the session.

    If client did not close session for any reason it will be closed by service host after certain period of time. That period is controlled by Binding.ReceiveTimeout property.
    Default value for that property is 10 min.

    Session will be closed and (Dispose() fired) if nobody sent request to server with certain Session Id for 10 min.
    This default timeout can be changed by setting receiveTimeout to some shorter value in web.config.

    <wsHttpBinding>
      <binding name="wsHttpEndpointBinding" receiveTimeout="00:00:05">
      </binding>
    </wsHttpBinding> 
    

    ReliableSession.InactivityTimeout is additionally checked when Reliable session is enabled. It is also defaulted to 10 min.

    It works as expected in self-hosted and IIS-hosted services.

    Try to update your client code as follows to test:

    using (EightBallClient ball = new EightBallClient())
    {    
        ball.ObtainAnswerToQuestion("test");
        ball.Close();
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not going to get specific here with code, because I don't feel like
My requirement is to call WCF web service from ASP.NET code behind and pass
If I run my WCF service(hosted in II7) and uses PerSession on contextinstance, will
I have a self-hosted WCF service with the InstanceContextMode set to PerSession. How can
When I want to connect to MySQL by Entity framework (code first) I get
i have configured a WCF service with wsHTTPBinding but even then i get the
In the below code i want the GetClassTeacher method to be executed only once
I have a WCF service with ServiceBehavior.InstanceContextMode = InstanceContextMode.PerSession . How do I need
If you have the read_stream permission and you get the posts of a facebook
On some action in my admin I get a Permission denied page, and in

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.