模块代码:
  1 [Guid("0DDBDCCF-172D-41B4-B05C-4C4D4646B4A4")]
  2     public interface iInvokeWS
  3     {
  4         string InvokeWebService(string url, string methodname, string args);
  5          
  6     }
  7 
  8     [Guid("C11BBE0D-6792-4DA4-8E24-3E67B7DC0FC5")]
  9     [ClassInterface(ClassInterfaceType.None)]
 10     public class InvokeWS : iInvokeWS
 11     {
 12         public string InvokeWebService(string url, string methodname,string  args)
 13         {
 14             try
 15             {
 16                 if (url.Length <= 0 || methodname.Length <= 0)
 17                 {
 18                     return "Error 請提供WebService地址及MethodName!";
 19                 }
 20                 else
 21                 {
 22                     object[] arg = args.Split('^');
 23                     string sExport = "Export;" + "WebService URL:" + url + "***methodName:" + methodname + "***args:" + args;
 24                     WriteLog(sExport);
 25                     return InvokeWebMethod(url, methodname, arg);
 26                 }
 27             }
 28             catch (Exception ex)
 29             {
 30                 return ex.Message;
 31             }
 32             finally
 33             {
 34                 ClearMemory();
 35             }
 36 
 37         }
 38         private string InvokeWebMethod(string url, string methodname, object[] args)
 39         {
 40             //这里的namespace是需引用的webservices的命名空间,没有改过,也可以使用。也可以加一个参数从外面传进来。
 41             string @namespace = "client";
 42             try
 43             {
 44                 //获取WSDL
 45                 WebClient wc = new WebClient();
 46                 Stream stream = wc.OpenRead(url + "?WSDL");
 47                 ServiceDescription sd = ServiceDescription.Read(stream);
 48                 string classname = sd.Services[0].Name;
 49                 ServiceDescriptionImporter sdi = new ServiceDescriptionImporter();
 50                 sdi.AddServiceDescription(sd, "", "");
 51                 CodeNamespace cn = new CodeNamespace(@namespace);
 52 
 53                 //生成客户端代理类代码
 54                 CodeCompileUnit ccu = new CodeCompileUnit();
 55                 ccu.Namespaces.Add(cn);
 56                 sdi.Import(cn, ccu);
 57                 CSharpCodeProvider csc = new CSharpCodeProvider();
 58                 //ICodeCompiler icc = csc.CreateCompiler();
 59 
 60                 //设定编译参数
 61                 CompilerParameters cplist = new CompilerParameters();
 62                 cplist.GenerateExecutable = false;//动态编译后的程序集不生成可执行文件
 63                 cplist.GenerateInMemory = true;//动态编译后的程序集只存在于内存中,不在硬盘的文件上
 64                 cplist.ReferencedAssemblies.Add("System.dll");
 65                 cplist.ReferencedAssemblies.Add("System.XML.dll");
 66                 cplist.ReferencedAssemblies.Add("System.Web.Services.dll");
 67                 cplist.ReferencedAssemblies.Add("System.Data.dll");
 68 
 69                 //编译代理类
 70                 CompilerResults cr = csc.CompileAssemblyFromDom(cplist, ccu);
 71                 if (true == cr.Errors.HasErrors)
 72                 {
 73                     System.Text.StringBuilder sb = new System.Text.StringBuilder();
 74                     foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors)
 75                     {
 76                         sb.Append(ce.ToString());
 77                         sb.Append(System.Environment.NewLine);
 78                     }
 79 
 80                     throw new Exception(sb.ToString() + "WebService URL:" + url + "***methodName:" + methodname + "***args length:" + args.Length);
 81                 }
 82 
 83                 //生成代理实例,并调用方法
 84                 System.Reflection.Assembly assembly = cr.CompiledAssembly;
 85                 Type t = assembly.GetType(@namespace + "." + classname, true, true);
 86                 object obj = Activator.CreateInstance(t);
 87                 System.Reflection.MethodInfo mi = t.GetMethod(methodname);
 88 
 89                 //注:method.Invoke(o, null)返回的是一个Object,如果你服务端返回的是DataSet,这里也是用(DataSet)method.Invoke(o, null)转一下就行了,method.Invoke(0,null)这里的null可以传调用方法需要的参数,string[]形式的
 90                 //return mi.Invoke(obj, args).ToString();
 91                 string sReturn = mi.Invoke(obj, args).ToString();
 92                 WriteLog("Return;" + sReturn);
 93                 return sReturn;
 94 
 95             }
 96             catch (Exception ex)
 97             {
 98                 return "Error " + ex.Message.ToString() + "WebService URL:" + url + "***methodName:" + methodname + "***args length:" + args.Length;
 99             }
100         }
101 
102         private static void WriteLog(string sLog)
103         {
104             try
105             {
106                 string sDllPath = System.AppDomain.CurrentDomain.BaseDirectory;
107                 string sLogPath = sDllPath + "LOG";
108                 if (!Directory.Exists(sLogPath))
109                 {
110                     Directory.CreateDirectory(sLogPath);
111                 }
112                 string sYearPath = sLogPath + @"\" + DateTime.Now.ToString("yyyy");
113                 if (!Directory.Exists(sYearPath))
114                 {
115                     Directory.CreateDirectory(sYearPath);
116                 }
117 
118                 string sMonthPath = sYearPath + @"\" + DateTime.Now.ToString("MM");
119                 if (!Directory.Exists(sMonthPath))
120                 {
121                     Directory.CreateDirectory(sMonthPath);
122                 }
123 
124                 string sFilePath = sMonthPath + @"\" + DateTime.Now.ToString("yyyy-MM-dd") + @".txt";
125                 if (!File.Exists(sFilePath))
126                 {
127                     File.Create(sFilePath);
128                 }
129 
130                 FileStream fs = new FileStream(sFilePath, FileMode.Append, FileAccess.Write);
131                 StreamWriter sw = new StreamWriter(fs, Encoding.Unicode);
132                 sw.WriteLine("" + DateTime.Now.ToString() + "" + sLog);
133                 sw.Flush();
134                 sw.Close();
135                 fs.Close();
136             }
137             catch (Exception ex)
138             {
139 
140             }
141         }
142 
143         //內存釋放
144         [DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize")]
145         public static extern int SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize);
146         /// <summary>      
147         /// 释放内存      
148         /// </summary>      
149         public static void ClearMemory()
150         {
151             GC.Collect();
152             GC.WaitForPendingFinalizers();
153             if (Environment.OSVersion.Platform == PlatformID.Win32NT)
154             {
155                 SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
156             }
157         }
158     }
View Code

 

