在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → 动态调用WebService

动态调用WebService

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:0.13M
  • 下载次数:142
  • 浏览次数:808
  • 发布时间:2014-03-30
  • 实例类别:C#语言基础
  • 发 布 人:13360742
  • 文件格式:.rar
  • 所需积分:2
 相关标签: webservice

实例介绍

【实例简介】动态调用WebService
【实例截图】

【核心代码】

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Web.Services.Description;
using System.Xml.Serialization;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Reflection;

namespace ClassLibrary1
{
    /// <summary>
    ///动态调用WebService
    /// </summary>
    public class DynamicCallWebService
    {
        /// <summary>
        /// 动态调用WebService,并自动更新WebService
        /// </summary>
        /// <param name="WebServiceURL">WebService地址(必填项)如:http://10.192.56.81:8085/TestWebService.asmx,注意:TestWebService有大小写之分</param>
        /// <param name="AgentClassName">代理类的名称,可以为空</param>
        /// <param name="MethodName">需要调用的方法名(必填项)</param>
        /// <param name="Parameters">调用方法的时候的参数列表,可以为空</param>
        /// <param name="Timeout">调用WebService超时设置,单位为毫秒,可以为空,默认为60000,即为1分钟</param>
        /// <param name="DllPath">代理类生成的程序集的路径(必填项)</param>
        /// <param name="ErrorMsg">错误消息</param>
        /// <returns></returns>
        public static object CallWebService(string WebServiceURL, string AgentClassName, string MethodName, object[] Parameters, string Timeout, string DllPath, out string ErrorMsg)
        {
            object Result = null;
            ErrorMsg = "";
            try
            {

                if (WebServiceURL == null || WebServiceURL == "")
                {
                    ErrorMsg = "WebServiceURL不能为空!";
                    return null;
                }
                if (MethodName == null || MethodName == "")
                {
                    ErrorMsg = "MethodName不能为空!";
                    return null;
                }
                //if (DllName == null || DllName == "")
                //{
                //    ErrorMsg = "DllName不能为空!";
                //    return null;
                //}
                if (DllPath == null || DllPath == "")
                {
                    ErrorMsg = "DllPath不能为空!";
                    return null;
                }

                if (Timeout == null || Timeout == "")
                {
                    Timeout = "60000";//默认为1分钟
                }


                WebServiceURL = WebServiceURL.Trim();
                string TempStr = WebServiceURL.Substring(WebServiceURL.Length - 5).Trim().ToLower();
                if (TempStr != "?wsdl")
                {
                    WebServiceURL = WebServiceURL "?wsdl";
                }

                //if (DllName == null || DllName == "")
                //{
                //    DllName = "WebService_DynamicCall.dll";
                //}
                if (DllPath.Substring(DllPath.Length - 2) != @"\")
                {
                    DllPath = DllPath @"\WebServiceDllFolder\";
                }
                else
                {
                    DllPath = DllPath @"WebServiceDllFolder\";
                }

                string DllName = "";
                DllName = GetDllName(DllPath);
                if (DllName == "")
                {
                    DllName = "WebService_DynamicCall_1.dll";
                }

                if (File.Exists(DllPath DllName) == true)//此代理类生成的Dll是否存在?
                {
                    Assembly asm = Assembly.LoadFrom(DllPath DllName);//加载前面生成的程序集
                    Type[] ClassArray = asm.GetTypes();//获取此dll下的所有类
                    if (AgentClassName == null || AgentClassName == "")
                    {
                        AgentClassName = ClassArray[0].Name;//一般第一个类就是此WebSrvice的类名
                    }
                    else
                    {
                        bool Flag = false;
                        for (int i = 0; i < ClassArray.Length; i )
                        {
                            if (ClassArray[i].Name.ToLower() == AgentClassName.Trim().ToLower())
                            {
                                Flag = true;
                                break;
                            }
                        }
                        if (Flag == false)
                        {
                            ErrorMsg = DllName "下没有此类: " AgentClassName;
                            return null;
                        }
                    }

                    Result = ExecMethod(asm, WebServiceURL, AgentClassName, MethodName, Parameters, Convert.ToInt32(Timeout), DllName, DllPath, true, out ErrorMsg);
                }
                else//第一次调用,得生成代理类
                {
                    OutputAssembly(WebServiceURL, Convert.ToInt32(Timeout), DllName, DllPath, out ErrorMsg);
                    Assembly asm = Assembly.LoadFrom(DllPath DllName);//加载前面生成的程序集
                    Type[] ClassArray = asm.GetTypes();//获取此dll下的所有类
                    if (AgentClassName == null || AgentClassName == "")
                    {
                        AgentClassName = ClassArray[0].Name;//一般第一个类就是此WebSrvice的类名
                    }
                    Result = ExecMethod(asm, WebServiceURL, AgentClassName, MethodName, Parameters, Convert.ToInt32(Timeout), DllName, DllPath, false, out ErrorMsg);
                }

            }
            catch (Exception ex)
            {
                ErrorMsg = ex.Message "; " ex.StackTrace;
            }


            return Result;
        }


        private static string GetDllName(string DllPath)
        {
            string DllName = "";
            System.Collections.Hashtable ht = new System.Collections.Hashtable();
            List<int> DllNum = new List<int>();
            if (System.IO.Directory.Exists(DllPath) == false)
            {
                System.IO.Directory.CreateDirectory(DllPath);
            }

            string[] Files = System.IO.Directory.GetFiles(DllPath);
            for (int i = 0; i < Files.Length; i )
            {
                if (Files[i].ToLower().IndexOf("webservice_dynamiccall") != -1)
                {
                    string TempStr = Files[i].Substring(Files[i].LastIndexOf("_") 1);
                    TempStr = TempStr.Substring(0, TempStr.LastIndexOf(".dll"));
                    int temp = Convert.ToInt32(TempStr);
                    DllNum.Add(temp);
                    ht.Add(temp, Files[i].Substring(Files[i].LastIndexOf("\\") 1));
                }
            }
            if (DllNum.Count != 0)
            {
                DllNum.Sort();
                DllName = ht[DllNum[DllNum.Count - 1]].ToString();//取出最新的Dll
            }

            return DllName;
        }


