44using SharePointPnP . PowerShell . Commands . Properties ;
55using System ;
66using System . Collections . Generic ;
7+ using System . IO ;
78using System . Linq ;
89using System . Management . Automation ;
10+ using System . Reflection ;
911using System . Text ;
1012using System . Threading . Tasks ;
1113
@@ -16,6 +18,7 @@ namespace SharePointPnP.PowerShell.Commands.Base
1618 /// </summary>
1719 public abstract class PnPGraphCmdlet : PSCmdlet
1820 {
21+ private Assembly newtonsoftAssembly ;
1922 public String AccessToken
2023 {
2124 get
@@ -49,11 +52,48 @@ public String AccessToken
4952 }
5053 }
5154
55+ private string AssemblyDirectory
56+ {
57+ get
58+ {
59+ string codeBase = Assembly . GetExecutingAssembly ( ) . CodeBase ;
60+ UriBuilder uri = new UriBuilder ( codeBase ) ;
61+ string path = Uri . UnescapeDataString ( uri . Path ) ;
62+ return Path . GetDirectoryName ( path ) ;
63+ }
64+ }
65+
66+ private void FixAssemblyResolving ( )
67+ {
68+ newtonsoftAssembly = System . Reflection . Assembly . LoadFrom ( Path . Combine ( AssemblyDirectory , "NewtonSoft.Json.dll" ) ) ;
69+ System . AppDomain . CurrentDomain . AssemblyResolve += CurrentDomain_AssemblyResolve ;
70+
71+ }
72+
73+ private System . Reflection . Assembly CurrentDomain_AssemblyResolve ( object sender , ResolveEventArgs args )
74+ {
75+
76+ if ( args . Name . StartsWith ( "Newtonsoft.Json" ) )
77+ {
78+ return newtonsoftAssembly ;
79+ }
80+ foreach ( var assembly in System . AppDomain . CurrentDomain . GetAssemblies ( ) )
81+ {
82+ if ( assembly . FullName == args . Name )
83+ {
84+ return assembly ;
85+ }
86+ }
87+ return null ;
88+ }
89+
5290 protected override void BeginProcessing ( )
5391 {
5492
5593 base . BeginProcessing ( ) ;
5694
95+ FixAssemblyResolving ( ) ;
96+
5797#if ! NETSTANDARD2_0
5898
5999 if ( SPOnlineConnection . CurrentConnection != null && SPOnlineConnection . CurrentConnection . ConnectionMethod == Model . ConnectionMethod . GraphDeviceLogin )
@@ -99,5 +139,10 @@ protected override void ProcessRecord()
99139 {
100140 ExecuteCmdlet ( ) ;
101141 }
142+
143+ protected override void EndProcessing ( )
144+ {
145+ System . AppDomain . CurrentDomain . AssemblyResolve -= CurrentDomain_AssemblyResolve ;
146+ }
102147 }
103148}
0 commit comments