Skip to content

Commit b050f5f

Browse files
Try and catch the already loaded exception
1 parent d5a5de5 commit b050f5f

1 file changed

Lines changed: 6 additions & 40 deletions

File tree

src/dsc/psresourceget.ps1

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -222,52 +222,18 @@ function Write-Trace {
222222
}
223223
}
224224

225-
function EnsureAssemblyLoadedByName {
226-
param(
227-
[Parameter(Mandatory)]
228-
[string] $SimpleName,
229-
230-
[string] $Path # optional fallback
231-
)
232-
233-
$loaded = [AppDomain]::CurrentDomain.GetAssemblies() |
234-
Where-Object { $_.GetName().Name -eq $SimpleName } |
235-
Select-Object -First 1
236-
237-
if (-not $loaded) {
238-
# Check in AssemblyLoadContext if running on .NET Core/.NET 5+
239-
if ($null -ne [System.Runtime.Loader.AssemblyLoadContext]::GetLoadContext([System.Reflection.Assembly])) {
240-
$loaded = [System.Runtime.Loader.AssemblyLoadContext]::All |
241-
ForEach-Object { $_.Assemblies } |
242-
Where-Object { $_.GetName().Name -eq $SimpleName } |
243-
Select-Object -First 1
244-
}
245-
}
246-
247-
if ($loaded) {
248-
# Already loaded — do nothing and return the loaded assembly
249-
return $loaded
250-
}
251-
252-
if ($Path) {
253-
# Not loaded — load from path
254-
Add-Type -Path $Path -ErrorAction Stop | Out-Null
255-
return [AppDomain]::CurrentDomain.GetAssemblies() |
256-
Where-Object { $_.GetName().Name -eq $SimpleName } |
257-
Select-Object -First 1
258-
}
259-
260-
throw "Assembly '$SimpleName' is not loaded and no -Path was provided."
261-
}
262-
263-
264225
function SatisfiesVersion {
265226
param(
266227
[string]$version,
267228
[string]$versionRange
268229
)
269230

270-
EnsureAssemblyLoadedByName -SimpleName "NuGet.Versioning" -Path "$PSScriptRoot/dependencies/NuGet.Versioning.dll" | Out-Null
231+
try {
232+
Add-Type -Path "$PSScriptRoot/dependencies/NuGet.Versioning.dll" -ErrorAction Stop | Out-Null
233+
}
234+
catch [System.IO.FileLoadException] {
235+
Write-Trace -message "NuGet.Versioning assembly is already loaded. Continuing with existing assembly." -level trace
236+
}
271237

272238
try {
273239
$versionRangeObj = [NuGet.Versioning.VersionRange]::Parse($versionRange)

0 commit comments

Comments
 (0)