问题
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.
回答1:
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
回答2:
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);
}
}
来源:https://stackoverflow.com/questions/20521286/synchronizationcontext-current-is-null-when-run-on-different-app-domains