        private static object ExecMethod(Assembly asm, string WebServiceURL, string AgentClassName, string MethodName, object[] Parameters, int Timeout, string DllName, string DllPath, bool IsUpdWebService, out string ErrorMsg)
        {
            ErrorMsg = "";
            object Result = null;
            Type t = asm.GetType("mydynamiccallwebservice." AgentClassName);

            object o = Activator.CreateInstance(t);
            MethodInfo method = t.GetMethod(MethodName);//GetPersons是服务端的方法名称

            if (method == null)
            {
                if (IsUpdWebService == true)//动态更新WebService
                {

                    //  File.Delete(DllPath);//先删除此dll
                    string TempStr = DllName.Substring(DllName.LastIndexOf("_") 1);
                    TempStr = TempStr.Substring(0, TempStr.LastIndexOf(".dll"));
                    int DllMaxNum = Convert.ToInt32(TempStr);

                    //给10次机会生成dll,如果还是调用不到的话,那么就代表这“方法名”在WebService里面已去除了
                    if (DllMaxNum==10)
                    {
                        ErrorMsg = "没有此方法:" method ",请重新设置方法名;注意:请把此文件夹:" DllPath " 下面自动生成dll都删除,如果删除不了,先停了这些dll所在的宿主(比如IIS,或者当前的应用程序等)再删除此dll,不然有可能是被这宿主占用了资源删除不了!";
                        return null;
                    }

                    DllName = "webservice_dynamiccall_" ( DllMaxNum).ToString() ".dll";//原来的那个dll没法删除,因为占用了资源,没法释放资源,只有垃圾回收站释放资源了,但是又得要更新些Dll,所以重新生成另一个新的dll


                    //如果method为null,那么只有两种可能:1.此类下没有此方法  2.服务器那边WebSrvice添加了新的方法,此代理类没有更新
                    //所以这里处理是:第一次method为null,那么给一次机会,用下面的方法"OutputAssembly"重新更新下WebService,并重新生成dll文件
                    OutputAssembly(WebServiceURL, Timeout, DllName, DllPath, out ErrorMsg);
                    if (ErrorMsg != "")//代表出错了
                    {
                        return null;
                    }

                    //重新加载新的dll
                    asm = Assembly.LoadFrom(DllPath DllName);
                    t = asm.GetType("mydynamiccallwebservice." AgentClassName);
                    o = Activator.CreateInstance(t);
                    method = t.GetMethod(MethodName);

                }


                //如果method还为null,那么则"MethodName"则真是错误了
                if (method == null)
                {
                    ErrorMsg = "没有此方法: " MethodName;
                    return null;
                }

            }

            try
            {
                //注:method.Invoke(o, null)返回的是一个Object,如果你服务端返回的是DataSet,这里也是用(DataSet)method.Invoke(o, null)转一下就行了
                Result = method.Invoke(o, Parameters);
            }
            catch (Exception)
            {
                ErrorMsg = "参数有错误:Parameters";
            }
            return Result;
        }


        private static void OutputAssembly(string WebServiceURL, int Timeout, string DllName, string DllPath, out string ErrorMsg)
        {
            ErrorMsg = "";
            try
            {

                HttpWebRequest client = (HttpWebRequest)HttpWebRequest.Create(WebServiceURL);
                client.Timeout = Timeout;
                Stream stream = ((HttpWebResponse)client.GetResponse()).GetResponseStream();

                ServiceDescription description = ServiceDescription.Read(stream);

                ServiceDescriptionImporter importer = new ServiceDescriptionImporter();//创建客户端代理代理类。

                importer.ProtocolName = "Soap"; //指定访问协议。
                importer.Style = ServiceDescriptionImportStyle.Client; //生成客户端代理。
                importer.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties | CodeGenerationOptions.GenerateNewAsync;

                importer.AddServiceDescription(description, null, null); //添加WSDL文档。

                CodeNamespace nmspace = new CodeNamespace(); //命名空间
                nmspace.Name = "mydynamiccallwebservice";
                CodeCompileUnit unit = new CodeCompileUnit();
                unit.Namespaces.Add(nmspace);

                ServiceDescriptionImportWarnings warning = importer.Import(nmspace, unit);
                CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");

                CompilerParameters parameter = new CompilerParameters();
                parameter.GenerateExecutable = false;
                parameter.OutputAssembly = DllName;//输出程序集的名称
                parameter.ReferencedAssemblies.Add("System.dll");
                parameter.ReferencedAssemblies.Add("System.XML.dll");
                parameter.ReferencedAssemblies.Add("System.Web.Services.dll");
                parameter.ReferencedAssemblies.Add("System.Data.dll");

                CompilerResults result = provider.CompileAssemblyFromDom(parameter, unit);
                if (result.Errors.HasErrors)
                {
                    // 显示编译错误信息
                    ErrorMsg = "动态编绎:\"" DllName "\"有错误!";
                }
                File.Copy(AppDomain.CurrentDomain.BaseDirectory DllName, DllPath DllName);
                File.Delete(AppDomain.CurrentDomain.BaseDirectory DllName);
            }
            catch (Exception ex)
            {

                ErrorMsg = "创建代理类并动态生成Dll出错:" ex.Message "; " ex.StackTrace;
            }
        }



    }
}

标签: webservice

实例下载地址

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警