Inheritance: System.MarshalByRefObject
Ejemplo n.º 1
0
 public static void SetPreAppStartStage() {
     var stage = typeof(BuildManager).GetProperty("PreStartInitStage", BindingFlags.Static | BindingFlags.NonPublic);
     var value = ((FieldInfo)typeof(BuildManager).Assembly.GetType("System.Web.Compilation.PreStartInitStage").GetMember("DuringPreStartInit")[0]).GetValue(null);
     stage.SetValue(null, value, new object[] { });
     SetAppData();
     var env = new HostingEnvironment();
 }
 public HostingEnvironment()
 {
     if (_theHostingEnvironment != null)
     {
         throw new InvalidOperationException(System.Web.SR.GetString("Only_1_HostEnv"));
     }
     _theHostingEnvironment = this;
     this._onAppDomainUnload = new EventHandler(this.OnAppDomainUnload);
     Thread.GetDomain().DomainUnload += this._onAppDomainUnload;
 }
Ejemplo n.º 3
0
        //private readonly object _fileChangesManager;
        //private readonly FieldInfo _callbackFieldInfo;
        //private readonly FieldInfo _isFCNDisabledFieldInfo;
        //private readonly object _savedCallbackValue;


        /// <exclude />
        public ShutdownGuard()
        {
            if (!HostingEnvironment.IsHosted)
            {
                return;
            }

            const BindingFlags getStaticFieldValue = BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.GetField;
            _runtime = (HttpRuntime)typeof(HttpRuntime).InvokeMember("_theRuntime", getStaticFieldValue, null, null, null);
            _hostingEnvironment = (HostingEnvironment)typeof(HostingEnvironment).InvokeMember("_theHostingEnvironment", getStaticFieldValue, null, null, null);


            const BindingFlags privateField = BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField;
            _shutdownWebEventRaised_FieldInfo = typeof(HttpRuntime).GetField("_shutdownWebEventRaised", privateField);

            // In .NET 3.5 the field is called "_shutdownInitated"
            //    .NET 4.0 the field is called "_shutdownInitiated"
            _shutdownInitiated_FieldInfo = typeof(HostingEnvironment).GetField("_shutdownInitiated", privateField)
                                        ?? typeof(HostingEnvironment).GetField("_shutdownInitated",  privateField);

            // Simulating situation, when all events to unload current AppDomain were already raised.
            _shutdownWebEventRaised_FieldInfo.SetValue(_runtime, true);
            _shutdownInitiated_FieldInfo.SetValue(_hostingEnvironment, true);



            //_fileChangesManager = runtime.GetType().GetField("_fcm", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField).GetValue(runtime);

            //_callbackFieldInfo = _fileChangesManager.GetType().GetField("_callbackRenameOrCriticaldirChange",
            //                                                                    BindingFlags.NonPublic |
            //                                                                    BindingFlags.Instance |
            //                                                                    BindingFlags.GetField);

            //_isFCNDisabledFieldInfo = _fileChangesManager.GetType().GetField("_FCNMode",
            //                                                                    BindingFlags.NonPublic |
            //                                                                    BindingFlags.Instance |
            //                                                                    BindingFlags.GetField);

            //_savedCallbackValue = _callbackFieldInfo.GetValue(_fileChangesManager);
            //_callbackFieldInfo.SetValue(_fileChangesManager, null);

            //// Turning off file change notifications. http://support.microsoft.com/kb/911272
            //_isFCNDisabledFieldInfo.SetValue(_fileChangesManager, (Int32)1);


        }
Ejemplo n.º 4
0
 protected static HostingEnvironment GetHostingEnvironment()
 {
     HostingEnvironment environment;
     try
     {
         environment = new HostingEnvironment();
     }
     catch (InvalidOperationException)
     {
         //Shoot, we need to grab it via reflection.
         environment = ReflectionHelper.GetStaticFieldValue<HostingEnvironment>("_theHostingEnvironment", typeof(HostingEnvironment));
     }
     return environment;
 }
Ejemplo n.º 5
0
        public static void SetupVirtualPathInAppDomain(string vpath, string contents)
        {
            var file = new Mock<VirtualFile>(vpath);
            file.Setup(f => f.Open()).Returns(new MemoryStream(ASCIIEncoding.Default.GetBytes(contents)));
            var vpp = new Mock<VirtualPathProvider>();
            vpp.Setup(p => p.FileExists(vpath)).Returns(true);
            vpp.Setup(p => p.GetFile(vpath)).Returns(file.Object);
            AppDomainUtils.SetAppData();
            var env = new HostingEnvironment();

            var register = typeof(HostingEnvironment).GetMethod("RegisterVirtualPathProviderInternal", BindingFlags.Static | BindingFlags.NonPublic);
            register.Invoke(null, new object[] { vpp.Object });
        }
        public HostingEnvironment() {
            if (_theHostingEnvironment != null)
                throw new InvalidOperationException(SR.GetString(SR.Only_1_HostEnv));

            // remember singleton HostingEnvironment in a static
            _theHostingEnvironment = this;

            // start watching for app domain unloading
            _onAppDomainUnload = new EventHandler(OnAppDomainUnload);
            Thread.GetDomain().DomainUnload += _onAppDomainUnload;
        }
