且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

AppDomain等待异步任务阻止SerializationException

更新时间:2023-01-23 18:20:49

感谢Hamlet Hakobyan和Stephen Toub的帖子,我认为我能够解决问题.

Thanks to Hamlet Hakobyan and the post from Stephen Toub, I think I was able to solve the problem.

我替换了主叫方的电话

 try { tExe = pProxy.Execute(panel, user.Username); }

 tExe = DoWorkInOtherDomain(pProxy, panel, user.Username);

和方法DoWorkInOtherDomain:

and the method DoWorkInOtherDomain:

private Task DoWorkInOtherDomain(PluginProxy pProxy, PluginPanel panel, string user)
    {
        var ch = new MarshaledResultSetter<string>();
        pProxy.Execute(panel, user, ch);
        return ch.Task;
    }

最后是代理类:

 Task.Run(() =>
        {
            try
            {
                m_Plugin.Execute(panel, user).Wait();
            }
            catch (AggregateException e)
            {
                if (e.InnerExceptions != null)
                    foreach (Exception ein in e.InnerExceptions)
                        AddToErrorLog(panel.PanelName, ein);
            }
            catch (Exception e) { AddToErrorLog(panel.PanelName, e); }
            finally { ch.SetResult(AppDomain.CurrentDomain.FriendlyName); }
        });

我需要在其中调用Wait()

I need to call Wait() in

m_Plugin.Execute(panel, user).Wait();

它从插件捕获异常,所以一切都很好. Wait()调用应仅阻止Task.Run,​​而不阻止其他Tasks.

it catches the Exceptions from the plugin so everything is doing fine. The Wait() call should only blocking the Task.Run and not the other Tasks.

任何人都可以告诉我这是否是一个好的解决方案,还是应该更改某些内容?我不需要结果,所以我只需要:

Can anyone tell me if this is a good solution or should I change something? I dont need a result so I just do:

 ch.SetResult(AppDomain.CurrentDomain.FriendlyName);

因为我不知道该怎么做才能没有结果.

because I dont know how I should do it without a result.