Beispiel #1
0
        private void LoadAddinInAppDomain(string assemblyName, string typeName, AddinManager addinManager)
        {
            var appSetup = new AppDomainSetup()
            {
                ApplicationName   = "Tangra 3",
                ApplicationBase   = AppDomain.CurrentDomain.BaseDirectory,
                ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile,
                PrivateBinPath    = @"Addins",
            };


            m_DomainName = string.Format("Tangra.Addins.{0}.v{1}", m_AssemblyName.Name, m_AssemblyName.Version.ToString());

            var e = new Evidence();

            e.AddHostEvidence(new Zone(SecurityZone.MyComputer));
            PermissionSet pset = SecurityManager.GetStandardSandbox(e);

            m_HostDomain = AppDomain.CreateDomain(m_DomainName, AppDomain.CurrentDomain.Evidence, appSetup, pset, null);
            m_HostDomain.AssemblyResolve += m_HostDomain_AssemblyResolve;
            m_HostDomain.ReflectionOnlyAssemblyResolve += m_HostDomain_AssemblyResolve;
            m_HostDomain.UnhandledException            += m_HostDomain_UnhandledException;

            object obj = m_HostDomain.CreateInstanceAndUnwrap(assemblyName, typeName);

            ILease lease = (ILease)(obj as MarshalByRefObject).GetLifetimeService();

            if (lease != null)
            {
                lease.Register(addinManager.RemotingClientSponsor);
            }

            m_Instance = (ITangraAddin)obj;
            m_Instance.Initialise(new TangraHostDelegate(typeName, addinManager));

            foreach (object actionInstance in m_Instance.GetAddinActions())
            {
                var mbrObj = actionInstance as MarshalByRefObject;
                if (mbrObj != null)
                {
                    lease = (ILease)mbrObj.GetLifetimeService();
                    if (lease != null)
                    {
                        lease.Register(addinManager.RemotingClientSponsor);
                    }
                }
            }
        }
Beispiel #2
0
        /// <returns></returns>
        private RemoteController CreateRemoteController(string strMachineName, int iPortNumber)
        {
            RemoteController remoteController = null;

            try
            {
                string       strRemoteObjUrl = "tcp://" + strMachineName + ":" + iPortNumber.ToString() + "/GalaxyRemoteController";
                UrlAttribute urlAttr         = new UrlAttribute(strRemoteObjUrl);
                object[]     rgAct           = { urlAttr };
                remoteController = Activator.CreateInstance(Type.GetType("Galaxy.Tools.RemoteController, RemoteImpLib"), null, rgAct) as RemoteController;
                remoteController.AddCmdOutputEventSinker(m_cmdOutputEventSinker.OnCmdOutput);
                remoteController.AddCmdFinishEventSinker(m_cmdOutputEventSinker.OnCmdFinish);
                remoteController.IsLive();
            }
            catch (Exception e)
            {
                // The remote server is offline
                remoteController = null;
                Console.WriteLine(e.Message);
            }

            // Register the lease sponsor
            if (remoteController != null)
            {
                remoteController.Port        = iPortNumber;
                remoteController.MachineName = strMachineName;
                ILease lease = RemotingServices.GetLifetimeService(remoteController) as ILease;
                lease.Register(m_sponsor);
            }

            return(remoteController);
        }
Beispiel #3
0
        //***********************************************

        ///////////////////////////////////////////////////////////////////////////////////
        public void Register(object obj)
        {
            if (obj == null)
            {
                return;
            }
            if (!(obj is MarshalByRefObject))
            {
                return;
            }
            MarshalByRefObject marshalObj = (MarshalByRefObject)obj;
            //ObjRef laRef = marshalObj.CreateObjRef ( obj.GetType() );
            ILease lease = (ILease)RemotingServices.GetLifetimeService(marshalObj);

            if (lease == null)
            {
                return;
            }
            //lease.Register(this, TimeSpan.FromSeconds(c_nbSecondes));
            lease.Register(this, TimeSpan.FromMinutes(2));

            m_nNbObjetsSponsorises++;
            I2iMarshalObject obj2i = obj as I2iMarshalObject;

            if (obj2i != null)
            {
                m_dicObjetsSponsor[obj2i.UniqueId] = new CProxy2iMarshal(obj2i);
            }
            else
            {
                m_listeObjetsSponsorises.Add(obj);
            }
        }
