//This function is invoked the first time a request is made to XAMLx file
            //It caches url as key and one of the
            //following 4 as value -> "Handler/Factory/HandlerCLRType/Exception"
            IHttpHandler GetHandlerFirstTime(HttpContext context, string requestType,
                                             string url, string pathTranslated)
            {
                Type httpHandlerType;
                ConfigurationErrorsException configException;

                //GetCompiledType is costly - invoke it just once.
                //This null check is required for "error after GetCompiledType on first attempt" cases only
                if (this.hostedXamlType == null)
                {
                    this.hostedXamlType = GetCompiledCustomString(context.Request.AppRelativeCurrentExecutionFilePath);
                }

                if (XamlHostingConfiguration.TryGetHttpHandlerType(url, this.hostedXamlType, out httpHandlerType))
                {
                    if (TD.HttpHandlerPickedForUrlIsEnabled())
                    {
                        TD.HttpHandlerPickedForUrl(url, hostedXamlType.FullName, httpHandlerType.FullName);
                    }
                    if (typeof(IHttpHandler).IsAssignableFrom(httpHandlerType))
                    {
                        IHttpHandler handler = (IHttpHandler)CreateInstance(httpHandlerType);
                        if (handler.IsReusable)
                        {
                            this.cachedResult = handler;
                        }
                        else
                        {
                            this.cachedResult = httpHandlerType;
                        }
                        return(handler);
                    }
                    else if (typeof(IHttpHandlerFactory).IsAssignableFrom(httpHandlerType))
                    {
                        IHttpHandlerFactory factory = (IHttpHandlerFactory)CreateInstance(httpHandlerType);
                        this.cachedResult = factory;
                        IHttpHandler handler = factory.GetHandler(context, requestType, url, pathTranslated);
                        return(HandlerWrapper.Create(handler, factory));
                    }
                    else
                    {
                        configException =
                            new ConfigurationErrorsException(SR.NotHttpHandlerType(url, this.hostedXamlType, httpHandlerType.FullName));
                        this.cachedResult = configException;
                        throw FxTrace.Exception.AsError(configException);
                    }
                }
                configException =
                    new ConfigurationErrorsException(SR.HttpHandlerForXamlTypeNotFound(url, this.hostedXamlType, XamlHostingConfiguration.XamlHostingSection));
                this.cachedResult = configException;
                throw FxTrace.Exception.AsError(configException);
            }
 //This function retrievs the cached object and uses it to get handler or exception
 IHttpHandler GetHandlerSubSequent(HttpContext context, string requestType,
                                   string url, string pathTranslated)
 {
     if (this.cachedResult is IHttpHandler)
     {
         return((IHttpHandler)this.cachedResult);
     }
     else if (this.cachedResult is IHttpHandlerFactory)
     {
         IHttpHandlerFactory factory = ((IHttpHandlerFactory)this.cachedResult);
         IHttpHandler        handler = factory.GetHandler(context, requestType, url, pathTranslated);
         return(HandlerWrapper.Create(handler, factory));
     }
     else if (this.cachedResult is Type)
     {
         return((IHttpHandler)CreateInstance((Type)this.cachedResult));
     }
     else
     {
         throw FxTrace.Exception.AsError((ConfigurationErrorsException)this.cachedResult);
     }
 }