C#中异地调用WEB Service的通用类库

原创 2005年05月23日 23:05:00

下面这个库是一个关于异地调用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 result;
   }
   catch(Exception e)
   {
    Console.WriteLine(e.Message);
    return null;
   }
  }

  public string Url
  {
   get
   {
    PropertyInfo propInfo = proxyInstance.GetType().GetProperty("Url");
    object result = propInfo.GetValue(proxyInstance, null);
    
    return (string)result;
   }
   set
   {
    PropertyInfo propInfo = proxyInstance.GetType().GetProperty("Url");
    propInfo.SetValue(proxyInstance, value,
     BindingFlags.NonPublic | BindingFlags.Static |
     BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetField,
     null, null, null
     );
   }
  }


  // TODO: move the init process to an explicit method Init() ...
  public string WSDL
  {
   get
   {
    return wsdl;
   }
   set
   {
    wsdl = value;
    ResetInternalState();
    BuildProxy();
   }
  }

  public string TypeName
  {
   get
   {
    return typeName;
   }
   set
   {
    typeName = value;
   }
  }

  public string MethodName
  {
   get
   {
    return methodName;
   }
   set
   {
    methodName = value;
   }
  }

  public ProtocolEnum ProtocolName
  {
   get
   {
    switch(protocolName)
    {
     case "HttpGet":
      return ProtocolEnum.HttpGet;
     case "HttpPost":
      return ProtocolEnum.HttpPost;
     case "Soap":
      return ProtocolEnum.HttpSoap;
     default:
      return ProtocolEnum.HttpSoap;
    }
   }
   set
   {
    switch(value)
    {
     case ProtocolEnum.HttpGet:
      protocolName = "HttpGet";
      break;
     case ProtocolEnum.HttpPost:
      protocolName = "HttpPost";
      break;
     case ProtocolEnum.HttpSoap:
      protocolName = "Soap";
      break;
    }
   }
  }

  public void ClearCache(string WSDL)
  {
   // clear the cached assembly file for this WSDL
   try
   {
    string path = Path.GetTempPath();
    string newFilename = path + GetMd5Sum(WSDL) + "_eYesoft_tmp.dll";

    File.Delete(newFilename);
   }
   catch(Exception e)
   {
    e = e;
   }
  }


  private void GetWsdl(string source)
  {
   // this could be a valid WSDL representation
   if(source.StartsWith("<?xml version") == true)
   {
    wsdlSource = source;
    return; 
   }
   // this is a URL to the WSDL
   else if(source.StartsWith("http://") == true)
   {
    wsdlSource = GetWsdlFromUri(source);
    return;
   }
       
   // try to get from local file system
   wsdlSource = GetWsdlFromFile(source);
   return;
  }
  
  private string GetWsdlFromUri(string uri)
  {
   WebRequest req = WebRequest.Create(uri);
   WebResponse result = req.GetResponse();
     
   Stream ReceiveStream = result.GetResponseStream();
   Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
   StreamReader sr = new StreamReader(ReceiveStream, encode);
     
   string wsdlSource = sr.ReadToEnd();
   sr.Close();
     
   return wsdlSource;
  }
  
  private string GetWsdlFromFile(string fileFullPathName)
  {
   FileInfo fi = new FileInfo(fileFullPathName);
     
   if(fi.Extension == "wsdl")
   {
    FileStream fs = new FileStream(fileFullPathName, FileMode.Open, FileAccess.Read);
    StreamReader sr = new StreamReader(fs);
       
    char[] buffer = new char[(int)fs.Length];
    sr.ReadBlock(buffer, 0, (int)fs.Length);
    sr.Close();
       
    return new string(buffer);
   }
    
   throw new Exception("This is not a WSDL file");
  }

  private Assembly BuildAssemblyFromWsdl(string strWsdl)
  {
   // Use an XmlTextReader to get the Web Service description
   StringReader  wsdlStringReader = new StringReader(strWsdl);
   XmlTextReader tr = new XmlTextReader(wsdlStringReader);
   ServiceDescription sd = ServiceDescription.Read(tr);
   tr.Close();

   // WSDL service description importer
   CodeNamespace cns = new CodeNamespace("eYesoft.Tools.WebServices.DynamicProxy");
   sdi = new ServiceDescriptionImporter();
   //sdi.AddServiceDescription(sd, null, null);
   
   // check for optional imports in the root WSDL
   CheckForImports(wsdl);

   sdi.ProtocolName = protocolName;
   sdi.Import(cns, null);

   // change the base class
   CodeTypeDeclaration ctDecl = cns.Types[0];
   cns.Types.Remove(ctDecl);
   ctDecl.BaseTypes[0] = new CodeTypeReference("eYesoft.Tools.WebServices.SoapHttpClientProtocolEx");
   cns.Types.Add(ctDecl);

   // source code generation
   CSharpCodeProvider cscp = new CSharpCodeProvider();
   ICodeGenerator icg = cscp.CreateGenerator();
   StringBuilder srcStringBuilder = new StringBuilder();
   StringWriter sw = new StringWriter(srcStringBuilder);
   icg.GenerateCodeFromNamespace(cns, sw, null);
   proxySource = srcStringBuilder.ToString();
   sw.Close();

   // assembly compilation
   CompilerParameters cp = new CompilerParameters();
   cp.ReferencedAssemblies.Add("System.dll");
   cp.ReferencedAssemblies.Add("System.Xml.dll");
   cp.ReferencedAssemblies.Add("System.Web.Services.dll");
   cp.ReferencedAssemblies.Add("System.Data.dll");
   cp.ReferencedAssemblies.Add(System.Reflection.Assembly.
    GetExecutingAssembly().Location);
 
   cp.GenerateExecutable = false;
   cp.GenerateInMemory = false;
   cp.IncludeDebugInformation = false;

   ICodeCompiler icc = cscp.CreateCompiler();
   CompilerResults cr = icc.CompileAssemblyFromSource(cp, proxySource);
   
   if(cr.Errors.Count > 0)
    throw new Exception(string.Format(@"Build failed: {0} errors", cr.Errors.Count));

   ass = cr.CompiledAssembly;
   
   //rename temporary assembly in order to cache it for later use
   RenameTempAssembly(cr.PathToAssembly);
   
   // create proxy instance
   proxyInstance = CreateInstance(typeName);
   
   return ass;
  }
  
  private object CreateInstance(string objTypeName)
  {
   // check whether the type is already created or not
   if (objTypeName == "" || objTypeName == null)
   {
    foreach (Type ty in ProxyAssembly.GetTypes())
    {
     if(ty.BaseType == typeof(eYesoft.Tools.WebServices.SoapHttpClientProtocolEx))
     {
      objTypeName = ty.Name;
      break;
     }
    }
   }
   Type t = ass.GetType("eYesoft.Tools.WebServices.DynamicProxy." + objTypeName);
   
   return Activator.CreateInstance(t);
  }
  
  private void RenameTempAssembly(string pathToAssembly)
  {   
   string path = Path.GetDirectoryName(pathToAssembly);
   string newFilename = path + @"/" + GetMd5Sum(wsdl) + "_eYesoft_tmp.dll";
   
   File.Copy(pathToAssembly, newFilename);
  }

  private void ResetInternalState()
  {
   typeName = "";
   methodName = "";
   protocolName = "Soap";
   methodParams.Clear();
   sdi = null;
  }

  private void BuildProxy()
  {
   // inject SOAP extensions in (client side) ASMX pipeline
   InjectExtension(typeof(SoapMessageAccessClientExtension));
   
   //check cache first
   if (!CheckCache())
   {
    GetWsdl(wsdl);
    BuildAssemblyFromWsdl(wsdlSource);
   }
  }

  private bool CheckCache()
  {
   string path = Path.GetTempPath() + GetMd5Sum(wsdl) + "_eYesoft_tmp.dll";

   if(File.Exists(path))
   {
    ass = Assembly.LoadFrom(path);
    // create proxy instance
    proxyInstance = CreateInstance(typeName);

    return true;
   }
   
   return false;
  }
  
  private void CheckForImports(string baseWSDLUrl)
  {
   DiscoveryClientProtocol dcp = new DiscoveryClientProtocol();
   dcp.DiscoverAny(baseWSDLUrl);
   dcp.ResolveAll();

   foreach (object osd in dcp.Documents.Values)
   {
    if (osd is ServiceDescription) sdi.AddServiceDescription((ServiceDescription)osd, null, null);;
    if (osd is XmlSchema) sdi.Schemas.Add((XmlSchema)osd);
   }
  }

  static void InjectExtension(Type extension)
  {
   Assembly assBase;
   Type webServiceConfig;
   object currentProp;
   PropertyInfo propInfo;
   object[] value;
   Type myType;
   object[] objArray;
   object myObj;
   FieldInfo myField;

   try
   {
    assBase = typeof(SoapExtensionAttribute).Assembly;
    webServiceConfig =
     assBase.GetType("System.Web.Services.Configuration.WebServicesConfiguration");

    if (webServiceConfig == null)
     throw new Exception("Error ...");

    currentProp = webServiceConfig.GetProperty("Current",
     BindingFlags.NonPublic | BindingFlags.Static |
     BindingFlags.Instance | BindingFlags.Public
     ).GetValue(null, null);
    propInfo = webServiceConfig.GetProperty("SoapExtensionTypes",
     BindingFlags.NonPublic | BindingFlags.Static |
     BindingFlags.Instance | BindingFlags.Public
     );
    value = (object[])propInfo.GetValue(currentProp, null);
    myType = value.GetType().GetElementType();
    objArray = (object[])Array.CreateInstance(myType, (int)value.Length + 1);
    Array.Copy(value, objArray, (int)value.Length);

    myObj = Activator.CreateInstance(myType);
    myField = myType.GetField("Type",
     BindingFlags.NonPublic | BindingFlags.Static |
     BindingFlags.Instance | BindingFlags.Public
     );
    myField.SetValue(myObj, extension,
     BindingFlags.NonPublic | BindingFlags.Static |
     BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetField,
     null, null
     );
    objArray[(int)objArray.Length - 1] = myObj;
    propInfo.SetValue(currentProp, objArray,
     BindingFlags.NonPublic | BindingFlags.Static |
     BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty,
     null, null, null
     );
   }
   catch (Exception e)
   {
    e = e;
   }
  }

  private string GetMd5Sum(string str)
  {
   // First we need to convert the string into bytes, which
   // means using a text encoder
   Encoder enc = System.Text.Encoding.Unicode.GetEncoder();

   // Create a buffer large enough to hold the string
   byte[] unicodeText = new byte[str.Length * 2];
   enc.GetBytes(str.ToCharArray(), 0, str.Length, unicodeText, 0, true);

   // Now that we have a byte array we can ask the CSP to hash it
   MD5 md5 = new MD5CryptoServiceProvider();
   byte[] result = md5.ComputeHash(unicodeText);

   // Build the final string by converting each byte
   // into hex and appending it to a StringBuilder
   StringBuilder sb = new StringBuilder();
   for (int i=0;i<result.Length;i++)
   {
    sb.Append(result[i].ToString("X2"));
   }

   return sb.ToString();
  }

  public string SoapRequest
  {
   get
   {
    PropertyInfo propInfo = proxyInstance.GetType().GetProperty("SoapRequest");
    object result = propInfo.GetValue(proxyInstance, null);
    System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
    
    return enc.GetString((byte[])result);
   }
  }

  public string SoapResponse
  {
   get
   {
    PropertyInfo propInfo = proxyInstance.GetType().GetProperty("SoapResponse");
    object result = propInfo.GetValue(proxyInstance, null);
    System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
    
    return enc.GetString((byte[])result);
   }
  }

  public Assembly ProxyAssembly
  {
   get
   {
    return ass;
   }
  }

 }
}

