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

The Archive Base Latest Questions

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

Background I am creating an ASP.net application which uses a custom library (not yet

  • 0

Background
I am creating an ASP.net application which uses a custom library (not yet publicly released, but soon) to provide a long polling handler to a javascript client in jQuery. The client will call the handler, wait until the handler returns some data, display it and then hit the handler again… wash, rinse, repeat.

The handler’s job is to take the request, stick it into a collection (normally a List<>) and wait for an event, containing the data to return to the awaiting requests. I have tested this, using a simple event within a static class on my server and this all works fine.

Problem
In order for the messages to be received by all clients, in all AppDomains on my server, I have created a simple C# console app – which is running in the background and provides a DataContract over WCF (NamedPipe). I then created a client object in ASP.net which can talk to the console app over the WCF interface.

I can synchronously pull/push messages into my cache – and this is working fine. However, when I try and use the Asynchronous pattern on the WCF contract, the EndMethod is fired before I ask it to, straight after returning the IAsyncResult to the BeginMethod.

I cannot see what is wrong – an Google has plenty of resources which show weak examples, and they dont help.

What I would like
Is their anyway to bind to an event over WCF through the data contract? That way I can just attach to the event and be done with it. New messages into the cache trigger my event, which triggers the async handler to return all the awaiting requests.

Is this possible, and if so does anyone have any samples/tutorials/examples?

Code To Follow…

  • 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-06T02:50:37+00:00Added an answer on June 6, 2026 at 2:50 am

    I know this is possible as I have done this, and know that there is limited info on this out there (I plan on creating a blog post soon). Basically, you need to create Subscribe/Unsubscribe WCF methods, which will keep your port open.

    Your server code should look like this:

    [ServiceContract(SessionMode = SessionMode.Required,
        CallbackContract = typeof(IProcessorCallBack))]
    public interface IProcessor
    {
        [OperationContract]
        Boolean SubscribeToEvents();
    
        [OperationContract]
        Boolean UnsubscribeToEvents();
    }
    
    public interface IProcessorCallBack
    {
        [OperationContract(IsOneWay = true)]
        void OnEvent(EventArgs args);
    }
    
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
    public class DDSpeechMikeProcessor:IProcessor
    {
        private readonly List<IProcessorCallBack> _eventListeners = 
            new List<IProcessorCallBack>();
    
        public bool SubscribeToEvents()
        {
            var listenerToCallBacks =    
                OperationContext.Current.GetCallbackChannel<IProcessorCallBack>();
            if(!_eventListeners.Contains(listenerToCallBacks))
                _eventListeners.Add(listenerToCallBacks);
        }
    
        public bool UnsubscribeToEvents()
        {
            var listenerToCallBacks = 
                OperationContext.Current.GetCallbackChannel<IProcessorCallBack>();
            if (_eventListeners.Contains(listenerToCallBacks))
                _eventListeners.Remove(listenerToCallBacks);
        }
    
        //This is your server listening for the main event
        //which it will pass on to all listeners
        void OnEvent(EventArgs args)
        {
            foreach(var listener in _eventListeners)
            {
                try
                {
                    listener.OnEvent(args);
                }
                catch (Exception exception)
                {
                    RemoveListenerIfBadCommunication(listener, exception);
                }
            }
        }
    
        //If a listener did not unsubscribe before shutting down you will get exceptions
        private void RemoveListenerIfBadCommunication(IProcessorCallBack listener, 
            Exception exception)
        {
            if (exception.GetType() == typeof(CommunicationException)
                || exception.GetType() == typeof(CommunicationObjectAbortedException)
                || exception.GetType() == typeof(CommunicationObjectFaultedException)
            )
               _eventListeners.Remove(listener);
        }
    }
    

    In your client code:

    public class Client : IProcessorCallBack
    {
        DuplexChannelFactory<IProcessor> _processorFactory;
        IProcessor _processor
        void OpenProcessor()
        {
            _speechMikeProcessorFactory = new DuplexChannelFactory<IProcessor>(
                  this,
                  new NetNamedPipeBinding(),
                  new EndpointAddress(baseUri + @"/" + HostName));
            _processor = _speechMikeProcessorFactory.CreateChannel();
            _processor.SubscribeToEvents();
        }
    
        void OnEvent(EventArgs args)
        {
             //Do Stuff
        }
    }
    

    Let me know if I need to explain anything in more detail

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

Sidebar

Related Questions

I am creating an asp web application that can't use JQuery and uses custom
I develop a web by using ASP.NET MVC2. Creating the cookie in the background.
I'm creating an application Appcelerator Titanium which as an external background image (wood with
Background: I'm testing a function within an ASP.NET 4.0 (Web Forms not MVC) and
I am writing an asp.net program for creating reservations in which I have a
Background We are developing some in-house utilities using ASP.NET 2.0. One of which is
Background: I'm creating a very simple chatroom-like ASP.NET page with C# Code-Behind. The current
Background: I'm in the process of creating a web service using ASP.NET 2.0. This
This requires a bit of background. I'm creating a web app (ASP.NET/VB.NET with SQL
I creating a program which send newsletter with a background image. It works fine

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.