@@ -94,8 +94,7 @@ private AssemblyLoadContext GetAssemblyLoadContext(string applicationId)
94
94
this . Log ( ) . LogTrace ( "Loading assembly from resolved path: {relPath}" , relPath ) ;
95
95
}
96
96
97
- using var fs = File . Open ( relPath , FileMode . Open , FileAccess . Read , FileShare . Read ) ;
98
- return context . LoadFromStream ( fs ) ;
97
+ return TryLoadAssemblyFromPath ( context , relPath ) ;
99
98
}
100
99
}
101
100
}
@@ -132,6 +131,37 @@ private AssemblyLoadContext GetAssemblyLoadContext(string applicationId)
132
131
}
133
132
}
134
133
134
+ private static Assembly TryLoadAssemblyFromPath ( AssemblyLoadContext context , string asmPath )
135
+ {
136
+ // Load the assembly using the full path to avoid duplicates related
137
+ // relative paths pointing to the same file.
138
+ asmPath = Path . GetFullPath ( asmPath ) ;
139
+
140
+ // Try loading the assembly multiple times, using a try catch and a loop with a sleep
141
+ // to avoid issues with the assembly being locked by another process.
142
+ int tries = 10 ;
143
+ do
144
+ {
145
+ try
146
+ {
147
+ return context . LoadFromAssemblyPath ( asmPath ) ;
148
+ }
149
+ catch ( Exception exc )
150
+ {
151
+ if ( context . Log ( ) . IsEnabled ( LogLevel . Trace ) )
152
+ {
153
+ context . Log ( ) . LogTrace ( "Failed to load assembly {asmPath} : {exc}" , asmPath , exc ) ;
154
+ }
155
+ }
156
+
157
+ Thread . Sleep ( 100 ) ;
158
+ }
159
+ while ( tries -- > 0 ) ;
160
+
161
+ // Try without exception handling to report the original exception
162
+ return context . LoadFromAssemblyPath ( asmPath ) ;
163
+ }
164
+
135
165
private void RegisterProcessor ( IServerProcessor hotReloadProcessor )
136
166
=> _processors [ hotReloadProcessor . Scope ] = hotReloadProcessor ;
137
167
@@ -287,8 +317,7 @@ private async Task ProcessDiscoveryFrame(Frame frame)
287
317
{
288
318
_resolveAssemblyLocations [ msg . AppInstanceId ] = msg . BasePath ;
289
319
290
- using var fs = File . Open ( msg . BasePath , FileMode . Open , FileAccess . Read , FileShare . Read ) ;
291
- assemblies . Add ( ( msg . BasePath , assemblyLoadContext . LoadFromStream ( fs ) ) ) ;
320
+ assemblies . Add ( ( msg . BasePath , TryLoadAssemblyFromPath ( assemblyLoadContext , msg . BasePath ) ) ) ;
292
321
}
293
322
catch ( Exception exc )
294
323
{
0 commit comments