【问题标题】:C# - Increment through a list of webservices in a directory and list available WebmethodsC# - 通过目录中的 Web 服务列表递增并列出可用的 Web 方法
【发布时间】:2010-11-28 12:13:32
【问题描述】:

我正在构建的 C# Web 应用程序中有许多不同的 Web 服务,我想创建一个快速文档页面,其中列出了所有 Web 服务和每个 Web 服务中可用的 Web 方法。当我更改/添加 web 方法时,不必让文档页面保持最新,如果文档是动态的会很好。

对于每个 webmethod,我想从 Webmethod deceleration 和(如果可能的话)每个方法的参数列表中获取 Description 属性。

我知道我可以从 .NET 为 .asmx 页面提供的 Web 服务摘要页面获取大量此类信息,但我不想强迫用户必须不断点击远离主文档页面。

提前致谢。

【问题讨论】:

    标签: c# web-services


    【解决方案1】:

    一个快速的解决方案是编写一个自定义的 Http Handler:

    public class InformationHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            // Select the assembly that contains the web service classes
            var assemblyThatContainsTheWebService = Assembly.GetExecutingAssembly();
    
            // Select all types in this assembly deriving from WebService
            var webServiceTypes = 
                from type in assemblyThatContainsTheWebService.GetTypes()
                where type.BaseType == typeof(WebService)
                select type;
    
            context.Response.ContentType = "text/plain";
    
            foreach (var type in webServiceTypes)
            {
                context.Response.Write(string.Format("Methods for web service {0}:{1}", type, Environment.NewLine));
                // Select all methods marked with the WebMethodAttribute
                var methods = 
                    from method in type.GetMethods()
                    where method.GetCustomAttributes(typeof(WebMethodAttribute), false).Count() > 0
                    select method;
    
                foreach (var method in methods)
                {
                    context.Response.Write(method);
                }
                context.Response.Write(Environment.NewLine);
            }
    
        }
    
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
    

    【讨论】:

    • 很可爱。我看到的一个挑剔:如果服务关闭,文档就会关闭,但这仍然很好。折叠 xml cmets 也很酷。
    • 感谢 darin,这会很好地工作 - 只是一个注释(对于这篇文章的任何未来浏览器)我正在使用 .NET 2.0,所以我只需要用一些条件替换 LINQ 语句。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-04
    • 1970-01-01
    • 2010-12-24
    • 1970-01-01
    相关资源
    最近更新 更多