应用程序域中.DoCallBack返回System.Runtime.Serialization.Serializatio

本文关键字:Runtime Serialization Serializatio System 返回 DoCallBack 应用程序域 | 更新日期: 2023-09-27 18:05:54

我一直在An exception of type 'System.Runtime.Serialization.SerializationException' occurred in IRCBot.exe but was not handled in user code从应用程序域中。DoCallBack

我的代码是
            object result = null;
        try {
            Console.WriteLine("Attempting to run in sandbox");
            sandbox.DoCallBack(() =>
            {
                Console.WriteLine("Creating thread");
                try
                {
                    var scriptThread = new Thread(() =>
                    {
                        try
                        {
                            Console.WriteLine("Trying");
                            result = CSharpScript.RunAsync(code).ReturnValue.Result;
                        }
                        catch (Exception ex)
                        {
                            result = ex.Message;
                        }
                    });
                    scriptThread.Start();
                    if (!scriptThread.Join(6000))
                    {
                        scriptThread.Abort();
                        AppDomain.Unload(sandbox);
                    }
                }
                catch (Exception ex)
                {
                    result = ex.ToString();
                }
            });
        }
        catch (Exception e)
        {
            result = e.ToString();
        }
对不起,如果这个问题听起来很简短,但我真的不知道还能说什么。我已经追踪到DoCallBack异常的来源。

应用程序域中.DoCallBack返回System.Runtime.Serialization.Serializatio

传递给DoCallback的委托必须要么代表静态方法,要么代表可序列化类上的方法。c#编译器为lambda表达式自动生成的类是不可序列化的。你需要自己实现应该在可序列化类上调用的方法,例如

[Serializable]
class CallbackContext
{
    public string Code { get; set; }
    public void Entry()
    {
        Console.WriteLine("Creating thread");
        try
        {
            var scriptThread = new Thread(() =>
            {
                try
                {
                    Console.WriteLine("Trying");
                    AppDomain.CurrentDomain.SetData("result", CSharpScript.RunAsync(Code).ReturnValue.Result);
                }
                catch (Exception ex)
                {
                    AppDomain.CurrentDomain.SetData("result", ex.Message);
                }
            });
            scriptThread.Start();
            if (!scriptThread.Join(6000))
            {
                scriptThread.Abort();
                AppDomain.Unload(AppDomain.CurrentDomain);
            }
        }
        catch (Exception ex)
        {
            AppDomain.CurrentDomain.SetData("result", ex.ToString());
        }
    }
}
    ...
    object result = null;
    try {
        Console.WriteLine("Attempting to run in sandbox");
        CallbackContext ctx = new CallbackContext();
        ctx.Code = code;
        sandbox.DoCallBack(ctx.Entry);
        result = sandbox.GetData("result");
    }
    catch (Exception e)
    {
        result = e.ToString();
    }