SynchronizationContext.Current 在不同的应用程序域上运行时为 null [英] SynchronizationContext.Current is null when run on different app domains

查看:26
本文介绍了SynchronizationContext.Current 在不同的应用程序域上运行时为 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在一台机器上运行的 Web 应用程序和另一台机器上的服务(即两者都在不同的应用程序域中).我的服务层中有一个工作流服务,它从 SynchronizationContext.Current 获取同步上下文.我得到 SynchronizationContext.Current 始终为空.但是如果我在同一台机器上运行我的应用程序和服务层(即同一个应用程序域),则 SynchronizationContext.Current 是 AspNetSynchronizationContext 并且它工作正常.有人可以帮我解决这个问题以运行不同的应用域.

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.

推荐答案

我通过覆盖同步上下文解决了这个问题

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);
   }
}

这篇关于SynchronizationContext.Current 在不同的应用程序域上运行时为 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