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

The Archive Base Latest Questions

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

I created a duplex service ( NetTcpBinding ) like this example : I used

  • 0

I created a duplex service (NetTcpBinding) like this example: I used the publish-subscribe pattern, and for each connection request from a new client, it creates a new instance of the service (containing a different callback). In this example, the callback is invoked through events and delegates.

Now I would like to change this example: suppose we do not want to respond immediately to client’s request, that is, suppose we want to invoke the callback method after a certain time interval. In this case I need to maintain a reference to the method of the callback… But what happens if in the meantime some client disconnects? The service instance is destroyed and we lose even the callback…

I wrote this example:

  • MySingletonTable is the data structure that stores the references to the methods of the callbacks;
  • SampleService is not a service, but simulates the instance of a service;

    public delegate void ProcessingHandler(string item, double price, double change);
    
    public class MySingletonTable
    {
        private static volatile MySingletonTable m_Instance;
        private static object syncRoot = new object();
    
        private static Dictionary<string, ProcessingHandler> pointersToHandlers;
    
        private MySingletonTable()
        {
            pointersToHandlers = new Dictionary<string, ProcessingHandler>();
        }
    
        // Return the singleton instance of this class.
        public static MySingletonTable Instance
        {
            get
            {
                if (m_Instance == null)
                {
                    lock (syncRoot)
                    {
                        if (m_Instance == null)
                            m_Instance = new MySingletonTable();
                    }
                }
                return m_Instance;
            }
        }
    
        /// The number of the entries in the table.
        public int Count
        {
            get
            {
                lock (syncRoot)
                {
                    return pointersToHandlers.Count;
                }
            }
        }
    
        // Add an handler.
        public void Add(string id, ProcessingHandler handler)
        {
            lock (syncRoot)
            {
                if (!pointersToHandlers.ContainsKey(id))
                    pointersToHandlers.Add(id, handler);
            }
        }
    
        // Get an handler from the table.
        public ProcessingHandler GetHandler(string id)
        {
            ProcessingHandler handler = null;
            lock (syncRoot)
            {
                if (pointersToHandlers.ContainsKey(id))
                    handler = pointersToHandlers[id];
            }
            return handler;
        }
    
        // Remove the specified handler.
        public bool Remove(string id)
        {
            lock (syncRoot)
            {
                return pointersToHandlers.Remove(id);
            }
        }
    }
    
    // This class simulates the service.
    public class SampleService
    {
        private static int counter = 0;
        private int service_i = ++counter;
    
        MySingletonTable reference = MySingletonTable.Instance;
    
        public SampleService(string id)
        {
            reference.Add(id, PriceChange);
        }
    
        private void PriceChange(string item, double price, double change)
        {
            // call the callback
            // ...
            Console.WriteLine("service_i {0}: {1} {2} {3}", service_i, item, price, change);
        }
    }
    
    
    class Program
    {
        static void Main(string[] args)
        {
            SampleService s1 = new SampleService("abc");
            SampleService s2 = new SampleService("def");
    
            MySingletonTable table = MySingletonTable.Instance;
            ProcessingHandler handler = null;
    
            handler = table.GetHandler("abc");
            handler("item one", 10, 20);
    
            handler = table.GetHandler("def");
            handler("item two", 30, 40);
    
            Console.ReadLine();
        }
    }
    

Obviously I can not explicitly destroy the two service instances simulated in this example. What would happen, however, if s1 and s2 were two instances of a service related to two different clients?

  • 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-06T10:32:18+00:00Added an answer on June 6, 2026 at 10:32 am

    If your delegate is stored in pointersToHandlers, then this will be holding onto your object so it is never going to be garbage collected, or as you call it, ‘destroyed’. Effectively, you also have a memory leak right now.

    You will need to remove the delegate from the list when clients disconnect (or whenever you are expecting the object to be destroyed). Not sure why you are not using events, but that is another question. Even if you do use events, you will still need to disconnect once you are done so that you dont end up with this situation.

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

Sidebar

Related Questions

Created .NET WCF service, tested it - works. Generated schemas from Data and service
I'm trying to set duplex service. I have created 2 interfaces: the first -
I am connecting to a duplex WCF service with an x509 cert, specifying the
I have a VERY complex service host which consists of multiple DUPLEX services here.
Created a custom validation attribute and I would like to get it to work
created a collection in MongoDB consisting of 11446615 documents. Each document has the following
Created a WCF service that is being called by a Silverlight app. Both reside
Created a AlertDialog that is used to prompting the user to enter a keyword.
I am creating a client service application using WCF duplex callback. The service sends
Created a simple WCF service that basically logs to a db. the build is

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.