//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);
            }
示例#2
0
        void GetXamlBuildProviderExtensionFactory(XamlType rootXamlType)
        {
            if (xamlBuildProviderExtensionFactory != null)
            {
                return;
            }

            // Get the HttpHandler type
            Type httpHandlerType;

            XamlHostingConfiguration.TryGetHttpHandlerType(this.VirtualPath, rootXamlType.UnderlyingType, out httpHandlerType);

            if (httpHandlerType != null && typeof(IXamlBuildProviderExtensionFactory).IsAssignableFrom(httpHandlerType))
            {
                xamlBuildProviderExtensionFactory = (IXamlBuildProviderExtensionFactory)Activator.CreateInstance(httpHandlerType,
                                                                                                                 BindingFlags.CreateInstance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance,
                                                                                                                 null, null, null);
            }
        }
            private IHttpHandler GetHandlerFirstTime(HttpContext context, string requestType, string url, string pathTranslated)
            {
                Type type;
                ConfigurationErrorsException exception;

                if (this.hostedXamlType == null)
                {
                    this.hostedXamlType = this.GetCompiledCustomString(context.Request.AppRelativeCurrentExecutionFilePath);
                }
                if (XamlHostingConfiguration.TryGetHttpHandlerType(url, this.hostedXamlType, out type))
                {
                    if (TD.HttpHandlerPickedForUrlIsEnabled())
                    {
                        TD.HttpHandlerPickedForUrl(url, this.hostedXamlType.FullName, type.FullName);
                    }
                    if (typeof(IHttpHandler).IsAssignableFrom(type))
                    {
                        IHttpHandler handler = (IHttpHandler)XamlHttpHandlerFactory.CreateInstance(type);
                        if (handler.IsReusable)
                        {
                            this.cachedResult = handler;
                            return(handler);
                        }
                        this.cachedResult = type;
                        return(handler);
                    }
                    if (typeof(IHttpHandlerFactory).IsAssignableFrom(type))
                    {
                        IHttpHandlerFactory factory = (IHttpHandlerFactory)XamlHttpHandlerFactory.CreateInstance(type);
                        this.cachedResult = factory;
                        return(XamlHttpHandlerFactory.HandlerWrapper.Create(factory.GetHandler(context, requestType, url, pathTranslated), factory));
                    }
                    exception         = new ConfigurationErrorsException(System.Xaml.Hosting.SR.NotHttpHandlerType(url, this.hostedXamlType, type.FullName));
                    this.cachedResult = exception;
                    throw FxTrace.Exception.AsError(exception);
                }
                exception         = new ConfigurationErrorsException(System.Xaml.Hosting.SR.HttpHandlerForXamlTypeNotFound(url, this.hostedXamlType, "system.xaml.hosting/httpHandlers"));
                this.cachedResult = exception;
                throw FxTrace.Exception.AsError(exception);
            }