Ejemplo n.º 7
0
        public HostingEnvironment() {
            if (_theHostingEnvironment != null)
                throw new InvalidOperationException(SR.GetString(SR.Only_1_HostEnv));

            // remember singleton HostingEnvironment in a static
            _theHostingEnvironment = this;

            // start watching for app domain unloading
            _onAppDomainUnload = new EventHandler(OnAppDomainUnload);
            Thread.GetDomain().DomainUnload += _onAppDomainUnload;

            // VSO 160528: We used to listen to the default AppDomain's UnhandledException only.
            // However, non-serializable exceptions cannot be passed to the default domain. Therefore
            // we should try to log exceptions in application AppDomains.
            Thread.GetDomain().UnhandledException += new UnhandledExceptionEventHandler(ApplicationManager.OnUnhandledException);
        }
        private HostingEnvironmentWrapper(object stub)
        {
            _Inner = new HostingEnvironment();

              Configure();
        }
 internal void HostingEnvironmentShutdownInitiated(string appId, HostingEnvironment env)
 {
     if (!this._shutdownInProgress)
     {
         LockableAppDomainContext lockableAppDomainContext = this.GetLockableAppDomainContext(appId);
         lock (lockableAppDomainContext)
         {
             if (!env.HasBeenRemovedFromAppManagerTable)
             {
                 env.HasBeenRemovedFromAppManagerTable = true;
                 lockableAppDomainContext.HostEnv = null;
                 Interlocked.Decrement(ref this._accessibleHostingEnvCount);
                 if ((lockableAppDomainContext.PreloadContext != null) && !lockableAppDomainContext.RetryingPreload)
                 {
                     ProcessHost.PreloadApplicationIfNotShuttingdown(appId, lockableAppDomainContext);
                 }
             }
         }
     }
 }
Ejemplo n.º 10
0
        public void FileExistsTest() {
            AppDomainUtils.RunInSeparateAppDomain(() => {
                var env = new HostingEnvironment();
                AppDomainUtils.SetAppData();
                var vpp = new Mock<VirtualPathProvider>();
                var vpath1 = "/website1/default.cshtml";
                var vpath2 = "/website2/default.cshtml";
                vpp.Setup(p => p.FileExists(vpath1)).Returns(true).Verifiable();
                vpp.Setup(p => p.FileExists(vpath2)).Returns(false).Verifiable();

                var register = typeof(HostingEnvironment).GetMethod("RegisterVirtualPathProviderInternal", BindingFlags.Static | BindingFlags.NonPublic);
                register.Invoke(null, new object[] { vpp.Object });

                var initPage = new Mock<StartPage>().Object;
                Assert.IsTrue(initPage.FileExists(vpath1));
                Assert.IsFalse(initPage.FileExists(vpath2));
                vpp.Verify();
            });
        }
Ejemplo n.º 11
0
        private void InitHostingEnvironment()
        {
            try
            {
                var env = new HostingEnvironment();

                var type = env.GetType();
                var init = type
                    .GetMethods(BindingFlags.Instance | BindingFlags.NonPublic)
                    .FirstOrDefault(m => m.Name == "Initialize");

                var args = new object[init.GetParameters().Length];
                args[0] = ApplicationManager.GetApplicationManager();
                args[1] = this;
                args[2] = GetConfigMapPathFactory();

                init.Invoke(env, args);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
Ejemplo n.º 12
0
        private void InitHostingEnvironment()
        {
            try
            {
                // very hacky way to initialize ASP.NET runtime inside same app domain

                var appSettings = ConfigurationManager.AppSettings;
                Debug.Assert(appSettings != null);

                var cfg = typeof(ConfigurationManager).GetStaticField("s_configSystem");

                var env = new HostingEnvironment();

                var configSystem = env.GetType().Assembly.GetType("System.Web.Configuration.HttpConfigurationSystem", false);

                var cfgsys = cfg.GetField("_configSystem");
                configSystem.SetStaticField("s_configSystem", cfgsys);
                configSystem.SetStaticField("s_configRoot", cfgsys.GetField("_configRoot"));
                configSystem.SetStaticField("s_inited", true);

                var type = env.GetType();
                var init = type
                    .GetMethods(BindingFlags.Instance | BindingFlags.NonPublic)
                    .FirstOrDefault(m => m.Name == "Initialize");

                var args = (from p in init.GetParameters() select GetDefaultValue(p.ParameterType)).ToArray();
                args[0] = ApplicationManager.GetApplicationManager();
                args[1] = this;
                args[2] = GetConfigMapPathFactory();

                init.Invoke(env, args);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }