LotSizingDataModel.Solver.Xpress 2.0.1
Xpress solver adapter for the LotSizingDataModel solver abstraction.
Loading...
Searching...
No Matches
XpressRuntimeLocator.cs
Go to the documentation of this file.
1using System.Reflection;
2using System.Runtime.Loader;
3
5
6/// <summary>
7/// Locates and loads the optional FICO Xpress Optimizer managed assembly.
8/// </summary>
9internal static class XpressRuntimeLocator
10{
12 "LOTSIZING_XPRESS_OPTIMIZER_ASSEMBLY";
13
14 /// <summary>
15 /// Loads the already available Optimizer assembly or resolves it from common
16 /// Xpress installation locations.
17 /// </summary>
18 /// <param name="resolvedPath">Resolved assembly path when loaded from disk.</param>
19 /// <returns>Loaded assembly, or <see langword="null"/>.</returns>
20 internal static Assembly? TryLoad(
21 out string resolvedPath)
22 {
23 resolvedPath = string.Empty;
24
25 Assembly? loaded =
26 AppDomain.CurrentDomain
27 .GetAssemblies()
28 .FirstOrDefault(
29 assembly =>
30 string.Equals(
31 assembly.GetName().Name,
32 "Optimizer",
33 StringComparison.OrdinalIgnoreCase) ||
34 string.Equals(assembly.GetName().Name, "xprsdn", StringComparison.OrdinalIgnoreCase));
35
36 if (loaded is not null)
37 {
38 resolvedPath =
39 string.IsNullOrWhiteSpace(loaded.Location)
40 ? "already loaded"
41 : loaded.Location;
42 return loaded;
43 }
44
45 string explicitPath =
46 Environment.GetEnvironmentVariable(
48 string.Empty;
49
50 if (File.Exists(explicitPath))
51 {
52 resolvedPath = Path.GetFullPath(explicitPath);
53 return AssemblyLoadContext.Default.LoadFromAssemblyPath(
54 resolvedPath);
55 }
56
57 string xpressDirectory =
58 Environment.GetEnvironmentVariable("XPRESSDIR")?.Trim() ??
59 string.Empty;
60
61 if (!string.IsNullOrWhiteSpace(xpressDirectory))
62 {
63 string[] candidates =
64 [
65 Path.Combine(xpressDirectory, "bin", "Optimizer.dll"),
66 Path.Combine(xpressDirectory, "lib", "Optimizer.dll"),
67 Path.Combine(xpressDirectory, "bin", "dotnet", "Optimizer.dll"),
68 Path.Combine(xpressDirectory, "lib", "dotnet", "Optimizer.dll"),
69 Path.Combine(xpressDirectory, "Optimizer.dll")
70 ];
71
72 foreach (string candidate in candidates)
73 {
74 if (!File.Exists(candidate))
75 {
76 continue;
77 }
78
79 resolvedPath = Path.GetFullPath(candidate);
80 return AssemblyLoadContext.Default.LoadFromAssemblyPath(
81 resolvedPath);
82 }
83 }
84
85 foreach (string assemblyName in new[] { "Optimizer", "xprsdn" })
86 {
87 try
88 {
89 Assembly assembly = Assembly.Load(assemblyName);
90 resolvedPath = string.IsNullOrWhiteSpace(assembly.Location)
91 ? "assembly probing"
92 : assembly.Location;
93 return assembly;
94 }
95 catch
96 {
97 // Try the next supported SDK assembly identity.
98 }
99 }
100
101 return null;
102 }
103}
Locates and loads the optional FICO Xpress Optimizer managed assembly.
static ? Assembly TryLoad(out string resolvedPath)
Loads the already available Optimizer assembly or resolves it from common Xpress installation locatio...