【超细完整版】C# WebService 通过URL生成WSDL文件和DLL文件 【生成篇】

2024-07-16 1444阅读

先学生成,再看调用哦

【超细完整版】C# WebService 通过URL生成WSDL文件和DLL文件 【生成篇】
(图片来源网络,侵删)

【超细完整版】C# 获取WebService所有方法并调用 【调用篇】

目的

支持通过web url (自适应“?wsdl”的有无) 生成.wsdl文件 和 .dll文件

实现

将通过一个类的三部分来实现这些功能

  • 获取url中的ClassName (GetClassNameFromUrl)
  • 转换为WSDL文件 (UrlToWsdlFile)
  • 转换为DLL文件 (UrlToDllFile)

    创建一个新类

    类名为 WebServiceHelper.cs

        /// 
        /// 动态调用WebService(支持SaopHeader)
        /// 
        public class WebServiceHelper
        {
        
        }
    

    并在该类实现下述方法

    获取url中的ClassName

    #region 获取url中的ClassName
    ///    
    /// 获取WebService的类名   
    ///    
    /// WebService地址   
    /// 返回WebService的类名   
    public static string GetClassNameFromUrl(string wsUrl)
    {
        string result = string.Empty;
        if (!wsUrl.EndsWith("?wsdl", StringComparison.CurrentCultureIgnoreCase))
        {
            wsUrl = wsUrl + "?wsdl";
        }
        XmlDocument doc = new XmlDocument();
        doc.Load(wsUrl);
        try
        {
            result = doc.GetElementsByTagName("wsdl:service")[0].Attributes[0].Value;
        }
        catch (Exception err)
        {
            return string.Empty;
        }
        return result;
    }
    #endregion
    

    转换为WSDL文件

     #region 生成WSDL
     public static void UrlToWsdlFile(string url, string savePath, string outName = "")
     {
         string className = string.Empty;
         string FullFileName = string.Empty;
         className = GetClassNameFromUrl(url);
         if (outName == "")
         {
             outName = className + ".wsdl";
         }
         else
         {
             if (!outName.EndsWith(".wsdl", StringComparison.CurrentCultureIgnoreCase))
             {
                 outName = outName + ".wsdl";
             }
         }
         if (!url.EndsWith("?wsdl", StringComparison.CurrentCultureIgnoreCase))
         {
             url = url + "?wsdl";
         }
         FullFileName = savePath + "\\" + outName;
         WebClient wc = new WebClient();
         if (!System.IO.Directory.Exists(savePath))
         {
             System.IO.Directory.CreateDirectory(savePath);//不存在就创建文件夹
         }
         wc.DownloadFile(url, FullFileName);
     }
     #endregion
    

    转换为DLL文件

    #region 生成DLL
    public static CompilerResults UrlToDllFile(string url, string @namespace = "")
    {
        string className = string.Empty;
        className = GetClassNameFromUrl(url);
        if (!url.EndsWith("?wsdl", StringComparison.CurrentCultureIgnoreCase))
        {
            url = url + "?wsdl";
        }
        WebClient web = new WebClient();
        Stream stream = web.OpenRead(url);
        //创建和格式化 WSDL 文档。       
        ServiceDescription description = ServiceDescription.Read(stream);
        CompilerResults compiler = CreatDll(className, description, @namespace);
        return compiler;
    }
    private static CompilerResults CreatDll(string className, ServiceDescription description, string @namespace = "")
    {
        try
        {
            // 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();
            nmspace.Name = @namespace;
            CodeCompileUnit unit = new CodeCompileUnit();
            unit.Namespaces.Add(nmspace);
            ServiceDescriptionImportWarnings warning = importer.Import(nmspace, unit);
            CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
            CompilerParameters parameter = new CompilerParameters();
            parameter.GenerateExecutable = false;
            parameter.GenerateInMemory = true;//在内存中生成输出
            // 可以指定你所需的任何文件名。
            parameter.OutputAssembly = AppDomain.CurrentDomain.BaseDirectory + "dll\\" + 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);
            if (result.Errors.HasErrors)
            {
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                foreach (System.CodeDom.Compiler.CompilerError ce in result.Errors)
                {
                    sb.Append(ce.ToString());
                    sb.Append(System.Environment.NewLine);
                }
                throw new Exception(sb.ToString());
            }
            return result;
        }
        catch (Exception err)
        {
            MessageBox.Show(err.Message, "Error");
            return null;
        }
    }
    #endregion
    

    应用

    个人示例,实际根据自己需求调整 ; 以下为窗体按钮事件

    private void bt_generate_dll_Click(object sender, EventArgs e)
    {
        try
        {
            WebServiceHelper.UrlToDllFile(tb_webLink.Text);
            if (MessageBox.Show("The dll is generated successfully. Do you want to open the file path?", "notice", MessageBoxButtons.YesNo) == DialogResult.Yes)
                openPath("dll");
        }
        catch (Exception err)
        {
            MessageBox.Show(err.Message, "Error");
        }
    }
    private void openPath(string type)
    {
        if (string.IsNullOrEmpty(type)) return;
        string key = string.Empty;
        key = type.Equals(defaultKey) ? wsdlPathKey : dllPathKey;
        //get Configuration object
        Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        //get value by key
        string path = config.AppSettings.Settings[key].Value;
        System.Diagnostics.Process.Start("explorer.exe", path);
    }
    

    老规矩,点赞关注走一波 😄

VPS购买请点击我

免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

目录[+]