惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
G
Google Developers Blog
Blog — PlanetScale
Blog — PlanetScale
U
Unit 42
A
About on SuperTechFans
Vercel News
Vercel News
B
Blog
Martin Fowler
Martin Fowler
MyScale Blog
MyScale Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
腾讯CDC
D
Docker
V
Visual Studio Blog
博客园 - 叶小钗
The Cloudflare Blog
Jina AI
Jina AI
B
Blog RSS Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
WordPress大学
WordPress大学
T
Tailwind CSS Blog
MongoDB | Blog
MongoDB | Blog
D
DataBreaches.Net
月光博客
月光博客
大猫的无限游戏
大猫的无限游戏

博客园 - habin

利用KindEditor实现公司通讯录的维护 在winform嵌入外部应用程序 Devexpress RichEditControl 导入word文件后字体变为方正姚体的解决方案 Js下载文件到本地(兼容多浏览器) 解决lhgDialog插件在IE11浏览器的BUG Devexpress ChartControl 柱状图简单例子 解决“远程会话已断开连接,因为访问被拒绝导致许可证存储的创建失败,请使用提升的权限运行远程桌面客户端”问题 MVC项目初次发布到IIS可能会遇到的问题 Android Studio下载及离线升级方法 统计某个单词出现次数 不能在 Page 回调中调用 Response.Redirect 解决方法 JQuery TextExt 控件使用 替换文本框title提示文本 通过ashx获取JSON数据的两种方式 终于解决SQL Server 2008 64位系统无法导入Access/Excel的问题 2012/08/01 Asp.net项目部署ActiveReport jQuery Mobile对话框插件 JS实现日期比较 Apache根目录运行.net网站
动态调用WebService
habin · 2014-02-25 · via 博客园 - habin
public class ServiceProxy {

		#region InvokeWebService
		[SecurityPermissionAttribute(SecurityAction.Demand, Unrestricted = true)]
		internal static object CallWebService(string webServiceAsmxUrl, string serviceName, string methodName, object[] args) {

			var client = new WebClient();

			// Connect To the web service
			var stream = client.OpenRead(webServiceAsmxUrl + "?wsdl");
			if (stream == null) return null;
			// Now read the WSDL file describing a service.
			var description = ServiceDescription.Read(stream);

			///// LOAD THE DOM /////////
			// Initialize a service description importer.
			var importer = new ServiceDescriptionImporter {ProtocolName = "Soap12"};

			importer.AddServiceDescription(description, null, null);

			// Generate a proxy client.
			importer.Style = ServiceDescriptionImportStyle.Client;

			// Generate properties to represent primitive values.
			importer.CodeGenerationOptions = System.Xml.Serialization.CodeGenerationOptions.GenerateProperties;

			// Initialize a Code-DOM tree into which we will import the service.
			var nmspace = new CodeNamespace();

			var unit1 = new CodeCompileUnit();

			unit1.Namespaces.Add(nmspace);

			// Import the service into the Code-DOM tree. This creates proxy code that uses the service.
			ServiceDescriptionImportWarnings warning = importer.Import(nmspace, unit1);

			if (warning == 0) // If zero then we are good to go

{

				// Generate the proxy code
				CodeDomProvider provider1 = CodeDomProvider.CreateProvider("CSharp");

				// Compile the assembly proxy with the appropriate references
				var assemblyReferences = new string[5] { "System.dll", "System.Web.Services.dll", "System.Web.dll", "System.Xml.dll", "System.Data.dll" };

				var parms = new CompilerParameters(assemblyReferences);

				var results = provider1.CompileAssemblyFromDom(parms, unit1);

				// Check For Errors

				if (results.Errors.Count > 0) {

					foreach (CompilerError oops in results.Errors) {

						System.Diagnostics.Debug.WriteLine("========Compiler error============");

						System.Diagnostics.Debug.WriteLine(oops.ErrorText);

					}

					throw new System.Exception("Compile Error Occured calling webservice. Check Debug ouput window.");

				}

				// Finally, Invoke the web service method
				var wsvcClass = results.CompiledAssembly.CreateInstance(serviceName);
				if (wsvcClass == null) return null;
				var mi = wsvcClass.GetType().GetMethod(methodName);

				return mi.Invoke(wsvcClass, args);

			}

			else {

				return null;

			}

		}
		#endregion

		public static object CallWebService(string webServiceAsmxUrl, string serviceName)
		{
			throw new System.NotImplementedException();
		}
	}
            //调用
            var url = "www.myweb.com/service1.asmx";
            var args = new object[2];
            args[0] = "参数1";
            args[1] = "参数2";

            var result = ServiceProxy.CallWebService(url,"服务名称", "方法名称", args);
            
            return result.ToString() ;//Web服务中方法的返回值