Beispiel #4
0
    private ICustomPlugin[] _plugins;            // I do not store real instances of Plugins, but a "CustomPluginProxy" which is known both by main AppDomain and Plugin AppDomain.

    // Constructor : load an assembly file in another AppDomain (sandbox)
    public PluginFile(System.IO.FileInfo f, AppDomainSetup appDomainSetup, Evidence evidence)
    {
        Directory = System.IO.Path.GetDirectoryName(f.FullName) + @"\";
        _sandbox  = AppDomain.CreateDomain("sandbox_" + Guid.NewGuid(), evidence, appDomainSetup);

        _sandbox.Load(typeof(Loader).Assembly.FullName);

        // - Instanciate class "Loader" INSIDE OTHER APPDOMAIN, so we couldn't use new() which would create in main AppDomain.
        _loader = (Loader)Activator.CreateInstance(
            _sandbox,
            typeof(Loader).Assembly.FullName,
            typeof(Loader).FullName,
            false,
            BindingFlags.Public | BindingFlags.Instance,
            null,
            null,
            null,
            null).Unwrap();

        // - Load plugins list for assembly
        _plugins = _loader.LoadPlugins(f.FullName);


        // - Keep object created in other AppDomain not to be "Garbage Collected". I create a sponsor. The sponsor in registed for object "Lease". The LeaseManager will check lease expiration, and call sponsor. Sponsor can decide to renew lease. I not renewed, the object is garbage collected.
        // - Here is an explanation. Source: https://stackoverflow.com/questions/12306497/how-do-the-isponsor-and-ilease-interfaces-work
        _sponsor = new RemotingSponsor(_loader);
        // Here is my SOLUTION after many hours ! I had to sponsor each MarshalByRefObject (plugins) and not only the main one that contains others !!!
        foreach (ICustomPlugin plugin in Plugins)
        {
            ILease lease = (ILease)RemotingServices.GetLifetimeService((PluginProxy)plugin);
            lease.Register(_sponsor);     // Use the same sponsor. Each Object lease could have as many sponsors as needed, and each sponsor could be registered in many Leases.
        }
    }
        public void Sponsors()
        {
            lease = (ILease)obj.GetLifetimeService();            //информация об аренде
            MyClientSponsor sponsor = new MyClientSponsor(this); //создание спонсора

            lease.Register(sponsor);                             //регистрация спонсора в службе аренды
        }
Beispiel #6
0
        /// <summary>
        /// 取一个远程对象接口
        /// </summary>
        /// <param name="_interfaceName"></param>
        /// <returns></returns>
        public static object GetRemotingInterface(string _interfaceName)
        {
            if (serverObjectLib.ContainsKey(_interfaceName))
            {
                object _icsObject = serverObjectLib[_interfaceName];
                try
                {
                    MarshalByRefObject _mo   = _icsObject as MarshalByRefObject;
                    ILease             lease = (ILease)_mo.GetLifetimeService();
                    if (lease.CurrentState == LeaseState.Active)
                    {
                        return(_icsObject);
                    }
                }
                catch (Exception e)
                {
                    string errmsg = e.Message;
                }
                sponsorLib.Remove(_interfaceName);
                serverObjectLib.Remove(_interfaceName);
            }

            IServiceFactory _serviceFactory = (IServiceFactory)RemotingClientSvc.GetAppSvrObj(typeof(IServiceFactory));

            RemotingClientSvc.BindTicketToCallContext(SessionClass.CurrentTicket);
            MarshalByRefObject _ret = _serviceFactory.GetInterFace(_interfaceName) as MarshalByRefObject;

            ILease clease           = (ILease)_ret.GetLifetimeService();
            SinoSZClientSponsor _sp = new SinoSZClientSponsor();

            clease.Register(_sp);            //为自己注册生存期租约主办方
            sponsorLib.Add(_interfaceName, _sp);
            serverObjectLib.Add(_interfaceName, _ret);
            return(_ret);
        }
        public override object InitializeLifetimeService()
        {
            ILease ret = (ILease)base.InitializeLifetimeService();

            ret.SponsorshipTimeout = TimeSpan.FromMinutes(2);
            ret.Register(this);
            return(ret);
        }
