开发者

Programmatically set HTTP handlers in an ASP.NET application

开发者 https://www.devze.com 2023-01-23 08:45 出处:网络
I need to dynamically instantiate a web application from a console application. By this definition, I mean that my console application contains a web application that is not bound to IIS/XSP.

I need to dynamically instantiate a web application from a console application. By this definition, I mean that my console application contains a web application that is not bound to IIS/XSP.

Currently, I create the web application into a temporary directory and copy some forged files into it. These are a special Global.asax that maps to my own implementation of HttpApplication to use in the web application (I need to do some initialization at app start), then I forge special .asmx files that map to my own skeleton classes and dynamic plugins

            foreach (IPlugin plugin in开发者_JAVA技巧 _target.Plugins)
            {
                WsdlSkeletonDefinition[] defs = plugin.GetWsdlSkeletons();
                if (defs != null)
                    foreach (WsdlSkeletonDefinition def in defs)
                    {
                        if (def.SkeletonType == null)
                            throw new LogbusException(string.Format("Plugin {0} declares empty skeleton type",
                                                                    plugin.Name));
                        if (def.SkeletonType.IsAssignableFrom(typeof(System.Web.Services.WebService)))
                            throw new LogbusException(
                                string.Format("Plugin {0} does not declare a valid WSDL skeleton type", plugin.Name));

                        string fname = def.UrlFileName;
                        if (fname.EndsWith(".asmx", false, CultureInfo.InvariantCulture))
                            fname = fname.Substring(0, fname.Length - 5);

                        if (!Regex.IsMatch(fname, @"^[a-zA-Z0-9_\.\-%]+$", RegexOptions.CultureInvariant))
                            throw new LogbusException(string.Format(
                                "Plugin {0} declares invalid WSDL endpoint: {1}",
                                plugin.Name, def.UrlFileName));

                        string wsDeclaration = string.Format(ASMX_TEMPLATE, def.SkeletonType.AssemblyQualifiedName);

                        using (
                            StreamWriter sw = new StreamWriter(File.Create(Path.Combine(_physicalPath, fname + ".asmx")),
                                                               Encoding.Default))
                            sw.Write(wsDeclaration);

                        //Copy skeleton asembly if needed
                        CopyAssemblyTo(def.SkeletonType.Assembly, bindir);
                        foreach (AssemblyName dependency in def.SkeletonType.Assembly.GetReferencedAssemblies())
                        {
                            try
                            {
                                CopyAssemblyTo(Assembly.Load(dependency), bindir);
                            }
                            //Possible broken dependency
                            catch { }
                        }
                    }
            }

My approach works, but I'm not so satisfied by it because I have to write lots of garbage into file system, even if I eventually delete it all.

I know I can control HTTP handlers via Web.config, but I don't want to forge a Web.config for that. I would like to create a mapping such as I can remove the .asmx extension from web services' URLs and still get them.

For example, one of the default scripts is "LogbusManagement.asmx", which must be hard-coded into client APIs and the .asmx prevents portability to other platforms such as PHP. I want to make "LogbusManagement.asmx" equivalent to "LogbusManagement" and any extension. For this, I might use an HttpHandlerFactory.

My straight question is,

like asked here by somebody else: is there a way to programmatically, possibly from Global.asax, to set IHttpHandlers or IHttpHandlerFactories for web applications?

Thank you


This question was already answered in the stackoverflow:

Any way to add HttpHandler programatically in .NET?

0

精彩评论

暂无评论...
验证码 换一张
取 消