Skip to content

Commit 4af8dd0

Browse files
authored
bug: correct Assembly.Location usages for self-contained builds (#139)
1 parent 9b4fc43 commit 4af8dd0

File tree

4 files changed

+12
-28
lines changed

4 files changed

+12
-28
lines changed

msgraph-developer-proxy/CertificateDiskCache.cs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,18 +92,7 @@ private string GetRootCertificateDirectory(bool create) {
9292
rootCertificatePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), ProxyConfigurationFolderName);
9393
}
9494
else {
95-
var assemblyLocation = GetType().Assembly.Location;
96-
97-
// dynamically loaded assemblies returns string.Empty location
98-
if (assemblyLocation == string.Empty) assemblyLocation = Assembly.GetEntryAssembly()?.Location;
99-
100-
#if NETSTANDARD2_1
101-
// single-file app returns string.Empty location
102-
if (assemblyLocation == string.Empty)
103-
{
104-
assemblyLocation = AppContext.BaseDirectory;
105-
}
106-
#endif
95+
var assemblyLocation = AppContext.BaseDirectory;
10796

10897
var path = Path.GetDirectoryName(assemblyLocation);
10998

msgraph-developer-proxy/PluginLoader.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ public PluginLoaderResult LoadPlugins(IPluginEvents pluginEvents, IProxyContext
2727
PluginConfig config = PluginConfig;
2828
List<Regex> globallyWatchedUrls = PluginConfig.UrlsToWatch.Select(ConvertToRegex).ToList();
2929
ISet<Regex> defaultUrlsToWatch = globallyWatchedUrls.ToHashSet();
30-
foreach (PluginReference h in config.Plugins) {
31-
if (h.Disabled) continue;
32-
// Load Handler Assembly if not disabled
33-
string? root = Path.GetDirectoryName(typeof(Program).Assembly.Location);
34-
if (!string.IsNullOrEmpty(root)) {
35-
string pluginLocation = Path.GetFullPath(Path.Combine(root, h.PluginPath.Replace('\\', Path.DirectorySeparatorChar)));
30+
string? rootDirectory = Path.GetDirectoryName(AppContext.BaseDirectory);
31+
if (!string.IsNullOrEmpty(rootDirectory)) {
32+
foreach (PluginReference h in config.Plugins) {
33+
if (h.Disabled) continue;
34+
// Load Handler Assembly if not disabled
35+
string pluginLocation = Path.GetFullPath(Path.Combine(rootDirectory, h.PluginPath.Replace('\\', Path.DirectorySeparatorChar)));
3636
PluginLoadContext pluginLoadContext = new PluginLoadContext(pluginLocation);
3737
_logger.LogDebug($"Loading from: {pluginLocation}");
3838
Assembly assembly = pluginLoadContext.LoadFromAssemblyName(new AssemblyName(Path.GetFileNameWithoutExtension(pluginLocation)));
@@ -65,7 +65,7 @@ private IProxyPlugin CreatePlugin(Assembly assembly, PluginReference h) {
6565

6666
string availableTypes = string.Join(",", assembly.GetTypes().Select(t => t.FullName));
6767
throw new ApplicationException(
68-
$"Can't find plugin {h.Name} which implements IProxyPlugin in {assembly} from {assembly.Location}.\n" +
68+
$"Can't find plugin {h.Name} which implements IProxyPlugin in {assembly} from {AppContext.BaseDirectory}.\n" +
6969
$"Available types: {availableTypes}");
7070
}
7171

msgraph-developer-proxy/ProxyEngine.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using Microsoft.Graph.DeveloperProxy.Abstractions;
55
using System.Diagnostics;
66
using System.Net;
7-
using System.Reflection;
87
using System.Text.RegularExpressions;
98
using Titanium.Web.Proxy;
109
using Titanium.Web.Proxy.EventArguments;
@@ -30,11 +29,8 @@ public class ProxyEngine {
3029
private static string _productVersion {
3130
get {
3231
if (__productVersion == string.Empty) {
33-
var assembly = Assembly.GetExecutingAssembly();
34-
if (assembly != null) {
35-
var fileVersionInfo = FileVersionInfo.GetVersionInfo(assembly.Location);
36-
__productVersion = fileVersionInfo?.ProductVersion!;
37-
}
32+
var fileVersionInfo = FileVersionInfo.GetVersionInfo(AppContext.BaseDirectory);
33+
__productVersion = fileVersionInfo?.ProductVersion!;
3834
}
3935

4036
return __productVersion;

msgraph-developer-proxy/UpdateNotification.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,8 @@ internal static class UpdateNotification {
4949
}
5050

5151
private static Version GetCurrentVersion() {
52-
var assembly = Assembly.GetExecutingAssembly();
53-
var fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
54-
var currentVersion = new Version(fvi.ProductVersion);
52+
var fvi = FileVersionInfo.GetVersionInfo(AppContext.BaseDirectory);
53+
var currentVersion = new Version(fvi.ProductVersion ?? "0.0.0.0");
5554

5655
return currentVersion;
5756
}

0 commit comments

Comments
 (0)