您的位置:首页 > 其它

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

2017-01-12 08:58 302 查看
原因:同步工具,需要实时调用第三方客户提供的接口进行解析,没有使用直接引用方式.考虑到方法后期会有变动,那样子得去更新引用.就用动态创建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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