动态Web Service调用,解决内存回收基本问题...

原因:同步工具,需要实时调用第三方客户提供的接口进行解析,没有使用直接引用方式.考虑到方法后期会有变动,那样子得去更新引用.就用动态创建ws的代码(动态创建ws代码直接搜索出复制过来的.)).动态创建通过反射创建的代理对象,不能有效的释放内存(我的程序就是由于这原因导致内存每次请求都增加内存).所以使用了appdomian的方式进行程序隔离来请求ws,然后在卸载appdomian,来到达回收内存.(内存回收是基本达到了,但是程序的cpu使用率也上去了.暂时还找不出什么原因.)

 /// <summary>
        /// 动态调用WebService
        /// </summary>
        /// <param name="url">WebService地址</param>
        /// <param name="methodname">方法名(模块名)</param>
        /// <param name="args">参数列表</param>
        /// <returns>object</returns>
        public  object InvokeWebService(string url, string methodname, object[] args)
        {
            try
            {
                return InvokeWebService(url, null, methodname, args);
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 动态调用WebService
        /// </summary>
        /// <param name="url">WebService地址</param>
        /// <param name="classname">类名</param>
        /// <param name="methodname">方法名(模块名)</param>
        /// <param name="args">参数列表</param>
        /// <returns>object</returns>
        private  object InvokeWebService(string url, string classname, string methodname, object[] args)
        {
            try
            {
                string @namespace = "SyncTools.WebService.DynamicWebLoad";
                if (classname == null || classname == "")
                {
                    classname = GetClassName(url);
                }
                classname = @namespace + "." + classname;
                // Construct and initialize settings for a second AppDomain.
                AppDomainSetup appDomainSetup = new AppDomainSetup
                {
                    ApplicationBase = System.Environment.CurrentDirectory,
                    DisallowBindingRedirects = false,
                    DisallowCodeDownload = false,
                    ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
                };
                // Create the second AppDomain.
                // AppDomain appDomain = AppDomain.CreateDomain("SyncTools.WebService.DynamicWebLoad", null, appDomainSetup);
                AppDomain appDomain = AppDomain.CreateDomain("SyncTools.WebService.DynamicWebLoad");
                Type t = typeof(ProxyObject);
                ProxyObject obj = (ProxyObject)appDomain.CreateInstanceAndUnwrap(t.Assembly.FullName, t.FullName);
                obj.LoadAssembly(url, @namespace);
                obj.Invoke(classname, methodname, args);
                var result = obj.Resutl;
                AppDomain.Unload(appDomain);
                appDomain = null;
                obj = null;
                return result;              
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                GC.Collect();
            }
        }

        private  string GetClassName(string url)
        {
            //URL为"http://localhost/InvokeService/Service1.asmx"
            //返回值为 Service1
            string[] parts = url.Split('/');
        string[] pps = parts[parts.Length - 1].Split('.');
        return pps[0];
        }


这段代码加载ws接口,通过相关参数的传递获取接口的返回结果值(Result).

internal class ProxyObject : MarshalByRefObject
    {
        Assembly assembly = null;
        private Object result = null;
        public Object Resutl
        {
            get { return this.result; }
        }

        public void LoadAssembly(string url, string @namespace)
        {
            // assembly = Assembly.LoadFile(@"TestDLL.dll");
            //获取服务描述语言(WSDL)
            using (WebClient wc = new WebClient())
            {
                using (Stream stream = wc.OpenRead(url + "?WSDL")) 
                {
                    ServiceDescription sd = ServiceDescription.Read(stream);
                    ServiceDescriptionImporter sdi = new ServiceDescriptionImporter();
                    sdi.AddServiceDescription(sd, "", "");
                    CodeNamespace cn = new CodeNamespace(@namespace);
                    //生成客户端代理类代码
                    CodeCompileUnit ccu = new CodeCompileUnit();
                    ccu.Namespaces.Add(cn);
                    sdi.Import(cn, ccu);
                    CSharpCodeProvider csc = new CSharpCodeProvider();
                    ICodeCompiler icc = csc.CreateCompiler();
                    //设定编译器的参数
                    CompilerParameters cplist = new CompilerParameters();
                    cplist.GenerateExecutable = false;
                    cplist.GenerateInMemory = false;
                    cplist.ReferencedAssemblies.Add("System.dll");
                    cplist.ReferencedAssemblies.Add("System.XML.dll");
                    cplist.ReferencedAssemblies.Add("System.Web.Services.dll");
                    cplist.ReferencedAssemblies.Add("System.Data.dll");
                    //编译代理类
                    CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu);
                    if (true == cr.Errors.HasErrors)
                    {
                        System.Text.StringBuilder sb = new StringBuilder();
                        foreach (CompilerError ce in cr.Errors)
                        {
                            sb.Append(ce.ToString());
                            sb.Append(System.Environment.NewLine);
                        }
                        throw new Exception(sb.ToString());
                    }
                    //生成代理实例,并调用方法
                    assembly = cr.CompiledAssembly;
                }
            }
        }

        public bool Invoke(string className, string methodName, params Object[] args)
        {
            if (assembly == null)
                return false;
            Type tp = assembly.GetType(className, true, true);
            if (tp == null)
                return false;
            MethodInfo method = tp.GetMethod(methodName);
            if (method == null)
                return false;
            Object obj = Activator.CreateInstance(tp);
            result = method.Invoke(obj, args);
            return true;
        }

内存回收

 #region 内存回收
        //[DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize")]
        [DllImportAttribute("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
        public static extern int SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize);
        /// <summary>
        /// 释放内存
        /// </summary>
        public static void ClearMemory()
        {
            SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
        }
        #endregion


评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值