@@ -127,18 +127,47 @@ async void OnHandleCommand(object sender, EventArgs e)
127127
128128file static class AssemblyResolver
129129{
130+ [ ThreadStatic ]
131+ private static int _nestingCount ;
132+
130133 public static void Initialize ( )
131134 => AppDomain . CurrentDomain . AssemblyResolve += OnAssemblyResolve ;
132135
133136 private static Assembly ? OnAssemblyResolve ( object sender , ResolveEventArgs e )
134137 {
135- var assemblyName = new AssemblyName ( e . Name ) . Name ;
136- var currentFolder = Path . GetDirectoryName ( Assembly . GetExecutingAssembly ( ) . Location ) ;
137- var assemblyPath = Path . Combine ( currentFolder , assemblyName + ".dll" ) ;
138+ // Prevent infinite recursion by checking if we're already inside this resolver
139+ if ( _nestingCount > 0 )
140+ return null ;
138141
139- if ( File . Exists ( assemblyPath ) )
140- return Assembly . LoadFile ( assemblyPath ) ;
142+ _nestingCount ++ ;
143+ try
144+ {
145+ var requestedAssembly = new AssemblyName ( e . Name ) ;
146+ var assemblyName = requestedAssembly . Name ;
147+ var currentFolder = Path . GetDirectoryName ( Assembly . GetExecutingAssembly ( ) . Location ) ;
148+ var assemblyPath = Path . Combine ( currentFolder , assemblyName + ".dll" ) ;
141149
142- return null ;
150+ if ( ! File . Exists ( assemblyPath ) )
151+ return null ;
152+
153+ var onDiskAssembly = AssemblyName . GetAssemblyName ( assemblyPath ) ;
154+
155+ // Only load if versions match - return null if they don't to allow other resolvers to try
156+ if ( requestedAssembly . Version != null &&
157+ onDiskAssembly . Version != requestedAssembly . Version )
158+ return null ;
159+
160+ return Assembly . LoadFile ( assemblyPath ) ;
161+ }
162+ catch
163+ {
164+ // Swallow all exceptions and return null to allow other resolvers to try
165+ // or to let the CLR handle the failure in the standard way
166+ return null ;
167+ }
168+ finally
169+ {
170+ _nestingCount -- ;
171+ }
143172 }
144173}
0 commit comments