Beispiel #1
0
        private void InitializeDomain()
        {
            var domaininfo = new AppDomainSetup {
                PrivateBinPath = Path.Combine(_baseFolderPath, _user, _path + "\\")
            };

            //Create evidence for the new appdomain from evidence of the current application domain
            var evidence = AppDomain.CurrentDomain.Evidence;

            _appDomain = AppDomain.CreateDomain(Path.Combine(_user, _path), evidence, domaininfo);
            _appDomain.Load("Scripting");
            _boundaryObject = (BoundaryObject)_appDomain.CreateInstanceAndUnwrap(
                typeof(BoundaryObject).Assembly.FullName,
                typeof(BoundaryObject).FullName ?? throw new InvalidOperationException());

            _args = new AppDomainArgs
            {
                Folder = domaininfo.PrivateBinPath,
                Name   = _path.Split(new[] { '\\' }, StringSplitOptions.RemoveEmptyEntries).Last(),
                User   = _user
            };

            var test = _boundaryObject.CreateScriptingInstance(_args);

            if (test == null)
            {
                throw new NoNullAllowedException("Failed to create scripting instance");
            }
            test = null;

            UserName      = _user;
            ScriptingName = _path;
        }
Beispiel #2
0
        public IScripting CreateScriptingInstance(AppDomainArgs args)
        {
            try
            {
                if (_assembly == null || _name != args.Name || _user != args.User || _folder != args.Folder)
                {
#if DEBUG
                    var dll     = File.ReadAllBytes(Path.Combine(args.Folder, args.Name + ".dll"));
                    var pdbFile = Path.Combine(args.Folder, args.Name + ".pdb");
                    _assembly = File.Exists(pdbFile) ? Assembly.Load(dll, File.ReadAllBytes(pdbFile)) : Assembly.Load(dll);
#else
                    _assembly = Assembly.Load(File.ReadAllBytes(Path.Combine(args.Folder, args.Name + ".dll")));
#endif
                    _name   = args.Name;
                    _user   = args.User;
                    _folder = args.Folder;
                }

                var instance = _assembly.CreateInstance(args.Name + "." + args.Name) as IScripting;

                //failed to create instance by name - try to retrieve the type by base type
                if (instance == null)
                {
                    var t = _assembly.GetTypes().FirstOrDefault(i => i.BaseType == typeof(SignalBase));  //|| i.BaseType == typeof(IndicatorBase));
                    if (t != null)
                    {
                        instance = _assembly.CreateInstance(t.FullName ?? throw new InvalidOperationException()) as IScripting;
                    }
                }

                if (instance != null)
                {
                    instance.Owner = args.User;
                }
                return(instance);
            }
            catch (Exception e)
            {
                Logger.Error("Failed to create scripting instance", e);
                return(null);
            }
        }