找传奇、传世资源到传世资源站!

动态调用WebService

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

动态调用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;
            }
        }

    }
}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复