【超细完整版】C# WebService 通过URL生成WSDL文件和DLL文件 【生成篇】
创始人
2024-11-19 05:36:21

先学生成,再看调用哦
【超细完整版】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); }  

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

相关内容

热门资讯

刚刚,Claude最新功能泄露... 新智元报道 编辑:定慧 大卫 【新智元导读】2026年5月4日,testingcatalog在An...
高分辨大宽带集成光子光谱仪成功... 麦姆斯咨询获悉,近日,中国科学院南京天文光学技术研究所天文光子学团队在面向天文观测的高分辨大宽带集成...
性价比高又稳定的云手机哪个好?... 作为搬了4年砖、踩过无数云手机坑的老玩家,今天直接给你们唠唠性价比高又稳定的云手机选法,全是实战干货...
以灵石破局,万物云参编国内首部... 4月23日,由低碳智慧建筑产业技术创新战略联盟与北京清华同衡规划设计研究院有限公司主办、万物云作为协...
专访 | CLA成功反哺全球 ... 2026年,是奔驰诞生的140周年,也是奔驰进入中国内地市场的20周年。 140年间,从第一款汽车问...