失敗之後,我們以點帶面的.Net正確的文件夾裝入從組件實現的AssemblyResolve事件以下知識庫文章的例子:http://support.microsoft.com/kb/837908裝載程序集隨機
這是我們的當前實現:
public static class AssemblyLoader
{
/// <summary>
/// A custom AssemblyResolver that will search for missing assemblies in the root and subfolders of the executing assembly
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
/// <returns></returns>
public static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
string assemblyPath = string.Empty;
string assemblyFileName = string.Empty;
try
{
// This handler is called only when the common language runtime tries to bind to the assembly and fails.
string rootProbingPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
// Loop through the referenced assembly names.
assemblyFileName = args.Name.Substring(0, args.Name.IndexOf(",")) + ".dll";
// Search for the filename in the root and subfolders of the rootProbingPath
string[] matchingAssemblies = Directory.GetFiles(rootProbingPath, assemblyFileName, SearchOption.AllDirectories);
// If a match is found, awesomeness was achieved!
if (matchingAssemblies.Length > 0)
assemblyPath = matchingAssemblies[0];
// Throw a clear exception when the assembly could not be found.
if (string.IsNullOrEmpty(assemblyPath))
throw new FileNotFoundException(string.Format("AssemblyLoader: Could not find assembly '{0}' in '{1}' or its subfolders.", assemblyFileName, rootProbingPath));
Console.WriteLine(string.Format("[" + DateTime.Now.ToString() + "] AssemblyLoader: Assembly '{0}' found at '{1}'", assemblyFileName, assemblyPath));
// Load the assembly from the specified path.
return Assembly.LoadFrom(assemblyPath, AppDomain.CurrentDomain.Evidence);
}
catch (Exception ex)
{
throw new Exception(string.Format("[" + DateTime.Now.ToString() + "] The assemblyloader could not load the assembly '{0}' from path '{1}': " + ex.Message, assemblyFileName, assemblyPath), ex);
}
}
}
我們在所有的往往並行運行,並且大多采用相同的組件就是批程序使用。
定期間歇會崩潰離開下列線索:
[26/04/2013 12時35分01秒] AssemblyLoader:裝配 'COMPANY.DistributieOrderFacade.dll' 在 「C發現:\ COMPANY_Batch \ MDS \可執行\ MDS \ FACADE \ COMPANY.DistributieOrderFacade.dll '
[26/04/2013 12時35分01秒] AssemblyLoader:裝配 'COMPANY.DOCUMENTCENTERDOCS.dll' 在 發現' C:\ COMPANY_Batch \ MDS \ Executables \ MDS \ CLIENT \ COMPANY.DOCUMENTCENTERDOCS.dll'
26/04/2013 12時35分01秒:在 隊列創建COMPANYDocument ...:\ rug.adroot \ dfsroot \ MDS \ DATA \ queue_new \ 26/04/2013 12時35分01秒
無法加載文件或程序集'COMPANY.DistributieOrderBRDA, 版本= 1.0.0.0,Culture = neutral,PublicKeyToken = null'或其依賴關係之一 。一般例外(來自HRESULT的例外:0x80131500)
錯誤中的程序集確實位於AssemblyLoader搜索的其中一個文件夾中。如果我再次運行該程序,它會成功。
我試着看看在兩個控制檯應用程序中使用這個程序集加載器,並且同時訪問相同的DLL,但是這似乎沒有問題。
我還應該提到,這只是來自其中一個批次的日誌。還有其他的,它很少是沒有被加載的相同的程序集。
我不知道從哪裏開始尋找解決方案。