// helper method // private static T ActivateOutOfProcess <T>(AddInToken token, AddInEnvironment environment, bool weOwn) { ActivationWorker worker; IContract contract = environment.AddInServerWorker.Activate(token, out worker); AddInControllerImpl controller = new AddInControllerImpl(environment, weOwn, token); controller.ActivationWorker = worker; T hav = AdaptToHost <T>(token, contract); if (weOwn) { environment.AddInServerWorker.SetAppDomainOwner(contract); } // Add this HAV and add-in controller to our list of currently // non-disposed add-ins. controller.AssociateWithHostAddinView(hav, contract); return(hav); }
private static T ActivateInAppDomain <T>(AddInToken pipeline, AppDomain domain, AddInControllerImpl controller, bool weOwn) { ContractComponent contract = pipeline._contract; HostAdapter hostAdapter = pipeline._hostAdapter; bool usingHostAppDomain = domain == AppDomain.CurrentDomain; //begin direct connect code if (AddInToken.EnableDirectConnect && !weOwn && usingHostAppDomain) { Type havType = typeof(T); TypeInfo havTypeInfo = new TypeInfo(havType); if (pipeline._addinBase.CanDirectConnectTo(havTypeInfo)) { // Connect directly for best performance. // Assert permission to the specific Addin directory only. PermissionSet permissionSet = new PermissionSet(PermissionState.None); permissionSet.AddPermission(new FileIOPermission(FileIOPermissionAccess.Read | FileIOPermissionAccess.PathDiscovery, Path.GetDirectoryName(pipeline._addin.Location))); permissionSet.Assert(); Assembly addInAssembly = Assembly.LoadFrom(pipeline._addin.Location); // Type addinType = addInAssembly.GetType(pipeline._addin.TypeInfo.FullName, true); Object addIn = addinType.GetConstructor(new Type[0]).Invoke(new Object[0]); System.Diagnostics.Contracts.Contract.Assert(addIn != null, "Bypass couldn't create the add-in"); // remember the addin directly as the HAV. Set the contract to null. controller.AssociateWithHostAddinView(addIn, null); return((T)addIn); } } //end direct connect code // Use Activator.CreateInstance instead of AppDomain.CreateInstanceAndUnwrap // because Activator will do the appropriate security asserts in the // remote appdomain. Type t = typeof(ActivationWorker); Object[] args = new Object[] { pipeline }; ObjectHandle objHandle = Activator.CreateInstance(domain, t.Assembly.FullName, t.FullName, false, BindingFlags.Instance | BindingFlags.NonPublic, null, args, null, null); ActivationWorker activationWorker = (ActivationWorker)objHandle.Unwrap(); activationWorker.UsingHostAppDomain = usingHostAppDomain; System.AddIn.Contract.IContract addInContract = null; try { addInContract = activationWorker.Activate(); } catch (Exception ex) { CheckForDuplicateAssemblyProblems(pipeline, ex); throw; } if (weOwn) { domain.SetData(ContractHandle.s_appDomainOwner, addInContract); } controller.ActivationWorker = activationWorker; T hav = AdaptToHost <T>(pipeline, addInContract); controller.AssociateWithHostAddinView(hav, addInContract); return(hav); }