4
4
using SharePointPnP . PowerShell . Commands . Properties ;
5
5
using System ;
6
6
using System . Collections . Generic ;
7
+ using System . IO ;
7
8
using System . Linq ;
8
9
using System . Management . Automation ;
10
+ using System . Reflection ;
9
11
using System . Text ;
10
12
using System . Threading . Tasks ;
11
13
@@ -16,6 +18,7 @@ namespace SharePointPnP.PowerShell.Commands.Base
16
18
/// </summary>
17
19
public abstract class PnPGraphCmdlet : PSCmdlet
18
20
{
21
+ private Assembly newtonsoftAssembly ;
19
22
public String AccessToken
20
23
{
21
24
get
@@ -49,11 +52,48 @@ public String AccessToken
49
52
}
50
53
}
51
54
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
+
52
90
protected override void BeginProcessing ( )
53
91
{
54
92
55
93
base . BeginProcessing ( ) ;
56
94
95
+ FixAssemblyResolving ( ) ;
96
+
57
97
#if ! NETSTANDARD2_0
58
98
59
99
if ( SPOnlineConnection . CurrentConnection != null && SPOnlineConnection . CurrentConnection . ConnectionMethod == Model . ConnectionMethod . GraphDeviceLogin )
@@ -99,5 +139,10 @@ protected override void ProcessRecord()
99
139
{
100
140
ExecuteCmdlet ( ) ;
101
141
}
142
+
143
+ protected override void EndProcessing ( )
144
+ {
145
+ System . AppDomain . CurrentDomain . AssemblyResolve -= CurrentDomain_AssemblyResolve ;
146
+ }
102
147
}
103
148
}
0 commit comments