Skip to content

Commit e90b69a

Browse files
committed
fix(hr): Don't load assemblies using a stream to ensure that Assembly.Location is set properly
1 parent 13ce10c commit e90b69a

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

src/Uno.UI.RemoteControl.Host/RemoteControlServer.cs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ private AssemblyLoadContext GetAssemblyLoadContext(string applicationId)
9494
this.Log().LogTrace("Loading assembly from resolved path: {relPath}", relPath);
9595
}
9696

97-
using var fs = File.Open(relPath, FileMode.Open, FileAccess.Read, FileShare.Read);
98-
return context.LoadFromStream(fs);
97+
return TryLoadAssemblyFromPath(context, relPath);
9998
}
10099
}
101100
}
@@ -132,6 +131,37 @@ private AssemblyLoadContext GetAssemblyLoadContext(string applicationId)
132131
}
133132
}
134133

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+
135165
private void RegisterProcessor(IServerProcessor hotReloadProcessor)
136166
=> _processors[hotReloadProcessor.Scope] = hotReloadProcessor;
137167

@@ -287,8 +317,7 @@ private async Task ProcessDiscoveryFrame(Frame frame)
287317
{
288318
_resolveAssemblyLocations[msg.AppInstanceId] = msg.BasePath;
289319

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)));
292321
}
293322
catch (Exception exc)
294323
{

0 commit comments

Comments
 (0)