1

I have a partially trusted AppDomain Sandbox, I execute method X, which instantiates MyException:

[Serializable]
class MyException: Exception
{
    public MyException():base("My exception") { }

    protected MyException(SerializationInfo serializationInfo, StreamingContext streamingContext)
        : base(serializationInfo, streamingContext) { }

    [SecurityCritical]
    public override void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        base.GetObjectData(info, context);
        Console.WriteLine("A line");
    }
}

Assembly A, which contains MyException is NOT loaded with full trust into the Sandbox. When X is executed in Sandbox, TypeLoadException occurs:

Inheritance security rules violated while overriding member: MyException.GetObjectData .

I thought, that having [SecurityCritical] on GetObjectData is what it requires, but it appears to have no effect.

If I add [assembly: AllowPartiallyTrustedCallers] to A, and load A with full trust into Sandbox, this code works fine. However, A contains many classes, and I only face this problem with this particular GetObjectData.

Without reverting to older security models, is there a way to make MyException work without AllowPartiallyTrustedCallers and without loading it with full trust?

Taher Rahgooy
  • 6,528
  • 3
  • 19
  • 30
LOST
  • 2,956
  • 3
  • 25
  • 40
  • Possible duplicate of [How do I implement Exception.GetObjectData in .NET 4 in a library assembly that has the AllowPartiallyTrustedCallersAttribute?](https://stackoverflow.com/questions/14124874/how-do-i-implement-exception-getobjectdata-in-net-4-in-a-library-assembly-that) – Carl Reinke Oct 27 '17 at 15:05

0 Answers0