C# 動態(tài)調(diào)用WebService的示例
更新時間:2020年11月06日 08:43:23 作者:秋荷雨翔
這篇文章主要介紹了C# 動態(tài)調(diào)用WebService的示例,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下
WebServiceHelper代碼:
using Microsoft.CSharp;
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Web.Services.Description;
using System.Xml.Serialization;
namespace SunCreate.HaiKang8600WebService.Utils
{
/// <summary>
/// 動態(tài)調(diào)用WebService
/// </summary>
public static class WebServiceHelper
{
/// <summary>
/// 動態(tài)調(diào)用WebService
/// </summary>
/// <param name="url">WebService地址</param>
/// <param name="strNamespace">命名空間</param>
/// <param name="className">類名</param>
/// <param name="methodName">方法名(模塊名)</param>
/// <param name="args">參數(shù)列表</param>
public static object InvokeWebService(string url, string strNamespace, string className, string methodName, object[] args)
{
try
{
WebClient webClient = new WebClient();
Stream stream = webClient.OpenRead(url); //獲取服務描述語言(WSDL)
ServiceDescription serviceDescription = ServiceDescription.Read(stream); //通過直接從 Stream實例加載 XML 來初始化ServiceDescription類的實例。
ServiceDescriptionImporter serviceDescriptionImporter = new ServiceDescriptionImporter();
serviceDescriptionImporter.AddServiceDescription(serviceDescription, null, null);
//生成客戶端代理類代碼
CodeNamespace codeNamespace = new CodeNamespace(strNamespace); //CodeNamespace表示命名空間聲明。
CodeCompileUnit codeCompileUnit = new CodeCompileUnit();
codeCompileUnit.Namespaces.Add(codeNamespace);
serviceDescriptionImporter.Import(codeNamespace, codeCompileUnit);
CSharpCodeProvider csharpCodeProvider = new CSharpCodeProvider();
ICodeCompiler iCodeCompiler = csharpCodeProvider.CreateCompiler();//取得C#程式碼編譯器的執(zhí)行個體
//設定編譯器的參數(shù)
CompilerParameters compilerParameters = new CompilerParameters();//創(chuàng)建編譯器的參數(shù)實例
compilerParameters.GenerateExecutable = false;
compilerParameters.GenerateInMemory = true;
compilerParameters.ReferencedAssemblies.Add("System.dll");
compilerParameters.ReferencedAssemblies.Add("System.XML.dll");
compilerParameters.ReferencedAssemblies.Add("System.Web.Services.dll");
compilerParameters.ReferencedAssemblies.Add("System.Data.dll");
//編譯代理類
CompilerResults compilerResults = iCodeCompiler.CompileAssemblyFromDom(compilerParameters, codeCompileUnit);
if (true == compilerResults.Errors.HasErrors)
{
StringBuilder sb = new StringBuilder();
foreach (CompilerError ce in compilerResults.Errors)
{
sb.Append(ce.ToString());
sb.Append(System.Environment.NewLine);
}
throw new Exception(sb.ToString());
}
//生成代理實例,并調(diào)用方法
System.Reflection.Assembly assembly = compilerResults.CompiledAssembly;
Type type = assembly.GetType(strNamespace + "." + className, true, true);
object obj = Activator.CreateInstance(type);
System.Reflection.MethodInfo methodInfo = type.GetMethod(methodName); //MethodInfo 的實例可以通過調(diào)用GetMethods或者Type對象或派生自Type的對象的GetMethod方法來獲取,還可以通過調(diào)用表示泛型方法定義的 MethodInfo 的MakeGenericMethod方法來獲取。
return methodInfo.Invoke(obj, args);
}
catch (Exception ex)
{
LogUtil.LogError(ex, "動態(tài)調(diào)用WebService 錯誤");
return null;
}
}
}
}
使用示例:
string url = "http://172.16.36.26:8080/attachment/services/AttachmentService?wsdl"; object[] args = new object[2]; args[0] = "1"; args[1] = "1"; object str = WebServiceHelper.InvokeWebService(url, "service.webservice", "AttachmentService", "checkGrade", args); string sstr = str.ToString();
以上就是C# 動態(tài)調(diào)用WebService的示例的詳細內(nèi)容,更多關于C# 動態(tài)調(diào)用WebService的資料請關注腳本之家其它相關文章!
相關文章
C#實現(xiàn)簡易灰度圖和酷炫HeatMap熱力圖winform(附DEMO)
本文主要介紹了C#實現(xiàn)簡易灰度圖和酷炫HeatMap熱力圖winform(附DEMO),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12
C#如何優(yōu)雅地取消進程的執(zhí)行之Cancellation詳解
本文介紹了.NET框架中的取消協(xié)作模型,包括CancellationToken的使用、取消請求的發(fā)送和接收、以及如何處理取消事件2024-12-12
C# 關于爬取網(wǎng)站數(shù)據(jù)遇到csrf-token的分析與解決
這篇文章主要介紹了C# 關于爬取網(wǎng)站數(shù)據(jù)遇到csrf-token的分析與解決,幫助大家更好的理解和學習c#,感興趣的朋友可以了解下2021-01-01
C#使用第三方組件實現(xiàn)動態(tài)解析和求值字符串表達式
這篇文章主要介紹了C#如何使用第三方組件(LambdaParser、DynamicExpresso、Z.Expressions)實現(xiàn)動態(tài)解析和求值字符串表達式,感興趣的小伙伴可以跟隨小編一起了解一下2022-06-06