1.因是完整的项目中截取,故提供可能需要引入的命名空间:

using System.CodeDom;//以下几个用于根据描述动态生成代码并动态编译获取程序集
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Web.Services.Description; //WS的描述
using System.Net;
using System.Reflection;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Serialization;
using System.Runtime.InteropServices;

2.项目属性设置

2.1 应用程序->程序集信息中勾选使程序集COM可见

2.2 生成->勾选为COM互操作注册

2.3 必须添加接口声明

3. 因追寻COM组件传递资料,可手动记录传递参数详情。

 1 private static void WriteLog(string sLog)
 2     {
 3         try
 4         {
 5             string sDllPath = System.AppDomain.CurrentDomain.BaseDirectory;
 6             string sLogPath = sDllPath + "LOG";
 7             if (!Directory.Exists(sLogPath))
 8             {
 9                 Directory.CreateDirectory(sLogPath);
10             }
11             string sYearPath = sLogPath + @"\" + DateTime.Now.ToString("yyyy");
12             if (!Directory.Exists(sYearPath))
13             {
14                 Directory.CreateDirectory(sYearPath);
15             }
16 
17             string sMonthPath = sYearPath + @"\" + DateTime.Now.ToString("MM");
18             if (!Directory.Exists(sMonthPath))
19             {
20                 Directory.CreateDirectory(sMonthPath);
21             }
22 
23             string sFilePath = sMonthPath + @"\" + DateTime.Now.ToString("yyyy-MM-dd") + @".txt";
24             if (!File.Exists(sFilePath))
25             {
26                 //File.Create(sFilePath);//需手動關閉FileStream,否則提示文件被使用
27                 FileStream fsw = File.Create(sFilePath);
28                 fsw.Close();
29             }
30 
31             FileStream fs = new FileStream(sFilePath, FileMode.Append, FileAccess.Write);
32             StreamWriter sw = new StreamWriter(fs, Encoding.Unicode);
33             sw.WriteLine(DateTime.Now.ToString() + ":" + sLog);
34             sw.Flush();
35             sw.Close();
36             fs.Close();
37         }
38         catch (Exception ex)
39         {
40 
41         }
42     }
View Code