Click here to Skip to main content
15,893,564 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I'm trying to create a sandboxed appdomain. It isn't to difficult so far. I just came across one problem I couldn't seem to fix:

This is a shortened version of my classes, but you should get the idea:
C#
public class LocalLoader : CrossAppDomainObject
{
    public LocalLoader()
    {
        RemoteLoader remoteloader;
        
        AppDomainSetup setup = new AppDomainSetup();
        setup.ApplicationName = "MyAppDomain";
        setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
        //..some path settings, not relevant
        
        PermissionSet permissions = new PermissionSet(PermissionState.None);
        permissions.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution));
        
        appdomain = AppDomain.CreateDomain("MyAppDomain", AppDomain.CurrentDomain.Evidence, setup, permissions, null);
        
        //Create an instance of RemoteLoader in a new appdomain!
        remoteloader = (RemoteLoader)appdomain.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, typeof(RemoteLoader).FullName);
    }
}

public class RemoteLoader: CrossAppDomainObject
{
}


The CrossAppDomainObject is simply put a class which inherits MarshalByRefObject and overrides the InitializeLifetimeService() method. Like this (also shortened):
C#
public abstract class CrossAppDomainObject : MarshalByRefObject
{
    public sealed override object InitializeLifetimeService()
    {
        return null;
    }
}

It will cause the lifetime of the connection to be infinite, a must have in my project.


When I do the appdomain.CreateInstanceAndUnwrap I receive this error:
Inheritance security rules violated while overriding member: 'MyApplication.CrossAppDomainObject.InitializeLifetimeService()'. Security accessibility of the overriding method must match the security accessibility of the method being overriden.


I've looked around for settings I should allow in my PermissionSet but I couldn't find anything that worked. Or should I add additional things to my CrossAppDomainObject?

Anyone an idea how I should fix this?
Answers are much appreciated.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900