Executing R.net in a restricted AppDomain

Mar 10, 2014 at 1:32 PM
Executing R.net in the same appdomain works great. Now, I want to have it run in a separate restricted appdomain. This throws the following exception:

System.TypeLoadException was unhandled
HResult=-2146233054
Message=Inheritance security rules violated by type: 'RDotNet.NativeLibrary.UnmanagedDll'. Derived types must either match the security accessibility of the base type or be less accessible.
Source=RBugReplication
TypeName=RDotNet.NativeLibrary.UnmanagedDll
StackTrace:
   at RBugReplication.Program.CreateREngine()
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at RBugReplication.Program.Main(String[] args) in c:\Users\For\Documents\Visual Studio 2013\Projects\RBugReplication\RBugReplication\Program.cs:line 35
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()
InnerException:

Here is the code the replicates the issue. All projects are built against .net 4.0. The project references RDotNet and RDotNet.Native library located in its 'lib' folder.
    class Program
    {
        static void Main(string[] args)
        {
            SetupPath();

            var ps = new PermissionSet(PermissionState.None);
            ps.AddPermission(new SecurityPermission(PermissionState.Unrestricted));
            ps.AddPermission(new FileIOPermission(PermissionState.Unrestricted));
            ps.AddPermission(new ReflectionPermission(PermissionState.Unrestricted));
            ps.AddPermission(new EnvironmentPermission(PermissionState.Unrestricted));

            var appDomain = AppDomain.CreateDomain(
                "sandbox",
                new Evidence(),
                AppDomain.CurrentDomain.SetupInformation,
                ps);

            appDomain.DoCallBack(LoadAssemblies);

            appDomain.DoCallBack(CreateREngine);
        }

        public static void SetupPath()
        {
            var oldPath = Environment.GetEnvironmentVariable("PATH");
            var rPath = @"C:\Program Files\R\R-3.0.2\bin\x64";
            var newPath = string.Format("{0}{1}{2}", rPath, Path.PathSeparator, oldPath);
            Environment.SetEnvironmentVariable("PATH", newPath);
        }

        private static void LoadAssemblies()
        {
            LoadDll(@"..\..\lib\RDotNet.NativeLibrary.dll");
            LoadDll(@"..\..\lib\RDotNet.dll");
        }

        private static void LoadDll(string libFileName)
        {
            var absoluteLibPath = Path.GetFullPath(libFileName);

            if (File.Exists(Environment.CurrentDirectory + "\\" + Path.GetFileName(libFileName)) == false)
                File.Copy(absoluteLibPath, Environment.CurrentDirectory + "\\" + Path.GetFileName(libFileName));

            Assembly.LoadFile(absoluteLibPath);
        }

        private static void CreateREngine()
        {
            var e = REngine.CreateInstance("test");
        }

    }
Has anybody seen this issue in the past? Any help would be appreciated.
Thanks,
Vitaly
Developer
Mar 10, 2014 at 10:06 PM
Hi,

I am looking at running in different app domains in the coming days/weeks. There is a feature request to Support usage of R.NET from multiple App Domains. Thank you for the code to reproduce the issue, this is useful to build use and test cases to work towards.

Cheers
Mar 12, 2014 at 9:25 AM
great, let me know if you need anything from me