仿真SoapUI,获取WebService接口的服务类与方法

83 篇文章 3 订阅
13 篇文章 6 订阅

上一篇 C#获取WebService接口的所有可调用方法[WebMethod]

C#获取WebService接口的所有可调用方法[WebMethod]_斯内科的博客-CSDN博客_c# webservice接口调用

这次对WebService相关类与方法的特性显示出来。

新建窗体应用程序DetailWebServiceDemo,将默认的Form1重命名为FormDetailWebService.

添加对System.Web.Services类库的引用

窗体FormDetailWebService设计如图:

 新建Soap自动生成并反射的关键类文件SoapUtil.cs

SoapUtil类源程序如下:

using System;
using System.Collections.Generic;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.IO;
using System.Net;
using System.Reflection;
using System.Web.Services.Description;
using System.Xml.Serialization;
using System.Text;
using System.Xml;

namespace DetailWebServiceDemo
{
    /// <summary>
    /// 动态获取WebService的所有方法
    /// 斯内科 2022-06-08
    /// </summary>
    public class SoapUtil
    {
        /// <summary>
        /// 获取WebService接口的所有WebMethod方法
        /// 通过WebService方法的特性为【System.Web.Services.Protocols.SoapDocumentMethodAttribute】
        /// 根据特性SoapDocumentMethodAttribute来筛选出所有WebMethod方法
        /// </summary>
        /// <param name="url"></param>
        public static List<MethodInfo> GetAllWebMethodsFromService(string url, out string className, out Type type)
        {
            className = "SoapWebService";
            if (!url.EndsWith("?wsdl", StringComparison.CurrentCultureIgnoreCase))
            {
                url = url + "?wsdl";
            }
            string tempUrl = url;
            if (url.EndsWith("?wsdl", StringComparison.CurrentCultureIgnoreCase))
            {
                tempUrl = url.Substring(0, url.Length - 5);
            }
            className = Path.GetFileNameWithoutExtension(tempUrl);
            
            // 1. 使用 WebClient 下载 WSDL 信息。
            WebClient web = new WebClient();
            Stream stream = web.OpenRead(url);
            // 2. 创建和格式化 WSDL 文档。
            ServiceDescription description = ServiceDescription.Read(stream);
            // 3. 创建客户端代理代理类。
            ServiceDescriptionImporter importer = new ServiceDescriptionImporter();
            // 指定访问协议。
            importer.ProtocolName = "Soap";
            // 生成客户端代理。
            importer.Style = ServiceDescriptionImportStyle.Client;
            importer.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties | CodeGenerationOptions.GenerateNewAsync;
            // 添加 WSDL 文档。
            importer.AddServiceDescription(description, null, null);
            // 4. 使用 CodeDom 编译客户端代理类。
            // 为代理类添加命名空间,缺省为全局空间。
            CodeNamespace nmspace = new CodeNamespace();
            CodeCompileUnit unit = new CodeCompileUnit();
            unit.Namespaces.Add(nmspace);
            ServiceDescriptionImportWarnings warning = importer.Import(nmspace, unit);
            CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");

            Microsoft.CSharp.CSharpCodeProvider
            //CodeTypeDeclaration codeTypeDeclaration = new CodeTypeDeclaration(className);
             Declare a new generated code attribute
            //GeneratedCodeAttribute generatedCodeAttribute = new GeneratedCodeAttribute("System.Web.Services", "4.8.3761.0");
             Use the generated code attribute members in the attribute declaration
            //CodeAttributeDeclaration codeAttrDecl = new CodeAttributeDeclaration(generatedCodeAttribute.GetType().Name,
            //        new CodeAttributeArgument(new CodePrimitiveExpression(generatedCodeAttribute.Tool)),
            //        new CodeAttributeArgument(new CodePrimitiveExpression(generatedCodeAttribute.Version)));
            //codeTypeDeclaration.CustomAttributes.Add(codeAttrDecl);
            //provider.GenerateCodeFromType(codeTypeDeclaration, Console.Out, new CodeGeneratorOptions());

            CompilerParameters parameter = new CompilerParameters();
            parameter.GenerateExecutable = false;
            parameter.GenerateInMemory = true;//在内存中生成输出
            // 可以指定你所需的任何文件名。
            parameter.OutputAssembly = AppDomain.CurrentDomain.BaseDirectory + className + ".dll";
            parameter.ReferencedAssemblies.Add("System.dll");
            parameter.ReferencedAssemblies.Add("System.XML.dll");
            parameter.ReferencedAssemblies.Add("System.Web.Services.dll");
            parameter.ReferencedAssemblies.Add("System.Data.dll");
            // 生成dll文件,并会把WebService信息写入到dll里面
            CompilerResults result = provider.CompileAssemblyFromDom(parameter, unit);

            Assembly assembly = result.CompiledAssembly;
            type = assembly.GetType(className);
            List<MethodInfo> methodInfoList = new List<MethodInfo>();
            MethodInfo[] methodInfos = type.GetMethods();
            for (int i = 0; i < methodInfos.Length; i++)
            {
                MethodInfo methodInfo = methodInfos[i];
                //WebMethod方法的特性为:System.Web.Services.Protocols.SoapDocumentMethodAttribute 
                Attribute attribute = methodInfo.GetCustomAttribute(typeof(System.Web.Services.Protocols.SoapDocumentMethodAttribute));
                if (methodInfo.MemberType == MemberTypes.Method && attribute != null)
                {
                    methodInfoList.Add(methodInfo);
                }
            }
            return methodInfoList;
        }
    }
}

