Simple host app searching for assemblies by special Interface and importing from them list of delegates, now it's Func<string,string>
.
Then it can execute any Func<T,T>
and there's no problem.
Problems starts when any of this Func
tries to access file, which doesn't exist.
No try-catch block, no File.Exists
doesn't helps — when function tries to access a file (anyway, read, get stream, check, etc) — whole app just fails with FileNotFound
in mscorlib
.
How this can be fixed? App is really critical, and I can't perform file check in app, only just in assemblies.
UPD: Yes, that delegates contains async
logic.
UPD2: Parts of code:
try
{
if(!File.Exists(filePath)) return null;
using (StreamWriter writer = new StreamWriter(destinationFilePath))
{
using (StreamReader reader = new StreamReader(filePath))
{
//some logic there
}
}
}
catch
{
}
Exception thrown at File.Exists().
This code used to import assemblies.
Commands = new Dictionary<string, Func<string, string>>();
foreach (string f in fileNames)
{
Assembly asm = Assembly.LoadFrom(f);
var types = asm.GetTypes();
foreach(Type t in types)
{
if (t.GetInterface("IMountPoint") != null)
{
var obj = Activator.CreateInstance(t);
var cmds = ((IMountPoint)obj).Init(EntryPoint);
foreach (var cmd in cmds)
{
if (!Commands.ContainsKey(cmd.Key.Trim().ToUpper()))
{
Commands.Add(cmd.Key.Trim().ToUpper(), cmd.Value);
}
}
}
}
}
And this code to run delegates:
string input = Console.ReadLine();
string res = Commands[command_key](input);
That's shameful. I'm using late binding and forgot to copy assemblies manually, so assemblies with file existence checking are not loaded by app and it used old ones. Sorry, guys.