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

这是一个C#编写的类库,用于动态生成并调用Web Service代理,实现异地调用。该类库通过反射机制,根据WSDL文件在本地构建代理类,模仿直接引用Web Service的效果。类库包括了获取WSDL、生成代理、设置参数、调用方法等功能,并提供了多种协议选择。
摘要由CSDN通过智能技术生成

下面这个库是一个关于异地调用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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值