如需要本人项目的源代码,可与本人联系!

WebService学习总结(三)——调用第三方提供的webService服务

互联网上面有很多的免费webService服务,我们可以调用这些免费的WebService服务,将一些其他网站的内容信息集成到我们的Web应用中显示,下面就以获取天气预报数据和查询国内手机号码归属地为...

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

JEECG十二个开源项目下载大全

JEECG 社区12个开源项目下载(总览)

JAVA快速开发平台 - 开源 免费 - JEECG

JEECG是一款基于代码生成器的J2EE快速开发平台,开源界“小普元”超越传统商业企业级开发平台。引领新的开发模式(Online Coding模式(自定义表单)->代码生成器模式->手工MERGE智能...

转载 C#的类库中添加Web Service引用

原文地址 http://developer.51cto.com/art/200908/143270.htm C#的类库中添加Web Service引用 2009-08-11...

在C#的类库中添加web service引用

你会在C#的类库中添加web service引用吗?      本文并不是什么高深的文章,只是VS2008应用中的一小部分,但小部分你不一定会,要不你试试:       本人对于分布式开发应用...
  • sjgfmqj
  • sjgfmqj
  • 2016年03月07日 11:08
  • 307

VB 调用C# web service

  • 2015年12月23日 13:55
  • 9KB
  • 下载

C#调用web service

  • 2010年07月30日 18:06
  • 65KB
  • 下载

C#使用Http Post方式传递Json数据字符串调用Web Service

引言   前段时间一直在做一个ERP系统,随着系统功能的完善,客户端(CS模式)变得越来越臃肿。现在想将业务逻辑层以下部分和界面层分离,使用Web Service来做。由于C#中通过直接添加引用...

C#调用 Web Service

1.1、Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:C#中异地调用WEB Service的通用类库
举报原因:
原因补充:

(最多只允许输入30个字)