Beispiel #8
0
        public UnmanagedObjectProxy(Type oftype, string chURI, string objURI) : base(oftype)
        {
            m_Proxy        = (UnmanagedProxy)Activator.GetObject(typeof(UnmanagedProxy), chURI + "/" + objURI + "_LOCALPROXY");
            m_ProxySponsor = new UnmanagedProxySponsor();
            ILease lease = (ILease)RemotingServices.GetLifetimeService(m_Proxy);

            lease.Register(m_ProxySponsor);
        }
 public LifetimeSponsor(MarshalByRefObject lease)
 {
     _lease = (ILease)System.Runtime.Remoting.RemotingServices.GetLifetimeService(lease);
     if (_lease != null)
     {
         _lease.Register(this);
     }
 }
Beispiel #10
0
    public ProcessHost(IProcessHostController controller)
    {
        this.controller = controller;
        MarshalByRefObject mbr   = (MarshalByRefObject)controller;
        ILease             lease = mbr.GetLifetimeService() as ILease;

        lease.Register(this);
    }
        public override object InitializeLifetimeService()
        {
            Console.WriteLine("Remoting.ClientActivated.InitializeLifetimeService()");
            ILease leaseInfo = (ILease)base.InitializeLifetimeService();

            leaseInfo.Register(new DisposingSponsor(this));

            return(leaseInfo);
        }
Beispiel #12
0
 public RemotingSponsor(MarshalByRefObject mbro)
 {
     _lease = ( ILease )RemotingServices.GetLifetimeService(mbro);
     if (_lease == null)
     {
         throw new NotSupportedException("Lease instance for MarshalByRefObject is NULL");
     }
     _lease.Register(this);
 }
Beispiel #13
0
 /// <summary>
 /// Initializes a new instance of the <see cref="RemoteClient"/> class.
 /// </summary>
 public RemoteClient()
 {
     clientchannel = new IpcChannel();
     ChannelServices.RegisterChannel(clientchannel, false);
     RemotingConfiguration.RegisterWellKnownClientType(typeof(RemoteObject), "ipc://localhost:15000/RemoteObject.rem");
     RemoteObj = new RemoteObject();
     sponser = new RemoteClientSponser("RemoteClient");
     lease = (ILease)RemoteObj.GetLifetimeService();
     lease.Register(sponser);
 }
        public Sponsor(T obj)
        {
            mObj = obj;

            // Get the lifetime service lease from the MarshalByRefObject,
            // and register ourselves as a sponsor.
            ILease lease = (ILease)obj.GetLifetimeService();

            lease.Register(this);
        }
Beispiel #15
0
        /// <summary>
        /// Obtains a lifetime service object to control the lifetime policy for this instance.
        /// </summary>
        /// <returns>An object of type ILease used to control the lifetime
        /// policy for this instance.</returns>
        public override object InitializeLifetimeService()
        {
            ILease lease = (ILease)base.InitializeLifetimeService();

            if (lease != null)
            {
                lease.Register(_sponsor);
            }
            return(lease);
        }
Beispiel #16
0
        override public object InitializeLifetimeService()
        {
            ILease iLease = (ILease)base.InitializeLifetimeService();

            iLease.InitialLeaseTime   = TimeSpan.FromSeconds(20);
            iLease.SponsorshipTimeout = TimeSpan.FromSeconds(20);
            iLease.RenewOnCallTime    = TimeSpan.FromSeconds(20);
            iLease.Register(this);
            return(iLease);
        }
Beispiel #17
0
        public Request(Host host, Connection connection)
            : base(string.Empty, string.Empty, null)
        {
            this._host       = host;
            this._connection = connection;
            ILease lease = (ILease)RemotingServices.GetLifetimeService(this._connection);

            this._connectionSponsor = new Sponsor();
            lease.Register(this._connectionSponsor);
        }
