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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:10:14+00:00 2026-05-14T06:10:14+00:00

Here is my situation. I have written a WCF service which calls into one

  • 0

Here is my situation. I have written a WCF service which calls into one of our vendor’s code bases to perform operations, such as Login, Logout, etc. A requirement of this operation is that we have a background thread to receive events as a result of that action. For example, the Login action is sent on the main thread. Then, several events are received back from the vendor service as a result of the login. There can be 1, 2, or several events received. The background thread, which runs on a timer, receives these events and fires an event in the wcf service to notify that a new event has arrived.

I have implemented the WCF service in Duplex mode, and planned to use callbacks to notify the UI that events have arrived. Here is my question: How do I send new events from the background thread to the thread which is executing the service?

Right now, when I call OperationContext.Current.GetCallbackChannel<IMyCallback>(), the OperationContext is null. Is there a standard pattern to get around this?

I am using PerSession as my SessionMode on the ServiceContract.

UPDATE:
I thought I’d make my exact scenario clearer by demonstrating how I’m receiving events from the vendor code. My library receives each event, determines what the event is, and fires off an event for that particular occurrence.

I have another project which is a class library specifically for connecting to the vendor service. I’ll post the entire implementation of the service to give a clearer picture:

    [ServiceBehavior(
        InstanceContextMode = InstanceContextMode.PerSession
        )]
    public class VendorServer:IVendorServer
    {
private IVendorService _vendorService;  // This is the reference to my class library

        public VendorServer()
        {
_vendorServer = new VendorServer();
_vendorServer.AgentManager.AgentLoggedIn += AgentManager_AgentLoggedIn; // This is the eventhandler for the event which arrives from a background thread

}

        public void Login(string userName, string password, string stationId)
        {
            _vendorService.Login(userName, password, stationId); // This is a direct call from the main thread to the vendor service to log in
        }

    private void AgentManager_AgentLoggedIn(object sender, EventArgs e)
    {

        var agentEvent = new AgentEvent
                             {
                                 AgentEventType = AgentEventType.Login,
                                 EventArgs = e
                             };
    }
}

The AgentEvent object contains the callback as one of its properties, and I was thinking I’d perform the callback like this:

agentEvent.Callback = OperationContext.Current.GetCallbackChannel<ICallback>();

The AgentEvent is an object defined in the service:

[DataContract]
public class AgentEvent
{
    [DataMember]
    public EventArgs EventArgs { get; set; }
    [DataMember]
    public AgentEventType AgentEventType { get; set; }
    [DataMember]
    public DateTime TimeStamp{ get; set; }
    [DataMember]
    public IVendorCallback Callback { get; set; }
}

IVendorCallback looks like this:

    public interface IVendorCallback
    {
        [OperationContract(IsOneWay = true)]
        void SendEvent(AgentEvent agentEvent);
    }

The callback is implemented on the client and uses the EventArgs porperty of the AgentEvent to populate data on the UI.
How would I pass the OperationContext.Current instance from the main thread into the background thread?

  • 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-14T06:10:15+00:00Added an answer on May 14, 2026 at 6:10 am

    OperationContext.Current is only available on the thread that is actually executing the operation. If you want it to be available to a worker thread, then you need to actually pass a reference to the callback channel to that thread.

    So your operation might look something vaguely like:

    public class MyService : IMyService
    {
        public void Login()
        {
            var callback = 
                OperationContext.Current.GetCallbackChannel<ILoginCallback>();
            ThreadPool.QueueUserWorkItem(s =>
            {
                var status = VendorLibrary.PerformLogin();
                callback.ReportLoginStatus(status);
            });
        }
    }
    

    This is a straightforward way of doing it using the ThreadPool and anonymous method variable capturing. If you want to do it with a free-running thread, you’d have to use a ParameterizedThreadStart instead and pass the callback as the parameter.


    Update for specific example:

    Seems that what’s going on here is that the IVendorService uses some event-driven model for callbacks.

    Since you’re using InstanceContextMode.PerSession, you can actually just store the callback in a private field of the service class itself, then reference that field in your event handler.

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
    public class VendorServer : IVendorServer
    {
        private IMyCallback callback;
        private IVendorService vendorService;
    
        public VendorServer()
        {
            callback = OperationContext.Current.GetCallbackChannel<IMyCallback>();
            vendorService = new VendorService();
            vendorService.AgentManager.AgentLoggedIn += AgentManager_AgentLoggedIn;
        }
    
        public void Login(string userName, string password, string stationId)
        {
            vendorService.Login(userName, password, stationId);
        }
    
        private void AgentManager_AgentLoggedIn(object sender, EventArgs e)
        {
            callback.ReportLoggedIn(...);
        }
    }
    

    If you decide to switch to a different instance mode later on then this won’t work, because each client will have a different callback. As long as you keep it in session mode, this should be fine.

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

Sidebar

Related Questions

Here's the situation I have a webpage which has one drop down called prefer.
I have a tricky situation here. I have a web service written in C#
Here is my situation: I have one table that contains a list of drugs
Here is the situation: You have one long-running calculation running in a background thread.
Here is my situation. I have written a Database application in Java. Now (unfortunately
Alright here's the situation, I have an application written in the Zend_Framework, that is
here i have written a window service, it job is to read files from
Basically, here's the situation. I have the following: public IService Service { get; set;
I have a Windows service written in Delphi. One of the third-party resources it
Here is the situation : we have to offer a customer with a web-based

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.