Skip to content

Commit a6439dc

Browse files
authored
Assembly loading improvements (#20196)
* conditional assembly loading * use newtonsoft.json 12 at runtime * eliminate PreloadAssemblies * remove 4 netfx dll: can use netstandard
1 parent da5fbf3 commit a6439dc

File tree

70 files changed

+1122
-216
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+1122
-216
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,3 +239,6 @@ src/DataFactory/DataFactoryV2.Test/SessionRecords/Microsoft.Azure.Commands.DataF
239239

240240
# GitHub codespaces
241241
.venv
242+
243+
# Visual studio live unit testing config
244+
*.lutconfig

build.proj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@
205205
<RuntimeDllsIncludeList>Microsoft.Powershell.*.dll,System*.dll,Microsoft.VisualBasic.dll,Microsoft.CSharp.dll,Microsoft.CodeAnalysis.dll,Microsoft.CodeAnalysis.CSharp.dll</RuntimeDllsIncludeList>
206206
<RuntimeDllsExcludeList>System.Security.Cryptography.ProtectedData.dll,System.Configuration.ConfigurationManager.dll,System.Runtime.CompilerServices.Unsafe.dll,System.IO.FileSystem.AccessControl.dll,System.Buffers.dll,System.Text.Encodings.Web.dll,System.CodeDom.dll,System.Management.dll,System.Text.Json.dll,System.Threading.Tasks.Extensions.dll,System.IO.Hashing.dll</RuntimeDllsExcludeList>
207207
</PropertyGroup>
208-
<Exec Command="$(PowerShellCoreCommandPrefix) &quot;Get-ChildItem -Path $(RepoArtifacts)/$(Configuration) -Recurse -Include $(RuntimeDllsIncludeList) -Exclude $(RuntimeDllsExcludeList) | Where-Object {$_.FullName -notlike '*PreloadAssemblies*' -and $_.FullName -notlike '*NetCoreAssemblies*' -and $_.FullName -notlike '*AzSharedAlcAssemblies*' -and $_.FullName -notlike '*ModuleAlcAssemblies*'} | Remove-Item -Force&quot;"/>
208+
<Exec Command="$(PowerShellCoreCommandPrefix) &quot;Get-ChildItem -Path $(RepoArtifacts)/$(Configuration) -Recurse -Include $(RuntimeDllsIncludeList) -Exclude $(RuntimeDllsExcludeList) | Where-Object {$_.FullName -notlike '*Accounts*lib*' -and $_.FullName -notlike '*ModuleAlcAssemblies*'} | Remove-Item -Force&quot;"/>
209209
<Exec Command="$(PowerShellCoreCommandPrefix) &quot;Get-ChildItem -Path $(RepoArtifacts)/$(Configuration) -Recurse -Include 'runtimes' | Remove-Item -Recurse -Force&quot;" Condition="'$(CodeSign)' == 'true'" />
210210

211211
<Exec Command="$(PowerShellCoreCommandPrefix) &quot;. $(RepoTools)/UpdateModules.ps1 -BuildConfig $(Configuration) -Scope $(Scope)&quot;"/>

src/Accounts/Accounts.sln

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Microsoft Visual Studio Solution File, Format Version 12.00
2-
# Visual Studio Version 16
3-
VisualStudioVersion = 16.0.30503.244
2+
# Visual Studio Version 17
3+
VisualStudioVersion = 17.3.32929.385
44
MinimumVisualStudioVersion = 10.0.40219.1
55
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Accounts", "Accounts\Accounts.csproj", "{142D7B0B-388A-4CEB-A228-7F6D423C5C2E}"
66
EndProject
@@ -22,6 +22,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Authentication.Test", "Auth
2222
EndProject
2323
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AuthenticationAssemblyLoadContext", "AuthenticationAssemblyLoadContext\AuthenticationAssemblyLoadContext.csproj", "{D06EF9FC-4C3E-4A51-A4AD-D39AD426EF8C}"
2424
EndProject
25+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AssemblyLoading", "AssemblyLoading\AssemblyLoading.csproj", "{74C0BD7E-DFFD-4B96-818E-D8660FA43159}"
26+
EndProject
27+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AssemblyLoading.Test", "AssemblyLoading.Test\AssemblyLoading.Test.csproj", "{D4540550-9808-4DEB-9D5E-F88E38D58A85}"
28+
EndProject
2529
Global
2630
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2731
Debug|Any CPU = Debug|Any CPU
@@ -64,13 +68,22 @@ Global
6468
{D06EF9FC-4C3E-4A51-A4AD-D39AD426EF8C}.Debug|Any CPU.Build.0 = Debug|Any CPU
6569
{D06EF9FC-4C3E-4A51-A4AD-D39AD426EF8C}.Release|Any CPU.ActiveCfg = Release|Any CPU
6670
{D06EF9FC-4C3E-4A51-A4AD-D39AD426EF8C}.Release|Any CPU.Build.0 = Release|Any CPU
71+
{74C0BD7E-DFFD-4B96-818E-D8660FA43159}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
72+
{74C0BD7E-DFFD-4B96-818E-D8660FA43159}.Debug|Any CPU.Build.0 = Debug|Any CPU
73+
{74C0BD7E-DFFD-4B96-818E-D8660FA43159}.Release|Any CPU.ActiveCfg = Release|Any CPU
74+
{74C0BD7E-DFFD-4B96-818E-D8660FA43159}.Release|Any CPU.Build.0 = Release|Any CPU
75+
{D4540550-9808-4DEB-9D5E-F88E38D58A85}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
76+
{D4540550-9808-4DEB-9D5E-F88E38D58A85}.Debug|Any CPU.Build.0 = Debug|Any CPU
77+
{D4540550-9808-4DEB-9D5E-F88E38D58A85}.Release|Any CPU.ActiveCfg = Release|Any CPU
78+
{D4540550-9808-4DEB-9D5E-F88E38D58A85}.Release|Any CPU.Build.0 = Release|Any CPU
6779
EndGlobalSection
6880
GlobalSection(SolutionProperties) = preSolution
6981
HideSolutionNode = FALSE
7082
EndGlobalSection
7183
GlobalSection(NestedProjects) = preSolution
7284
{152D78F0-A642-4D0E-B3A8-2FC64FFA9714} = {95C16AED-FD57-42A0-86C3-2CF4300A4817}
7385
{43BE9983-BD21-4474-92E5-1FFC0220B2AD} = {95C16AED-FD57-42A0-86C3-2CF4300A4817}
86+
{D4540550-9808-4DEB-9D5E-F88E38D58A85} = {95C16AED-FD57-42A0-86C3-2CF4300A4817}
7487
EndGlobalSection
7588
GlobalSection(ExtensibilityGlobals) = postSolution
7689
SolutionGuid = {AA51E4F8-AA75-429D-9626-12C7B4305D72}

src/Accounts/Accounts/Accounts.csproj

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
</PropertyGroup>
1212

1313
<ItemGroup>
14-
<PreLoadAssemblies Include="$(RepoSrc)lib\NetFxPreloadAssemblies\*.dll" />
15-
<NetCoreAssemblies Include="$(RepoSrc)lib\NetCorePreloadAssemblies\*.dll" />
14+
<NetFxAssemblies Include="$(RepoSrc)lib\netfx\*.dll" />
15+
<NetCore21Assemblies Include="$(RepoSrc)lib\netcoreapp2.1\*.dll" />
16+
<NetCore31Assemblies Include="$(RepoSrc)lib\netcoreapp3.1\*.dll" />
17+
<NetStandardAssemblies Include="$(RepoSrc)lib\netstandard2.0\*.dll" />
1618
<StorageDependencies Include="$(RepoSrc)lib\WindowsAzure.Storage\9.3.0\Microsoft.WindowsAzure.Storage.dll" />
1719
<StorageDependencies Include="$(RepoSrc)lib\WindowsAzure.Storage\9.3.0\Microsoft.WindowsAzure.Storage.DataMovement.dll" />
1820
</ItemGroup>
@@ -22,10 +24,12 @@
2224
</ItemGroup>
2325

2426
<Target Name="CopyFiles" AfterTargets="Build">
25-
<Copy SourceFiles="@(PreLoadAssemblies)" DestinationFolder="$(TargetDir)PreloadAssemblies" />
26-
<Copy SourceFiles="@(NetCoreAssemblies)" DestinationFolder="$(TargetDir)AzSharedAlcAssemblies" />
27+
<Copy SourceFiles="@(NetFxAssemblies)" DestinationFolder="$(TargetDir)lib\netfx" />
28+
<Copy SourceFiles="@(NetCore21Assemblies)" DestinationFolder="$(TargetDir)lib\netcoreapp2.1" />
29+
<Copy SourceFiles="@(NetCore31Assemblies)" DestinationFolder="$(TargetDir)lib\netcoreapp3.1" />
30+
<Copy SourceFiles="@(NetStandardAssemblies)" DestinationFolder="$(TargetDir)lib\netstandard2.0" />
2731
<Copy SourceFiles="@(StorageDependencies)" DestinationFolder="$(TargetDir)" />
28-
<!--To automatically lauch pwsh for debugging when hitting F5, set below and UNCOMMENT Copy:
32+
<!--To automatically launch pwsh for debugging when hitting F5, set below and UNCOMMENT Copy:
2933
Executable: pwsh
3034
Application arguments: -NoExit -Command "Import-Module C:\AME\azure-powershell\artifacts\Debug\Az.Accounts\Az.Accounts.psd1"-->
3135
<!--<Copy SourceFiles="@(NetCoreAssemblies)" DestinationFolder="$(TargetDir)" />-->
@@ -43,27 +47,27 @@
4347
<EmbeddedResource Include="AzureRmAlias\Mappings.json" />
4448
<EmbeddedResource Include="Utilities\CommandMappings.json" />
4549
</ItemGroup>
46-
50+
4751
<ItemGroup>
4852
<ProjectReference Include="..\Authentication.ResourceManager\Authentication.ResourceManager.csproj" />
4953
<ProjectReference Include="..\Authentication\Authentication.csproj" />
50-
<ProjectReference Include="..\Authenticators\Authenticators.csproj" />
54+
<ProjectReference Include="..\Authenticators\Authenticators.csproj" />
5155
</ItemGroup>
52-
56+
5357
<ItemGroup>
5458
<Reference Include="FuzzySharp">
5559
<HintPath>..\..\lib\FuzzySharp.dll</HintPath>
5660
</Reference>
5761
</ItemGroup>
58-
62+
5963
<ItemGroup>
6064
<Compile Update="Properties\Resources.Designer.cs">
6165
<DesignTime>True</DesignTime>
6266
<AutoGen>True</AutoGen>
6367
<DependentUpon>Resources.resx</DependentUpon>
6468
</Compile>
6569
</ItemGroup>
66-
70+
6771
<ItemGroup>
6872
<EmbeddedResource Update="Properties\Resources.resx">
6973
<Generator>ResXFileCodeGenerator</Generator>

src/Accounts/Accounts/Az.Accounts.psd1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ DotNetFrameworkVersion = '4.7.2'
5656
# RequiredModules = @()
5757

5858
# Assemblies that must be loaded prior to importing this module
59-
RequiredAssemblies = 'Microsoft.Azure.PowerShell.Authentication.Abstractions.dll',
59+
RequiredAssemblies = 'Microsoft.Azure.PowerShell.AssemblyLoading.dll',
60+
'Microsoft.Azure.PowerShell.Authentication.Abstractions.dll',
6061
'Microsoft.Azure.PowerShell.Authentication.dll',
6162
'Microsoft.Azure.PowerShell.Authenticators.dll',
6263
'Microsoft.Azure.PowerShell.Authentication.ResourceManager.dll',

src/Accounts/Accounts/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
-->
2020

2121
## Upcoming Release
22+
* Optimized the mechanism for assembly loading.
2223
* Enabled AzKeyStore with keyring in Linux.
2324
* Fixed a typo in GetAzureRmContextAutosaveSetting.cs changing the cmdlet class name from GetzureRmContextAutosaveSetting to GetAzureRmContextAutosaveSetting
2425
* Removed survey on error message in `Resolve-AzError`. [#20398]

src/Accounts/Accounts/StartupScripts/InitializeAssemblyResolver.ps1

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
if ($PSEdition -eq 'Desktop') {
1+
$assemblyRootPath = [System.IO.Path]::Combine($PSScriptRoot, "..", "lib")
2+
$conditionalAssemblyContext = [Microsoft.Azure.PowerShell.AssemblyLoading.ConditionalAssemblyContext]::new($Host.Version)
3+
[Microsoft.Azure.PowerShell.AssemblyLoading.ConditionalAssemblyProvider]::Initialize($assemblyRootPath, $conditionalAssemblyContext)
4+
5+
if ($PSEdition -eq 'Desktop') {
26
try {
37
[Microsoft.Azure.Commands.Profile.Utilities.CustomAssemblyResolver]::Initialize()
48
}
@@ -9,9 +13,8 @@
913
else {
1014
try {
1115
Add-Type -Path ([System.IO.Path]::Combine($PSScriptRoot, "..", "Microsoft.Azure.PowerShell.AuthenticationAssemblyLoadContext.dll")) | Out-Null
12-
$assemblyLoadContextFolder = [System.IO.Path]::Combine($PSScriptRoot, "..", "AzSharedAlcAssemblies")
13-
Write-Debug "Registering Az shared AssemblyLoadContext for path: '$assemblyLoadContextFolder'."
14-
[Microsoft.Azure.PowerShell.AuthenticationAssemblyLoadContext.AzAssemblyLoadContextInitializer]::RegisterAzSharedAssemblyLoadContext($assemblyLoadContextFolder)
16+
Write-Debug "Registering Az shared AssemblyLoadContext."
17+
[Microsoft.Azure.PowerShell.AuthenticationAssemblyLoadContext.AzAssemblyLoadContextInitializer]::RegisterAzSharedAssemblyLoadContext()
1518
Write-Debug "AssemblyLoadContext registered."
1619
}
1720
catch {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<PsModuleName>Accounts</PsModuleName>
4+
</PropertyGroup>
5+
6+
<Import Project="$(MSBuildThisFileDirectory)..\..\Az.Test.props" />
7+
8+
<PropertyGroup>
9+
<AssemblyName>Microsoft.Azure.PowerShell.AssemblyLoading.Test</AssemblyName>
10+
<RootNamespace>Microsoft.Azure.PowerShell.AssemblyLoading.Test</RootNamespace>
11+
</PropertyGroup>
12+
13+
<ItemGroup>
14+
<ProjectReference Include="..\AssemblyLoading\AssemblyLoading.csproj" />
15+
</ItemGroup>
16+
17+
</Project>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
16+
17+
namespace Microsoft.Azure.PowerShell.AssemblyLoading.Test.Mocks
18+
{
19+
internal class MockConditionalAssembly : IConditionalAssembly
20+
{
21+
public MockConditionalAssembly(IConditionalAssemblyContext context)
22+
{
23+
Context = context;
24+
}
25+
public bool ShouldLoad { get; set; } = true;
26+
27+
public string Name { get; set; }
28+
29+
public string Path { get; set; }
30+
31+
public Version Version { get; set; }
32+
33+
public IConditionalAssemblyContext Context { get; set; }
34+
35+
public void UpdateShouldLoad(bool shouldLoad)
36+
{
37+
ShouldLoad = ShouldLoad && shouldLoad;
38+
}
39+
}
40+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
16+
using System.Runtime.InteropServices;
17+
18+
namespace Microsoft.Azure.PowerShell.AssemblyLoading.Test.Mocks
19+
{
20+
internal class MockConditionalAssemblyContext : IConditionalAssemblyContext
21+
{
22+
public Version PSVersion { get; set; }
23+
public Architecture OSArchitecture { get; set; }
24+
public OSPlatform OS { get; set; }
25+
26+
public bool IsOSPlatform(OSPlatform os)
27+
{
28+
return OS.Equals(os);
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)