我有一个应用程序加载DLL来执行特定的处理部分
Example : "Application.dll" loading "Process.dll"
Run Code Online (Sandbox Code Playgroud)
Process.dll在运行时使用反射动态加载,而不是在应用程序中引用.
处理完成后,需要在服务器上重新编译DLL并稍后再次加载.
为了做到这一点,我需要释放它,否则我收到以下消息:"无法将文件"Process.dll"复制到"Process.dll".进程无法访问文件'Process.dll',因为它是被另一个进程使用."
所以问题是:如何Process.dll在重新加载之前以编程方式从我的应用程序中释放/释放/卸载.当然,重点是在不停止应用程序的情况下执行此操作.
编辑1:
建议的解决方案是这样的:
AppDomain newDomain4Process = AppDomain.CreateDomain("newDomain4Process");
Assembly processLibrary = newDomain4Process.Load("Process.dll");
AppDomain.Unload(newDomain4Process);
Run Code Online (Sandbox Code Playgroud)
我仍然遇到的问题是,虽然我给出了正确的完整路径,但我得到了一个FileNotFound Exception.这篇文章的答案也没有预期的效果.
编辑2:
这篇文章救了我的命,这里是代码:
class ProxyDomain : MarshalByRefObject
{
public Assembly GetAssembly(string AssemblyPath)
{
try
{
return Assembly.LoadFrom(AssemblyPath);
}
catch (Exception ex)
{
throw ex;
}
}
}
ProxyDomain pd = new ProxyDomain();
Assembly a = pd.GetAssembly(FullDLLPath);
Run Code Online (Sandbox Code Playgroud)
编辑3:
我没有访问AppDomain并使用之前的解决方案卸载它.当我使用经典的AppDomain创建方法时,我感觉到阿列克谢的警告:AppDomain.Unload"似乎"工作,但程序集仍然被加载(模块视图).所以我仍然以某种方式遇到问题,因为我无法真正有效地卸载DLL.
我试图掌握.NET 4.0+任务并行库概念......
在以下C#4.0代码段中:
Task t = Task.Factory.StartNew(() =>
{
Console.WriteLine("I am the task");
return "res1";
});
Run Code Online (Sandbox Code Playgroud)
为什么编译器没有(和运行时)如果不能使用返回产生任何错误,除非使用通用任务:
Task<string> t = Task.Factory.StartNew(() =>
{
Console.WriteLine("I am the task");
return "res1";
});
Run Code Online (Sandbox Code Playgroud)
或者它(返回的对象)可以使用?
做正确地明白的是<string>在Task<string>仅需要用于检测或确保返回(编辑对象)的类型或t.Result?
或者除了这个以外还有其他隐藏的必需品吗?
为什么这种类型不能从返回对象的类型中确定?
也就是为什么任务的Result属性不可用于非通用任务?