窗体FormDetailWebService的主要程序如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DetailWebServiceDemo
{
    public partial class FormDetailWebService : Form
    {
        public FormDetailWebService()
        {
            InitializeComponent();
            //可用的WebService接口列表 https://blog.csdn.net/u012408847/article/details/83027861
        }

        private void btnGetWebMethod_Click(object sender, EventArgs e)
        {
            tvAllMethods.Nodes.Clear();
            string url = txtUrl.Text.Trim();
            string className;
            try
            {
                Type typeService;
                List<MethodInfo> methodInfos = SoapUtil.GetAllWebMethodsFromService(url, out className, out typeService);
                TreeNode rootNode = new TreeNode("服务名:" + className);//以类名作为根节点
                rootNode.Tag = typeService;
                tvAllMethods.Nodes.Add(rootNode);
                for (int i = 0; i < methodInfos.Count; i++)
                {
                    MethodInfo methodInfo = methodInfos[i];
                    TreeNode treeNode = new TreeNode($"{methodInfo.Name}");
                    //设置Tag标签为方法信息
                    treeNode.Tag = methodInfo;
                    rootNode.Nodes.Add(treeNode);
                }
                tvAllMethods.ExpandAll();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "出错");
            }
        }

        private void tvAllMethods_AfterSelect(object sender, TreeViewEventArgs e)
        {
            rtxtMethodInfo.Clear();
            if (e.Node.Level == 0)
            {
                //处理根节点:服务类名
                Type typeService = e.Node.Tag as Type;
                if (typeService != null)
                {
                    System.CodeDom.Compiler.GeneratedCodeAttribute generatedCodeAttribute = typeService.GetCustomAttribute<System.CodeDom.Compiler.GeneratedCodeAttribute>();
                    //MessageBox.Show($"Tool:【{generatedCodeAttribute.Tool}】, Version:【{generatedCodeAttribute.Version}】");
                    System.Web.Services.WebServiceBindingAttribute webServiceBindingAttribute = typeService.GetCustomAttribute<System.Web.Services.WebServiceBindingAttribute>();
                    //MessageBox.Show($"Name:【{webServiceBindingAttribute.Name}】\nNamespace:【{webServiceBindingAttribute.Namespace}】\nLocation:【{webServiceBindingAttribute.Location}】");
                    rtxtMethodInfo.AppendText($"{e.Node.Text}\n");
                    rtxtMethodInfo.AppendText($"生成代码特性:Tool:【{generatedCodeAttribute.Tool}】, Version:【{generatedCodeAttribute.Version}】\n");
                    rtxtMethodInfo.AppendText($"服务绑定特性:绑定名称:【{webServiceBindingAttribute.Name}】\n命名空间:【{webServiceBindingAttribute.Namespace}】\n绑定位置:【{webServiceBindingAttribute.Location}】\n");
                }
                return;
            }
            MethodInfo methodInfo = e.Node.Tag as MethodInfo;
            if (methodInfo == null)
            {
                return;
            }
            System.Web.Services.Protocols.SoapDocumentMethodAttribute attribute = methodInfo.GetCustomAttribute(typeof(System.Web.Services.Protocols.SoapDocumentMethodAttribute)) as System.Web.Services.Protocols.SoapDocumentMethodAttribute;
            ParameterInfo[] parameterInfos = methodInfo.GetParameters();
            rtxtMethodInfo.AppendText($"{methodInfo.ToString()}\n");
            rtxtMethodInfo.AppendText($"接口方法名:【{methodInfo.Name}】\n");
            rtxtMethodInfo.AppendText($"接口返回类型:【{methodInfo.ReturnType}】\n");
            rtxtMethodInfo.AppendText($"接口参数个数:【{parameterInfos.Length}】\n");
            for (int i = 0; i < parameterInfos.Length; i++)
            {
                rtxtMethodInfo.AppendText($"  --->参数【{i + 1}】:【{parameterInfos[i].ToString()}】\n");
            }
            if (attribute != null)
            {
                rtxtMethodInfo.AppendText($"SoapDocumentMethodAttribute特性:\n");
                rtxtMethodInfo.AppendText($"  --->HTTP标头动作:【{attribute.Action}】\n");
                rtxtMethodInfo.AppendText($"  --->操作绑定:【{attribute.Binding}】\n");
                rtxtMethodInfo.AppendText($"  --->请求元素名称:【{attribute.RequestElementName}】\n");
                rtxtMethodInfo.AppendText($"  --->请求命名空间:【{attribute.RequestNamespace}】\n");
                rtxtMethodInfo.AppendText($"  --->响应元素名称:【{attribute.ResponseElementName}】\n");
                rtxtMethodInfo.AppendText($"  --->响应命名空间:【{attribute.ResponseNamespace}】\n");
                rtxtMethodInfo.AppendText($"  --->封装参数样式:【{attribute.ParameterStyle}】\n");
                rtxtMethodInfo.AppendText($"  --->绑定架构样式:【{attribute.Use}】\n");
            }
            rtxtMethodInfo.AppendText($"接口整体描述:\n{methodInfo.ReturnType} {methodInfo.Name}({string.Join(",", parameterInfos.Select(p => p.ToString()))})");
        }
    }
}

程序运行如图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

斯内科

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值