Skip to content
This repository was archived by the owner on Jan 19, 2021. It is now read-only.

Commit b33a014

Browse files
Merge pull request #1614 from erwinvanhunen/dev
Fixes issue with Graph cmdlets
2 parents 758dea0 + 9595ded commit b33a014

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

Commands/Base/PnPGraphCmdlet.cs

+45
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
using SharePointPnP.PowerShell.Commands.Properties;
55
using System;
66
using System.Collections.Generic;
7+
using System.IO;
78
using System.Linq;
89
using System.Management.Automation;
10+
using System.Reflection;
911
using System.Text;
1012
using 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
}

Commands/Properties/AssemblyInfo.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,6 @@
4444
// You can specify all the values or you can default the Build and Revision Numbers
4545
// by using the '*' as shown below:
4646
// [assembly: AssemblyVersion("1.0.*")]
47-
[assembly: AssemblyVersion("2.27.1806.0")]
48-
[assembly: AssemblyFileVersion("2.27.1806.0")]
47+
[assembly: AssemblyVersion("2.27.1806.1")]
48+
[assembly: AssemblyFileVersion("2.27.1806.1")]
4949
[assembly: InternalsVisibleTo("SharePointPnP.PowerShell.Tests")]

0 commit comments

Comments
 (0)