且构网

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

AppDomain 等待异步任务阻止 SerializationException

更新时间:2023-01-23 18:25:07

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

我需要在

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

它从插件中捕获异常,所以一切正常.Wait() 调用应该只阻塞 Task.Run 而不是其他任务.

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.