动态引用webservice

整理过后的代码

通过这个类可以将webservice提供的某个命名空间下的全部的类的实例

并可以通过invokemethod方法调用某个类的实例的方法

 

ExpandedBlockStart.gif
  1using System;
  2using System.Collections.Generic;
  3using System.Linq;
  4using System.Web;
  5using System.Xml;
  6using System.Web.Services.Description;
  7using System.CodeDom;
  8using System.CodeDom.Compiler;
  9
 10namespace Ecommerce.Web.AppCode
 11ExpandedBlockStart.gif{
 12    public class DynamicService
 13ExpandedSubBlockStart.gif    {
 14        private static string _url = string.Empty;
 15
 16        public static string Url
 17ExpandedSubBlockStart.gif        {
 18ExpandedSubBlockStart.gif            get return _url; }
 19ExpandedSubBlockStart.gif            set { _url = value; }
 20        }

 21
 22        private static string _nameSpace = string.Empty;
 23
 24ExpandedSubBlockStart.gif        /// <summary>
 25        /// Namespace of the classes
 26        /// </summary>

 27        public static string NameSpace
 28ExpandedSubBlockStart.gif        {
 29ExpandedSubBlockStart.gif            get return _nameSpace; }
 30ExpandedSubBlockStart.gif            set { _nameSpace = value; }
 31        }

 32
 33        private static IList<string> _classNames = null;
 34
 35ExpandedSubBlockStart.gif        /// <summary>
 36        /// All classes's names
 37        /// </summary>

 38        public static IList<string> ClassNames
 39ExpandedSubBlockStart.gif        {
 40ExpandedSubBlockStart.gif            get return _classNames; }
 41            set
 42ExpandedSubBlockStart.gif            {
 43                IList<string> nameList = value;
 44
 45                if (nameList == null || nameList.Count <= 0)
 46                    throw new Exception("No class name is in the list, please check!");
 47
 48                _classNames = nameList;
 49            }

 50        }

 51
 52        private static IDictionary<stringobject> classList = null;
 53
 54ExpandedSubBlockStart.gif        /// <summary>
 55        /// Get class's instances to use in webservice
 56        /// </summary>

 57        public static IDictionary<stringobject> ClassList
 58ExpandedSubBlockStart.gif        {
 59            get
 60ExpandedSubBlockStart.gif            {
 61                IDictionary<string, Object> classNameObjList = new Dictionary<string, Object>();
 62
 63                foreach (var item in _classNames)
 64ExpandedSubBlockStart.gif                {
 65                    object instance = InvokeWebservice(item);
 66                    classNameObjList.Add(item, instance);
 67                }

 68
 69                return classNameObjList;
 70            }

 71        }

 72
 73ExpandedSubBlockStart.gif        /// <summary>
 74        /// 根据指定的信息,调用远程WebService方法
 75        /// </summary>
 76        
 77        /// <param name="classname">欲调用的WebService的类名(不包括命名空间前缀)</param>
 78        /// <remarks>
 79        /// 如果调用失败,将会抛出Exception。请调用的时候,适当截获异常。
 80        /// 异常信息可能会发生在两个地方:
 81        /// 1、动态构造WebService的时候,CompileAssembly失败。
 82        /// 2、WebService本身执行失败。
 83        /// </remarks>
 84        /// <example>
 85        /// <code>
 86        /// object obj = InvokeWebservice("http://localhost/GSP_WorkflowWebservice/common.asmx","Genersoft.Platform.Service.Workflow","Common","GetToolType",new object[]{"1"});
 87        /// </code>
 88        /// </example>

 89        public static object InvokeWebservice(string classname)
 90ExpandedSubBlockStart.gif        {
 91            try
 92ExpandedSubBlockStart.gif            {
 93                System.Net.WebClient wc = new System.Net.WebClient();
 94                System.IO.Stream stream = wc.OpenRead(_url + "?WSDL");
 95                System.Web.Services.Description.ServiceDescription sd = System.Web.Services.Description.ServiceDescription.Read(stream);
 96                System.Web.Services.Description.ServiceDescriptionImporter sdi = new System.Web.Services.Description.ServiceDescriptionImporter();
 97                sdi.AddServiceDescription(sd, """");
 98                System.CodeDom.CodeNamespace cn = new System.CodeDom.CodeNamespace(_nameSpace);
 99                System.CodeDom.CodeCompileUnit ccu = new System.CodeDom.CodeCompileUnit();
100                ccu.Namespaces.Add(cn);
101                sdi.Import(cn, ccu);
102
103                Microsoft.CSharp.CSharpCodeProvider csc = new Microsoft.CSharp.CSharpCodeProvider();
104                System.CodeDom.Compiler.ICodeCompiler icc = csc.CreateCompiler();
105
106                System.CodeDom.Compiler.CompilerParameters cplist = new System.CodeDom.Compiler.CompilerParameters();
107                cplist.GenerateExecutable = false;
108                cplist.GenerateInMemory = true;
109                cplist.ReferencedAssemblies.Add("System.dll");
110                cplist.ReferencedAssemblies.Add("System.XML.dll");
111                cplist.ReferencedAssemblies.Add("System.Web.Services.dll");
112                cplist.ReferencedAssemblies.Add("System.Data.dll");
113
114                System.CodeDom.Compiler.CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu);
115                if (true == cr.Errors.HasErrors)
116ExpandedSubBlockStart.gif                {
117                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
118                    foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors)
119ExpandedSubBlockStart.gif                    {
120                        sb.Append(ce.ToString());
121                        sb.Append(System.Environment.NewLine);
122                    }

123                    throw new Exception(sb.ToString());
124                }

125                System.Reflection.Assembly assembly = cr.CompiledAssembly;
126                Type t = assembly.GetType(_nameSpace + "." + classname, truetrue);
127                object obj = Activator.CreateInstance(t);
128
129                return obj;
130            }

131            catch (Exception ex)
132ExpandedSubBlockStart.gif            {
133                throw new Exception(ex.InnerException.Message, new Exception(ex.InnerException.StackTrace));
134            }

135        }

136
137ExpandedSubBlockStart.gif        /// <summary>
138        /// Invoke the method in this class by specifying the method name
139        /// </summary>
140        /// <param name="classInstance"></param>
141        /// <param name="functionName"></param>
142        /// <param name="args"></param>
143        /// <returns></returns>

144        public static object InvokeMethod(object classInstance, string functionName,object[] args)
145ExpandedSubBlockStart.gif        {
146            System.Reflection.MethodInfo mi = classInstance.GetType().GetMethod(functionName);
147            return mi.Invoke(classInstance, args);
148        }

149    }

150}

151
欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 330987132 | Go:217696290 | Python:336880185 | 做人要厚道,转载请注明出处!http://www.cnblogs.com/sunshine-anycall/archive/2009/01/06/1370525.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值