Beispiel #18
0
        /// <inheritdoc />
        /// <summary>
        /// InitializeLifetimeService is called when the remote object is activated.
        /// This method will determine how long the lifetime for the object will be.
        /// </summary>
        /// <returns>The lease object to control this object's lifetime.</returns>
        public override object InitializeLifetimeService()
        {
            lock (_callbackMonitor)
            {
                VerifyActiveProxy();

                // Each MarshalByRef object has a reference to the service which
                // controls how long the remote object will stay around
                ILease lease = (ILease)base.InitializeLifetimeService();

                // Set how long a lease should be initially. Once a lease expires
                // the remote object will be disconnected and it will be marked as being availiable
                // for garbage collection
                int initialLeaseTime = 1;

                string initialLeaseTimeFromEnvironment = Environment.GetEnvironmentVariable("MSBUILDENGINEPROXYINITIALLEASETIME");

                if (!String.IsNullOrEmpty(initialLeaseTimeFromEnvironment))
                {
                    int leaseTimeFromEnvironment;
                    if (int.TryParse(initialLeaseTimeFromEnvironment, out leaseTimeFromEnvironment) && leaseTimeFromEnvironment > 0)
                    {
                        initialLeaseTime = leaseTimeFromEnvironment;
                    }
                }

                lease.InitialLeaseTime = TimeSpan.FromMinutes(initialLeaseTime);

                // Make a new client sponsor. A client sponsor is a class
                // which will respond to a lease renewal request and will
                // increase the lease time allowing the object to stay in memory
                _sponsor = new ClientSponsor();

                // When a new lease is requested lets make it last 1 minutes longer.
                int leaseExtensionTime = 1;

                string leaseExtensionTimeFromEnvironment = Environment.GetEnvironmentVariable("MSBUILDENGINEPROXYLEASEEXTENSIONTIME");
                if (!String.IsNullOrEmpty(leaseExtensionTimeFromEnvironment))
                {
                    int leaseExtensionFromEnvironment;
                    if (int.TryParse(leaseExtensionTimeFromEnvironment, out leaseExtensionFromEnvironment) && leaseExtensionFromEnvironment > 0)
                    {
                        leaseExtensionTime = leaseExtensionFromEnvironment;
                    }
                }

                _sponsor.RenewalTime = TimeSpan.FromMinutes(leaseExtensionTime);

                // Register the sponsor which will increase lease timeouts when the lease expires
                lease.Register(_sponsor);

                return(lease);
            }
        }
Beispiel #19
0
        public ClientForm()
        {
            InitializeComponent();

            m_Sponsor = new MySponsor();
            m_Obj     = new MyClass();

            //Register the sponsor
            ILease lease = (ILease)RemotingServices.GetLifetimeService(m_Obj);

            lease.Register(m_Sponsor);
        }
Beispiel #20
0
        public Sponsor(T obj) {
            mObj = obj;

            // Get the lifetime service lease from the MarshalByRefObject,
            // and register ourselves as a sponsor.
            ILease lease = (ILease)obj.GetLifetimeService();
            lease.Register(this);

            Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss") + "|Sponsor created; initLt=" +
                lease.InitialLeaseTime + " renOC=" + lease.RenewOnCallTime +
                " spon=" + lease.SponsorshipTimeout);
        }
Beispiel #21
0
        /// <summary>
        /// Get a <see cref="RemoteObject"/> for remote invoke.
        /// </summary>
        /// <param name="assemblyFile">the assembly file for the job,which in AdDlls directory.</param>
        /// <returns>return a <see cref="RemoteObject"/>.</returns>
        public RemoteObject GetObject(string assemblyFile)
        {
            var obj = (RemoteObject)m_AppDomain.CreateInstanceFromAndUnwrap(Assembly.GetExecutingAssembly().Location, typeof(RemoteObject).FullName);

            obj.Init(assemblyFile, m_TypeFullName);

            ILease lease = (ILease)obj.GetLifetimeService();

            lease.Register(obj);

            return(obj);
        }
