Skip to content

Commit

Permalink
Hack for Allowing Plugins to load with ProxyType Definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
daryllabar committed Aug 12, 2016
1 parent 3071130 commit b81e1da
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
56 changes: 56 additions & 0 deletions XrmToolBox/AppDomainProxyTypeHack.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Linq;
using System.Reflection;

namespace XrmToolBox
{
/// <summary>
/// This class/hack was created to address issues with the Xrm Sdk not allowing the earlybound type assembly to be specified.
/// https://github.com/MscrmTools/XrmToolBox/issues/353
/// </summary>
internal class AppDomainProxyTypeHack
{
public static void OverrideAppDomainKnownTypeInitialization()
{
InvokeAddingOfAssemblyLoadEventHandler();
RemoveAssemblyLoadEventHandler();
}

private static void InvokeAddingOfAssemblyLoadEventHandler()
{
var proxyTypesProvider = typeof(Microsoft.Xrm.Sdk.IOrganizationService).Assembly.GetType("Microsoft.Xrm.Sdk.KnownProxyTypesProvider");
if (proxyTypesProvider == null)
{
throw new Exception("AppDomainProxyTypeHack expected Microsoft.Xrm.Sdk.KnownProxyTypesProvider to exist in the same assembly as Microsoft.Xrm.Sdk.IOrganizationService, but it wasn't found!");
}
var getInstance = proxyTypesProvider.GetMethod("GetInstance", BindingFlags.NonPublic | BindingFlags.Static);
if (getInstance == null)
{
throw new Exception("AppDomainProxyTypeHack expected Microsoft.Xrm.Sdk.KnownProxyTypesProvider to contain a GetInstance(bool) method, but it wasn't found!");
}
getInstance.Invoke(null, BindingFlags.Static, null, new object[] { false }, null);
}

private static void RemoveAssemblyLoadEventHandler()
{
var assemblyLoadField = AppDomain.CurrentDomain.GetType().GetField("AssemblyLoad", BindingFlags.Instance | BindingFlags.NonPublic);
if (assemblyLoadField == null)
{
throw new Exception("AppDomainProxyTypeHack expected AppDomain to contain an AssemblyLoad field, and none was found!");
}

var listOfDelegates = (AssemblyLoadEventHandler) assemblyLoadField.GetValue(AppDomain.CurrentDomain);
if (listOfDelegates == null)
{
throw new Exception("AppDomainProxyTypeHack expected Microsoft.Xrm.Sdk.KnownProxyTypesProvider.GetInstance(false) to add an Event handler to the AssemblyLoad Event, and none was found!");
}
var first = listOfDelegates.GetInvocationList().FirstOrDefault(i => i.Target.GetType().FullName == "Microsoft.Xrm.Sdk.AppDomainBasedKnownProxyTypesProvider");
if (first == null)
{
throw new Exception("AppDomainProxyTypeHack expected Microsoft.Xrm.Sdk.KnownProxyTypesProvider.GetInstance(false) to add an Event handler of type Microsoft.Xrm.Sdk.AppDomainBasedKnownProxyTypesProvider to the AssemblyLoad Event, but none was found!");
}
// Remove Event Handler
AppDomain.CurrentDomain.AssemblyLoad -= (AssemblyLoadEventHandler) first;
}
}
}
2 changes: 1 addition & 1 deletion XrmToolBox/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private static void Main(string[] args)
}

SearchAndDestroyPluginsInRootFolder();

AppDomainProxyTypeHack.OverrideAppDomainKnownTypeInitialization();
CopyUpdatedPlugins();

Application.EnableVisualStyles();
Expand Down
3 changes: 2 additions & 1 deletion XrmToolBox/XrmToolBox.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@
<Compile Include="AppCode\PluginControlStatus.cs" />
<Compile Include="AppCode\PluginUpdates.cs" />
<Compile Include="AppCode\WebProxyHelper.cs" />
<Compile Include="AppDomainProxyTypeHack.cs" />
<Compile Include="ConnectionParameterInfo.cs" />
<Compile Include="Forms\NewVersionForm.cs">
<SubType>Form</SubType>
Expand Down Expand Up @@ -339,7 +340,7 @@
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<ProjectExtensions>
<VisualStudio>
<UserProperties BuildVersion_UpdateFileVersion="True" BuildVersion_UpdateAssemblyVersion="True" BuildVersion_BuildVersioningStyle="None.YearStamp.MonthStamp.DayStamp" BuildVersion_ConfigurationName="Release" />
<UserProperties BuildVersion_ConfigurationName="Release" BuildVersion_BuildVersioningStyle="None.YearStamp.MonthStamp.DayStamp" BuildVersion_UpdateAssemblyVersion="True" BuildVersion_UpdateFileVersion="True" />
</VisualStudio>
</ProjectExtensions>
<PropertyGroup>
Expand Down

0 comments on commit b81e1da

Please sign in to comment.