SynchronizationContext.Current is null when run on different app domains

I have a Web Application running in one machine and the services in another machine (i.e.both are in different App domains). I have a workflow service in my service layer which gets the Synschronization Context from SynchronizationContext.Current. I get the SynchronizationContext.Current always as null. But If I run both my application and service layer in the same machine (i.e. same appdomain) the SynchronizationContext.Current is AspNetSynchronizationContext and it works fine. Can somebody help me to resolve this to run different app domains.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I solved it by overiding the synchronization context

if (syncContext == null)
{
   SynchronousSynchronizationContext sync = new SynchronousSynchronizationContext();
   syncContext = sync;
}

class SynchronousSynchronizationContext : SynchronizationContext
{
   public override void Post(SendOrPostCallback d, object state)
   {
      this.Send(d, state);
   }
}

Method 2

The synchronization context is usually created by some kind of framework, such as ASP.NET, WPF, WinForms, etc. It sounds like when you’re running your services in their own process you’re not using any kind of framework which would do this.

It’s also worth mentioning that SynchronizationContext.Current usually only returns the synchronization context of the current thread, so if you are calling it from the wrong thread it will almost certainly return null.

If you don’t have a synchronization context available, you can always create your own. This article explains how:

Synchronization Contexts in WCF


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x