我有一个命令对象,它根据请求队列中的请求进行工作。此特定命令将在子appdomain中执行其工作。在子appdomain中完成其工作的一部分涉及阻塞ConcurrentQueue操作(例如,Add或Take)。我需要能够通过请求队列传播中止信号,并将其传播到子应用程序域,并唤醒其中的工作线程。

因此,我认为我需要跨AppDomain边界传递CancellationToken。

我尝试创建一个继承自MarshalByRefObject的类:

protected class InterAppDomainAbort : MarshalByRefObject, IAbortControl
    {
        public InterAppDomainAbort(CancellationToken t)
        {
            Token = t;
        }

        [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.Infrastructure)]
        public override object InitializeLifetimeService()
        {
            return null;
        }

        public CancellationToken Token
        {
            get;
            private set;
        }

    };

并将其作为参数传递给worker函数:
// cts is an instance variable which can be triggered by another thread in parent appdomain
cts = new CancellationTokenSource();
InterAppDomainAbort abortFlag = new InterAppDomainAbort(cts.Token);
objectInRemoteAppDomain = childDomain.CreateInstanceAndUnwrap(...);

// this call will block for a long while the work is being performed.
objectInRemoteAppDomain.DoWork(abortFlag);

但是当objectInRemoteAppDomain尝试访问Token getter属性时,我仍然遇到异常:
System.Runtime.Serialization.SerializationException: Type 'System.Threading.CancellationToken' in Assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.

我的问题是:如何在应用程序域之间传播中止/取消信号并唤醒可能在.NET并发数据结构(支持CancellationToken参数的位置)中阻塞的线程。

最佳答案

自从我看了任何跨AppDomain的东西以来已经有一段时间了,所以这段代码可能存在我尚未意识到的问题,但似乎可以完成工作。根本的问题是,似乎没有办法将CancellationToken [Source]从一个AppDomain转移到另一个。因此,我创建了两个来源,并在适当时取消了主要来源的设置。

在这种情况下,有两个单独的 token 源这一事实当然可能是个问题,但是我认为您并没有绕过以下事实:缺乏可串行性会阻止您在两个单独的AppDomain中使用相同的 token 源。

有关最低限度的错误检查,Dispose实现等的标准警告。

// I split this into a separate interface simply to make the boundary between
// canceller and cancellee explicit, similar to CancellationTokenSource itself.
public interface ITokenSource
{
    CancellationToken Token { get; }
}

public class InterAppDomainCancellable: MarshalByRefObject,
                                        ITokenSource,
                                        IDisposable
{
    public InterAppDomainCancellable()
    {
        cts = new CancellationTokenSource();
    }

    public void Cancel() { cts.Cancel(); }

    // Explicitly implemented to make it less tempting to call Token
    // from the wrong side of the boundary.
    CancellationToken ITokenSource.Token { get { return cts.Token; } }

    public void Dispose() { cts.Dispose(); }

    private readonly CancellationTokenSource cts;
}

// ...

// Crucial difference here is that the remotable cancellation source
// also lives in the other domain.
interAppDomainCancellable = childDomain.CreateInstanceAndUnwrap(...);

var primaryCts = new CancellationTokenSource();
// Cancel the secondary when the primary is cancelled.
// CancellationToken.Register returns a disposable object which unregisters when disposed.
using (primaryCts.Token.Register(() => interAppDomainCancellable.Cancel()))
{
    objectInRemoteAppDomain = childDomain.CreateInstanceAndUnwrap(...);
    // DoWork expects an instance of ITokenSource.
    // It can access Token because they're all in the same domain together.
    objectInRemoteAppDomain.DoWork(interAppDomainCancellable);
    // ... some other work which might cancel the primary token.
}

关于c# - 如何跨AppDomain边界传递CancellationToken?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15149211/

10-17 00:07