Beispiel #22
0
        public void Register(MarshalByRefObject obj)
        {
            ILease lease = (ILease)RemotingServices.GetLifetimeService(obj);

            Debug.Assert(lease.CurrentState == LeaseState.Active);

            lease.Register(this);
            lock (this)
            {
                leaseObjects.Add(lease);
            }
        }
        /// <summary>
        /// 通过Remoting 接口创建远程接口对象
        /// </summary>
        virtual protected void CreateObject()
        {
            IServiceFactory _serviceFactory = (IServiceFactory)RemotingClientSvc.GetAppSvrObj(typeof(IServiceFactory));

            RemotingClientSvc.BindTicketToCallContext(SessionClass.CurrentTicket);
            MarshalByRefObject _ret = _serviceFactory.GetInterFace(InterfaceName) as MarshalByRefObject;

            ILease clease = (ILease)_ret.GetLifetimeService();

            sponsor = new SinoSZClientSponsor();
            clease.Register(sponsor);            //为自己注册生存期租约主办方
            remoteObject = _ret;
        }
        public virtual void Start(StartOptions options)
        {
            var context = new StartContext(options);

            IServiceProvider services = ServicesFactory.Create(context.Options.Settings);

            var engine = services.GetService <IHostingEngine>();

            _runningApp = engine.Start(context);

            _lease = (ILease)RemotingServices.GetLifetimeService(this);
            _lease.Register(this);
        }
Beispiel #25
0
        /// <summary>
        /// Obtains a lifetime service object to control the lifetime policy for this instance.
        /// </summary>
        /// <returns>An object of type ILease used to control the lifetime
        /// policy for this instance.</returns>
        public override object InitializeLifetimeService()
        {
#if !NETCORE
            ILease lease = (ILease)base.InitializeLifetimeService();
            if (lease != null)
            {
                lease.Register(_sponsor);
            }
            return(lease);
#elif NETCORE
            throw new NotImplementedException();
#endif
        }
Beispiel #26
0
        public bool Register(MarshalByRefObject obj)
        {
            ILease lease = (ILease)obj.GetLifetimeService();

            if (lease == null)
            {
                return(false);
            }
            lease.Register((ISponsor)this);
            lock (this.sponsorTable)
                this.sponsorTable[(object)obj] = (object)lease;
            return(true);
        }
        public virtual void Start(StartOptions options)
        {
            var context = new StartContext(options);

            IServiceProvider services = ServicesFactory.Create(context.Options.Settings);

            var engine = services.GetService<IHostingEngine>();

            _runningApp = engine.Start(context);

            _lease = (ILease)RemotingServices.GetLifetimeService(this);
            _lease.Register(this);
        }
Beispiel #28
0
 public void Register(MarshalByRefObject obj)
 {
     if (RemotingServices.IsTransparentProxy(obj))
     {
         ILease lease = (ILease)RemotingServices.GetLifetimeService(obj);
         Debug.Assert(lease.CurrentState == LeaseState.Active);
         lease.Register(this);
         lock (this._lock) {
             this._leaseList.Add(lease);
             Logger.Debug(this, "Sponsoring lease #" + lease.GetHashCode() + " for proxy to " + obj.GetType().Name + ", id = #" + obj.GetHashCode() + ", url = " + RemotingServices.GetObjectUri(obj));
         }
     }
 }
        /// <summary>
        /// Add a new server to the list of available servers.
        /// </summary>
        /// <param name="HostName"></param>
        /// <param name="Port"></param>
        /// <param name="Protocol"></param>
        /// <returns></returns>
        public bool RegisterServer(String HostName, String Port, String Protocol, String Description)
        {
            //
            // Check to see if we already have this server registered
            if (m_Servers.ContainsKey(HostName + Port))
            {
                return(true);
            }

            //
            // Make sure the Protocol channel has been successfully registered
            if (Protocol.ToUpper() == GPEnums.CHANNEL_TCP && !m_TcpRegistered)
            {
                return(false);
            }
            if (Protocol.ToUpper() == GPEnums.CHANNEL_HTTP && !m_HttpRegistered)
            {
                return(false);
            }

            //
            // Obtain the IGPServer interface
            String CompleteHost = Protocol.ToLower() + "://" + HostName + ":" + Port + "/" + GPEnums.SERVER_SOAPNAME;
            object obj          = Activator.GetObject(
                typeof(IGPServer),
                CompleteHost);

            IGPServer iGPServer = (IGPServer)obj;

            //
            // Validate this server is available
            if (!ValidateServer(iGPServer))
            {
                return(false);
            }

            //
            // Assign a sponsor for the remote object
            GPServerClientSponsor Sponsor = fmMain.SponsorServer.Create("GPServer Singleton");
            ILease LeaseInfo = (ILease)((MarshalByRefObject)iGPServer).GetLifetimeService();

            LeaseInfo.Register(Sponsor);
            m_Sponsors.Add(HostName + Port, Sponsor);

            //
            // Add this server interface to our singleton
            m_Servers.Add(HostName + Port, iGPServer);
            m_Descriptions.Add(HostName + Port, Description);

            return(true);
        }
