Asp.Net后台调用Web Service的三种方式

一、通过服务引用进行调用

         首先在项目中对要使用的Web Service地址进行服务引用后,再直接对类进行实例化,最后调用要使用的方法即可

public object GetWebService2(string txtCity, string EndCity, string Date, string UserID)     {         object obj = new object();         ServiceReference.DomesticAirlineSoapClient client = new ServiceReference.DomesticAirlineSoapClient();         obj = client.getDomesticAirlinesTime(txtCity, EndCity, Date, UserID);         return obj;     }

 二、使用WebClient下载WSDL信息,使用代理类和反射完成调用

public object GetWebService(string txtCity, string EndCity, string Date, string UserID)     {         object obj = new object();         // 1. 使用 WebClient 下载 WSDL 信息。         WebClient web = new WebClient();         Stream stream = web.OpenRead("http://www.webxml.com.cn/webservices/DomesticAirline.asmx?WSDL");          // 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;          importer.AddServiceDescription(description, null, null); // 添加 WSDL 文档。          // 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");          CompilerParameters parameter = new CompilerParameters();         parameter.GenerateExecutable = false;         parameter.OutputAssembly = "DomesticAirline.dll";         //parameter.GenerateInMemory = true;         parameter.ReferencedAssemblies.Add("System.dll");         parameter.ReferencedAssemblies.Add("System.XML.dll");         parameter.ReferencedAssemblies.Add("System.Web.Services.dll");         parameter.ReferencedAssemblies.Add("System.Data.dll");          CompilerResults result = provider.CompileAssemblyFromDom(parameter, unit);          // 5. 使用 Reflection 调用 WebService。         if (!result.Errors.HasErrors)         {             Assembly asm = Assembly.LoadFile(Path.GetFullPath("DomesticAirline.dll"));  //result.CompiledAssembly;             Type t = asm.GetType("DomesticAirline"); // 如果在前面为代理类添加了命名空间,此处需要将命名空间添加到类型前面。              object o = Activator.CreateInstance(t);             MethodInfo method = t.GetMethod("getDomesticAirlinesTime", new Type[] { typeof(string), typeof(string), typeof(string), typeof(string) });             obj = method.Invoke(o, new object[] { txtCity, EndCity, Date, UserID });         }         return obj;     }

三、通过将下载下来的内容编译成dll存储在本地,再通过反射进行调用

public object GetWebService3(string txtCity, string EndCity, string Date, string UserID)     {         object obj = new object();         // 1. 使用 WebClient 下载 WSDL 信息。         WebClient web = new WebClient();         Stream stream = web.OpenRead("http://www.webxml.com.cn/webservices/DomesticAirline.asmx?WSDL");          // 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;          importer.AddServiceDescription(description, null, null); // 添加 WSDL 文档。          // 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");          CompilerParameters parameter = new CompilerParameters();         parameter.GenerateExecutable = false;         parameter.GenerateInMemory = true;         parameter.ReferencedAssemblies.Add("System.dll");         parameter.ReferencedAssemblies.Add("System.XML.dll");         parameter.ReferencedAssemblies.Add("System.Web.Services.dll");         parameter.ReferencedAssemblies.Add("System.Data.dll");          CompilerResults result = provider.CompileAssemblyFromDom(parameter, unit);          // 5. 使用 Reflection 调用 WebService。         if (!result.Errors.HasErrors)         {             Assembly asm = result.CompiledAssembly;  //result.CompiledAssembly;             Type t = asm.GetType("DomesticAirline"); // 如果在前面为代理类添加了命名空间,此处需要将命名空间添加到类型前面。              object o = Activator.CreateInstance(t);             MethodInfo method = t.GetMethod("getDomesticAirlinesTime", new Type[] { typeof(string), typeof(string), typeof(string), typeof(string) });             obj = method.Invoke(o, new object[] { txtCity, EndCity, Date, UserID });         }         return obj;     }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ASP.NET后台模板是一种用于开发Web应用程序的模板引擎,它能够帮助开发人员快速构建动态网页。这种模板引擎使用ASP.NET编程语言,如C#或VB.NET,并且使用标记、控件和代码来生成HTML输出。 ASP.NET后台模板的一个主要优势是它与服务器端代码的紧密集成。使用后台模板,开发人员可以在网页中嵌入与服务器端代码相结合的HTML和其他标记,从而使网页能够以动态方式生成。例如,可以在模板中使用循环、条件语句和变量,以根据需求动态创建页面内容。 此外,ASP.NET后台模板还提供了许多可以提高开发效率的功能。它支持数据绑定,使开发人员能够将数据源(如数据库、XML文件或对象集合)与模板进行绑定,从而自动将数据填充到页面中。它还提供了强大的控件库,包括按钮、文本框、下拉列表等控件,使开发人员能够轻松地创建交互式网页。 另一个重要的特点是ASP.NET后台模板的可重用性。开发人员可以创建模板文件,并在需要时在不同的页面中重复使用。这样可以大大提高代码的可维护性,并节省开发时间。此外,模板还支持继承和部分视图,使开发人员可以将页面分解为多个可重用的部分,并在需要时进行组合。 总结而言,ASP.NET后台模板是一种用于创建动态网页的强大工具。它提供了与服务器端代码的紧密集成、数据绑定、丰富的控件库和可重用性等功能,使开发人员能够更快速、高效地开发出功能丰富、可维护的Web应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值