SynchronizationContext.Current is null when run on different app domains

后端 未结 2 1356
小蘑菇
小蘑菇 2021-01-27 20:07

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

相关标签:
2条回答
  • 2021-01-27 20:39

    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

    0 讨论(0)
  • 2021-01-27 20:56

    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);
       }
    }
    
    0 讨论(0)
提交回复
热议问题