/// <summary>
        /// Invoked when the request is beginning.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        /// <param name="cb"></param>
        /// <param name="extraData"></param>
        /// <returns></returns>
        IAsyncResult BeginOnBeginRequestAsync(object sender, EventArgs args, AsyncCallback cb, object extraData)
        {
            // ignore spurious calls
            var context = HttpContext.Current;

            if (context == null)
            {
                return(new CompletedAsyncResult(null, null));
            }

            // only generate for classic ASP requests
            if (IsAspPage(context))
            {
                // generate new proxy reference to current request context
                var proxy = new ComponentContextProxy(() => GetAutofacRequestContext(context.ApplicationInstance));
                context.Items[ContextProxyKey] = proxy;

                // add serialized object ref into a header, reachable by classic ASP
                var intPtr = Marshal.GetIUnknownForObject(proxy);
                context.Request.Headers.Add(ComponentContextUtil.HeadersProxyItemKey, intPtr.ToInt64().ToString("X"));

                // ensures that the reference is released if the context is abandoned
                context.Items[DisposerActionKey] = new DisposableAction(() =>
                {
                    while (Marshal.Release(intPtr) > 0)
                    {
                        continue;
                    }
                });
            }

            return(new CompletedAsyncResult(null, null));
        }
        /// <summary>
        /// Initializes the instance.
        /// </summary>
        /// <param name="context"></param>
        public void Init(HttpApplication context)
        {
            lock (rootSyncRoot)
            {
                // only enable if Autofac.Web is configured
                if (context is IContainerProviderAccessor accessor)
                {
                    // connect the app domain proxy to the root container
                    var container = GetAutofacApplicationContext(context);
                    var rootProxy = new ComponentContextProxy(() => container);

                    // store reference to this application in the default app domain
                    var appDomainKey = $"{ComponentContextUtil.AppDomainItemPrefix}{HostingEnvironment.ApplicationID}";
                    new mscoree.CorRuntimeHost().GetDefaultDomain(out var adv);
                    if (adv is AppDomain ad && ad.IsDefaultAppDomain() && ad.GetData(appDomainKey) == null)
                    {
                        ad.SetData(appDomainKey, Marshal.GetIUnknownForObject(rootProxy));
                    }
                }
            }

            // register request events for context
            context.AddOnBeginRequestAsync(BeginOnBeginRequestAsync, EndOnBeginRequestAsync);
            context.AddOnEndRequestAsync(BeginOnEndRequestAsync, EndOnEndRequestAsync);
        }