AppDomain.DoCallBack returns System.Runtime.Serialization.SerializationException

I keep getting    An exception of type 'System.Runtime.Serialization.SerializationException' occurred in IRCBot.exe but was not handled in user code

from AppDomain.DoCallBack

My code

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

      

Sorry if this question was short, but I don't know what else to say. I have traced the source of the exception in DoCallBack. Thank you in advance.

+3


source to share


1 answer


The delegate passed to DoCallback must either represent a static method or a method for the class being serialized. A class autogenerated by the C # compiler to express a lambda is not serializable. You need to implement the method that needs to be called for the serializable class yourself, eg.



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

      

+3


source







All Articles