Beispiel #30
0
        /// <summary>
        /// Obtains a lifetime service object to control the lifetime policy for this instance.
        /// </summary>
        /// <returns>
        /// An object of type <see cref="T:System.Runtime.Remoting.Lifetime.ILease"></see> used to control the
        /// lifetime policy for this instance. This is the current lifetime service object for this instance if
        /// one exists; otherwise, a new lifetime service object initialized to the value of the
        /// <see cref="P:System.Runtime.Remoting.Lifetime.LifetimeServices.LeaseManagerPollTime"></see> property.
        /// </returns>
        /// <exception cref="T:System.Security.SecurityException">The immediate caller does not have infrastructure
        /// permission. </exception>
        /// <PermissionSet><IPermission class="System.Security.Permissions.SecurityPermission, mscorlib,
        /// Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1"
        /// Flags="RemotingConfiguration, Infrastructure"/></PermissionSet>
        public override object InitializeLifetimeService()
        {
            ILease lease = (ILease)base.InitializeLifetimeService();

            if (lease.CurrentState == LeaseState.Initial)
            {
                lease.InitialLeaseTime = this.initialLeaseTime;
                lease.Register(this);
                lease.SponsorshipTimeout = this.sponsorshipTimeout;
                lease.RenewOnCallTime    = TimeSpan.Zero;
            }

            return(lease);
        }
        /// <summary>
        /// Initialises a new instance of the Sponsor&lt;TInterface&gt; class,
        /// wrapping the specified object instance.
        /// </summary>
        /// <param name="instance"></param>
        public Sponsor(TInterface instance)
        {
            Instance = instance;

            if (Instance is MarshalByRefObject)
            {
                object lifetimeService = RemotingServices.GetLifetimeService((MarshalByRefObject)(object)Instance);
                if (lifetimeService is ILease)
                {
                    ILease lease = (ILease)lifetimeService;
                    lease.Register(this);
                }
            }
        }
Beispiel #32
0
        public DebuggerServer(IDebuggerController dc)
        {
            this.controller       = dc;
            mdbObjectValueAdaptor = new MdbObjectValueAdaptor();
            MarshalByRefObject mbr   = (MarshalByRefObject)controller;
            ILease             lease = mbr.GetLifetimeService() as ILease;

            lease.Register(this);
            max_frames = 100;

            ST.Thread t = new ST.Thread((ST.ThreadStart)EventDispatcher);
            t.IsBackground = true;
            t.Start();
        }
Beispiel #33
0
        /// <summary>
        /// Loads the user code.
        /// </summary>
        /// <param name="AssemblyPath">The assembly path.</param>
        private static void Load()
        {
            if (!File.Exists(AssemblyPath))
                throw new FileNotFoundException("The assembly cannot be found.", AssemblyPath);

            MarshalByRefObject  obj;
            ad = AppDomain.CreateDomain("Sandbox", null, ads);
            obj = (MarshalByRefObject)ad.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, "ScriptKeyCode.RemoteLoader");
            target = (IScript)obj;
            try
            {
                lease = (ILease)obj.GetLifetimeService();
                sponser = new RemoteClientSponser("CodeHost");
                lease.Register(sponser);
                target.Load(AssemblyPath);
            }
            catch (Exception ex)
            {
                OnErrorOccured(ex);
            }
        }