private HostingEnvironment CreateAppDomainWithHostingEnvironment(string appId, IApplicationHost appHost, HostingEnvironmentParameters hostingParameters)
 {
     string physicalPath = appHost.GetPhysicalPath();
     if (!System.Web.Util.StringUtil.StringEndsWith(physicalPath, Path.DirectorySeparatorChar))
     {
         physicalPath = physicalPath + Path.DirectorySeparatorChar;
     }
     string domainId = ConstructAppDomainId(appId);
     string appName = System.Web.Util.StringUtil.GetStringHashCode(appId.ToLower(CultureInfo.InvariantCulture) + physicalPath.ToLower(CultureInfo.InvariantCulture)).ToString("x", CultureInfo.InvariantCulture);
     VirtualPath appVPath = VirtualPath.Create(appHost.GetVirtualPath());
     IDictionary dict = new Hashtable(20);
     AppDomainSetup setup = new AppDomainSetup();
     PopulateDomainBindings(domainId, appId, appName, physicalPath, appVPath, setup, dict);
     AppDomain domain = null;
     Exception innerException = null;
     string siteID = appHost.GetSiteID();
     string virtualPathStringNoTrailingSlash = appVPath.VirtualPathStringNoTrailingSlash;
     bool flag = false;
     bool flag2 = false;
     System.Configuration.Configuration configuration = null;
     PolicyLevel policyLevel = null;
     PermissionSet grantSet = null;
     List<StrongName> list = new List<StrongName>();
     string[] strArray = new string[] { "System.Web, PublicKey=002400000480000094000000060200000024000052534131000400000100010007d1fa57c4aed9f0a32e84aa0faefd0de9e8fd6aec8f87fb03766c834c99921eb23be79ad9d5dcc1dd9ad236132102900b723cf980957fc4e177108fc607774f29e8320e92ea05ece4e821c0a5efe8f1645c4c0c93c1ab99285d622caa652c1dfad63d745d6f2de5f17e5eaf0fc4963d261c8a12436518206dc093344d5ad293", "System.Web.Extensions, PublicKey=0024000004800000940000000602000000240000525341310004000001000100b5fc90e7027f67871e773a8fde8938c81dd402ba65b9201d60593e96c492651e889cc13f1415ebb53fac1131ae0bd333c5ee6021672d9718ea31a8aebd0da0072f25d87dba6fc90ffd598ed4da35e44c398c454307e8e33b8426143daec9f596836f97c8f74750e5975c64e2189f45def46b2a2b1247adc3652bf5c308055da9", "System.Web.Abstractions, PublicKey=0024000004800000940000000602000000240000525341310004000001000100b5fc90e7027f67871e773a8fde8938c81dd402ba65b9201d60593e96c492651e889cc13f1415ebb53fac1131ae0bd333c5ee6021672d9718ea31a8aebd0da0072f25d87dba6fc90ffd598ed4da35e44c398c454307e8e33b8426143daec9f596836f97c8f74750e5975c64e2189f45def46b2a2b1247adc3652bf5c308055da9", "System.Web.Routing, PublicKey=0024000004800000940000000602000000240000525341310004000001000100b5fc90e7027f67871e773a8fde8938c81dd402ba65b9201d60593e96c492651e889cc13f1415ebb53fac1131ae0bd333c5ee6021672d9718ea31a8aebd0da0072f25d87dba6fc90ffd598ed4da35e44c398c454307e8e33b8426143daec9f596836f97c8f74750e5975c64e2189f45def46b2a2b1247adc3652bf5c308055da9", "System.ComponentModel.DataAnnotations, PublicKey=0024000004800000940000000602000000240000525341310004000001000100b5fc90e7027f67871e773a8fde8938c81dd402ba65b9201d60593e96c492651e889cc13f1415ebb53fac1131ae0bd333c5ee6021672d9718ea31a8aebd0da0072f25d87dba6fc90ffd598ed4da35e44c398c454307e8e33b8426143daec9f596836f97c8f74750e5975c64e2189f45def46b2a2b1247adc3652bf5c308055da9", "System.Web.DynamicData, PublicKey=0024000004800000940000000602000000240000525341310004000001000100b5fc90e7027f67871e773a8fde8938c81dd402ba65b9201d60593e96c492651e889cc13f1415ebb53fac1131ae0bd333c5ee6021672d9718ea31a8aebd0da0072f25d87dba6fc90ffd598ed4da35e44c398c454307e8e33b8426143daec9f596836f97c8f74750e5975c64e2189f45def46b2a2b1247adc3652bf5c308055da9", "System.Web.DataVisualization, PublicKey=0024000004800000940000000602000000240000525341310004000001000100b5fc90e7027f67871e773a8fde8938c81dd402ba65b9201d60593e96c492651e889cc13f1415ebb53fac1131ae0bd333c5ee6021672d9718ea31a8aebd0da0072f25d87dba6fc90ffd598ed4da35e44c398c454307e8e33b8426143daec9f596836f97c8f74750e5975c64e2189f45def46b2a2b1247adc3652bf5c308055da9", "System.Web.ApplicationServices, PublicKey=0024000004800000940000000602000000240000525341310004000001000100b5fc90e7027f67871e773a8fde8938c81dd402ba65b9201d60593e96c492651e889cc13f1415ebb53fac1131ae0bd333c5ee6021672d9718ea31a8aebd0da0072f25d87dba6fc90ffd598ed4da35e44c398c454307e8e33b8426143daec9f596836f97c8f74750e5975c64e2189f45def46b2a2b1247adc3652bf5c308055da9" };
     Exception appDomainCreationException = null;
     ImpersonationContext context = null;
     IntPtr zero = IntPtr.Zero;
     if ((hostingParameters != null) && ((hostingParameters.HostingFlags & HostingEnvironmentFlags.ClientBuildManager) != HostingEnvironmentFlags.Default))
     {
         flag2 = true;
         setup.LoaderOptimization = LoaderOptimization.MultiDomainHost;
     }
     try
     {
         zero = appHost.GetConfigToken();
         if (zero != IntPtr.Zero)
         {
             context = new ImpersonationContext(zero);
         }
         try
         {
             if (flag2 && (hostingParameters.IISExpressVersion != null))
             {
                 grantSet = new PermissionSet(PermissionState.Unrestricted);
                 setup.PartialTrustVisibleAssemblies = strArray;
             }
             else
             {
                 if (appHost is ISAPIApplicationHost)
                 {
                     string key = "f" + siteID + appVPath.VirtualPathString;
                     MapPathCacheInfo info1 = (MapPathCacheInfo) HttpRuntime.CacheInternal.Remove(key);
                     configuration = WebConfigurationManager.OpenWebConfiguration(virtualPathStringNoTrailingSlash, siteID);
                 }
                 else
                 {
                     WebConfigurationFileMap fileMap = new WebConfigurationFileMap();
                     IConfigMapPath path2 = appHost.GetConfigMapPathFactory().Create(appVPath.VirtualPathString, physicalPath);
                     string directory = null;
                     string baseName = null;
                     string path = "/";
                     path2.GetPathConfigFilename(siteID, path, out directory, out baseName);
                     if (directory != null)
                     {
                         fileMap.VirtualDirectories.Add(path, new VirtualDirectoryMapping(Path.GetFullPath(directory), true));
                     }
                     foreach (string str10 in virtualPathStringNoTrailingSlash.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries))
                     {
                         path = path + str10;
                         path2.GetPathConfigFilename(siteID, path, out directory, out baseName);
                         if (directory != null)
                         {
                             fileMap.VirtualDirectories.Add(path, new VirtualDirectoryMapping(Path.GetFullPath(directory), true));
                         }
                         path = path + "/";
                     }
                     configuration = WebConfigurationManager.OpenMappedWebConfiguration(fileMap, virtualPathStringNoTrailingSlash, siteID);
                 }
                 TrustSection trustSection = (TrustSection) configuration.GetSection("system.web/trust");
                 if ((trustSection == null) || string.IsNullOrEmpty(trustSection.Level))
                 {
                     throw new ConfigurationErrorsException(System.Web.SR.GetString("Config_section_not_present", new object[] { "trust" }));
                 }
                 bool legacyCasModel = trustSection.LegacyCasModel;
                 if (legacyCasModel)
                 {
                     SetNetFx40LegacySecurityPolicy(setup);
                 }
                 if (flag2)
                 {
                     grantSet = new PermissionSet(PermissionState.Unrestricted);
                     setup.PartialTrustVisibleAssemblies = strArray;
                 }
                 else
                 {
                     flag = legacyCasModel;
                     if (!flag)
                     {
                         if (trustSection.Level == "Full")
                         {
                             grantSet = new PermissionSet(PermissionState.Unrestricted);
                             setup.PartialTrustVisibleAssemblies = strArray;
                         }
                         else
                         {
                             SecurityPolicySection section = (SecurityPolicySection) configuration.GetSection("system.web/securityPolicy");
                             CompilationSection compilationSection = (CompilationSection) configuration.GetSection("system.web/compilation");
                             FullTrustAssembliesSection section4 = (FullTrustAssembliesSection) configuration.GetSection("system.web/fullTrustAssemblies");
                             policyLevel = GetPartialTrustPolicyLevel(trustSection, section, compilationSection, physicalPath, appVPath);
                             grantSet = policyLevel.GetNamedPermissionSet(trustSection.PermissionSetName);
                             if (grantSet == null)
                             {
                                 throw new ConfigurationErrorsException(System.Web.SR.GetString("Permission_set_not_found", new object[] { trustSection.PermissionSetName }));
                             }
                             if (section4 != null)
                             {
                                 FullTrustAssemblyCollection fullTrustAssemblies = section4.FullTrustAssemblies;
                                 if (fullTrustAssemblies != null)
                                 {
                                     list.AddRange(from fta in fullTrustAssemblies.Cast<FullTrustAssembly>() select CreateStrongName(fta.AssemblyName, fta.Version, fta.PublicKey));
                                 }
                             }
                             if (list.Contains(_mwiV1StrongName))
                             {
                                 list.AddRange(CreateFutureMicrosoftWebInfrastructureStrongNames());
                             }
                             setup.AppDomainManagerType = typeof(AspNetAppDomainManager).FullName;
                             setup.AppDomainManagerAssembly = typeof(AspNetAppDomainManager).Assembly.FullName;
                         }
                     }
                     if (trustSection.Level != "Full")
                     {
                         PartialTrustVisibleAssembliesSection section5 = (PartialTrustVisibleAssembliesSection) configuration.GetSection("system.web/partialTrustVisibleAssemblies");
                         string[] array = null;
                         if (section5 != null)
                         {
                             PartialTrustVisibleAssemblyCollection partialTrustVisibleAssemblies = section5.PartialTrustVisibleAssemblies;
                             if ((partialTrustVisibleAssemblies != null) && (partialTrustVisibleAssemblies.Count != 0))
                             {
                                 array = new string[partialTrustVisibleAssemblies.Count + strArray.Length];
                                 for (int i = 0; i < partialTrustVisibleAssemblies.Count; i++)
                                 {
                                     array[i] = partialTrustVisibleAssemblies[i].AssemblyName + ", PublicKey=" + NormalizePublicKeyBlob(partialTrustVisibleAssemblies[i].PublicKey);
                                 }
                                 strArray.CopyTo(array, partialTrustVisibleAssemblies.Count);
                             }
                         }
                         if (array == null)
                         {
                             array = strArray;
                         }
                         setup.PartialTrustVisibleAssemblies = array;
                     }
                 }
             }
         }
         catch (Exception exception3)
         {
             appDomainCreationException = exception3;
             grantSet = new PermissionSet(PermissionState.Unrestricted);
         }
         try
         {
             if (flag)
             {
                 domain = AppDomain.CreateDomain(domainId, GetDefaultDomainIdentity(), setup);
             }
             else
             {
                 domain = AppDomain.CreateDomain(domainId, GetDefaultDomainIdentity(), setup, grantSet, list.ToArray());
             }
             foreach (DictionaryEntry entry in dict)
             {
                 domain.SetData((string) entry.Key, (string) entry.Value);
             }
         }
         catch (Exception exception4)
         {
             innerException = exception4;
         }
     }
     finally
     {
         if (context != null)
         {
             context.Undo();
             context = null;
         }
         if (zero != IntPtr.Zero)
         {
             System.Web.UnsafeNativeMethods.CloseHandle(zero);
             zero = IntPtr.Zero;
         }
     }
     if (domain == null)
     {
         throw new SystemException(System.Web.SR.GetString("Cannot_create_AppDomain"), innerException);
     }
     Type type = typeof(HostingEnvironment);
     string fullName = type.Module.Assembly.FullName;
     string typeName = type.FullName;
     ObjectHandle handle = null;
     ImpersonationContext context2 = null;
     IntPtr token = IntPtr.Zero;
     int num2 = 10;
     int num3 = 0;
     while (num3 < num2)
     {
         try
         {
             token = appHost.GetConfigToken();
             break;
         }
         catch (InvalidOperationException)
         {
             num3++;
             Thread.Sleep(250);
             continue;
         }
     }
     if (token != IntPtr.Zero)
     {
         try
         {
             context2 = new ImpersonationContext(token);
         }
         catch
         {
         }
         finally
         {
             System.Web.UnsafeNativeMethods.CloseHandle(token);
         }
     }
     try
     {
         handle = Activator.CreateInstance(domain, fullName, typeName);
     }
     finally
     {
         if (context2 != null)
         {
             context2.Undo();
         }
         if (handle == null)
         {
             AppDomain.Unload(domain);
         }
     }
     HostingEnvironment environment = (handle != null) ? (handle.Unwrap() as HostingEnvironment) : null;
     if (environment == null)
     {
         throw new SystemException(System.Web.SR.GetString("Cannot_create_HostEnv"));
     }
     IConfigMapPathFactory configMapPathFactory = appHost.GetConfigMapPathFactory();
     if (appDomainCreationException == null)
     {
         environment.Initialize(this, appHost, configMapPathFactory, hostingParameters, policyLevel);
         return environment;
     }
     environment.Initialize(this, appHost, configMapPathFactory, hostingParameters, policyLevel, appDomainCreationException);
     return environment;
 }