下面这个库是一个关于异地调用WEB Service的通用类库,它主要利用C#中Reflection的机制,通过获取远程服务器的WSDL文件,在本地生成一个代理类,获得通过在本机上引用Web Service生成代理类的相同效果。
本类库从www.codeproject.com上下载,笔者曾在一个实验项目上应用过,觉得很不错,在此与大家共享:
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Collections;
using System.Web.Services.Description;
using System.Web.Services.Discovery;
using System.Web.Services.Protocols;
using Microsoft.CSharp;
using System.IO;
using System.Reflection;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using System.Text;
using System.Net;
using System.Security.Cryptography;
// DynamicWebServiceProxy - This sample is provided as is!
// Please notice that this code is only a technology demo
// and should not be included unedited und untested into any serious project.
// -------------------------------------------------------------------------------------
// The usage of the library is shown in the WSLibTester project in the same download file.
//
// (C) Christian Weyer, 2002-2003. All rights reserved.
namespace eYesoft.Tools.WebServices
{
public enum ProtocolEnum
{
HttpGet,
HttpPost,
HttpSoap
}
// For feedback/questions/comments: cw@eyesoft.de
public class DynamicWebServiceProxy
{
private Assembly ass = null;
private object proxyInstance = null;
private string wsdl;
private string wsdlSource;
private string typeName;
private string methodName;
private string protocolName = "Soap";
private ArrayList methodParams = new ArrayList();
private string proxySource;
private ServiceDescriptionImporter sdi;
public DynamicWebServiceProxy()
{
}
public DynamicWebServiceProxy(string wsdlLocation)
{
wsdl = wsdlLocation;
BuildProxy();
}
public DynamicWebServiceProxy(string wsdlLocation, string inTypeName)
{
wsdl = wsdlLocation;
typeName = inTypeName;
BuildProxy();
}
public DynamicWebServiceProxy(string wsdlLocation, string inTypeName, string inMethodName)
{
wsdl = wsdlLocation;
typeName = inTypeName;
methodName = inMethodName;
BuildProxy();
}
public DynamicWebServiceProxy(string wsdlLocation, string inTypeName, string inMethodName, params object[] inMethodParams)
{
wsdl = wsdlLocation;
typeName = inTypeName;
methodName = inMethodName;
methodParams = null;
methodParams = new ArrayList(inMethodParams);
BuildProxy();
}
public void AddParameter(object param)
{
methodParams.Add(param);
}
public object InvokeCall()
{
try
{
MethodInfo mi = proxyInstance.GetType().GetMethod(methodName);
object result = mi.Invoke(proxyInstance, (object[])methodParams.ToArray(typeof(object)));
return resul