@@ -34,30 +34,38 @@ public async Task<TReturn> Execute<TReturn>(string dllPath, IEnumerable<string>
3434var runtimeDepsMap = ScriptCompiler . CreateScriptDependenciesMap ( runtimeDeps ) ;
3535var assembly = Assembly . LoadFrom ( dllPath ) ; // this needs to be called prior to 'AppDomain.CurrentDomain.AssemblyResolve' event handler added
3636
37- AppDomain . CurrentDomain . AssemblyResolve += ( sender , args ) => ResolveAssembly ( args , runtimeDepsMap ) ;
37+ Assembly OnResolve ( object sender , ResolveEventArgs args ) => ResolveAssembly ( args , runtimeDepsMap ) ;
3838
39- var type = assembly . GetType ( "Submission#0" ) ;
40- var method = type . GetMethod ( "<Factory>" , BindingFlags . Static | BindingFlags . Public ) ;
41-
42- var globals = new CommandLineScriptGlobals ( ScriptConsole . Out , CSharpObjectFormatter . Instance ) ;
43- foreach ( var arg in commandLineArgs )
44- globals . Args . Add ( arg ) ;
45-
46- var submissionStates = new object [ 2 ] ;
47- submissionStates [ 0 ] = globals ;
48-
49- var resultTask = method . Invoke ( null , new [ ] { submissionStates } ) as Task < TReturn > ;
39+ AppDomain . CurrentDomain . AssemblyResolve += OnResolve ;
5040try
5141{
52- _ = await resultTask ;
42+ var type = assembly . GetType ( "Submission#0" ) ;
43+ var method = type . GetMethod ( "<Factory>" , BindingFlags . Static | BindingFlags . Public ) ;
44+
45+ var globals = new CommandLineScriptGlobals ( ScriptConsole . Out , CSharpObjectFormatter . Instance ) ;
46+ foreach ( var arg in commandLineArgs )
47+ globals . Args . Add ( arg ) ;
48+
49+ var submissionStates = new object [ 2 ] ;
50+ submissionStates [ 0 ] = globals ;
51+
52+ var resultTask = method . Invoke ( null , new [ ] { submissionStates } ) as Task < TReturn > ;
53+ try
54+ {
55+ _ = await resultTask ;
56+ }
57+ catch ( System . Exception ex )
58+ {
59+ ScriptConsole . WriteError ( ex . ToString ( ) ) ;
60+ throw new ScriptRuntimeException ( "Script execution resulted in an exception." , ex ) ;
61+ }
62+
63+ return await resultTask ;
5364}
54- catch ( System . Exception ex )
65+ finally
5566{
56- ScriptConsole . WriteError ( ex . ToString ( ) ) ;
57- throw new ScriptRuntimeException ( "Script execution resulted in an exception." , ex ) ;
67+ AppDomain . CurrentDomain . AssemblyResolve -= OnResolve ;
5868}
59-
60- return await resultTask ;
6169}
6270
6371public Task < TReturn > Execute < TReturn > ( ScriptContext context )