Skip to content

Commit

Permalink
Add assembly redirects to MSBuild
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel15 committed Jul 2, 2017
1 parent 1972c59 commit c366e3d
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 1 deletion.
86 changes: 86 additions & 0 deletions src/React.MSBuild/AssemblyBindingRedirect.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright (c) 2017-Present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;

namespace React.MSBuild
{
/// <summary>
/// Hacks around the fact that it's not possible to do assembly binding redirects in MSBuild.
///
/// https://github.com/Microsoft/msbuild/issues/1309
/// http://blog.slaks.net/2013-12-25/redirecting-assembly-loads-at-runtime/
/// </summary>
public static class AssemblyBindingRedirect
{
/// <summary>
/// Redirects that have been configured
/// </summary>
private static readonly Dictionary<string, Version> _redirects = new Dictionary<string, Version>();

static AssemblyBindingRedirect()
{
// This is in a static constructor because it needs to run as early as possible
ConfigureRedirect("JavaScriptEngineSwitcher.Core");
AppDomain.CurrentDomain.AssemblyResolve += ResolveAssembly;
}

/// <summary>
/// Enables assembly binding redirects
/// </summary>
public static void Enable()
{
// Intentionally empty. This is just meant to ensure the static constructor
// has run.
}

/// <summary>
/// Configures a redirect for the specified assembly. Redirects to the version in the bin directory.
/// </summary>
/// <param name="name">Name of the assembly to redirect</param>
private static void ConfigureRedirect(string name)
{
var currentAssemblyPath = Assembly.GetExecutingAssembly().Location;
var redirectAssemblyPath = Path.Combine(
Path.GetDirectoryName(currentAssemblyPath),
name + ".dll"
);

try
{
var realAssembly = Assembly.LoadFile(redirectAssemblyPath);
var version = realAssembly.GetName().Version;
_redirects[name] = version;
}
catch (Exception ex)
{
Trace.WriteLine("Warning: Could not determine version of " + name + " to use! " + ex.Message);
}
}

/// <summary>
/// Overrides assembly resolution to redirect if necessary.
/// </summary>
private static Assembly ResolveAssembly(object sender, ResolveEventArgs args)
{
var requestedAssembly = new AssemblyName(args.Name);

if (_redirects.ContainsKey(requestedAssembly.Name) && requestedAssembly.Version != _redirects[requestedAssembly.Name])
{
requestedAssembly.Version = _redirects[requestedAssembly.Name];
return Assembly.Load(requestedAssembly);
}
return null;
}
}
}
3 changes: 3 additions & 0 deletions src/React.MSBuild/MSBuildHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

using System;
using System.Diagnostics;
using System.Reflection;

namespace React.MSBuild
{
Expand Down Expand Up @@ -36,6 +37,8 @@ public static bool EnsureInitialized()
/// <returns></returns>
private static bool Initialize()
{
AssemblyBindingRedirect.Enable();

// All "per-request" registrations should be singletons in MSBuild, since there's no
// such thing as a "request"
Initializer.Initialize(requestLifetimeRegistration: registration => registration.AsSingleton());
Expand Down
2 changes: 1 addition & 1 deletion src/React.Sample.Cassette/Web.config
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="JavaScriptEngineSwitcher.Core" publicKeyToken="c608b2a8cc9e4472" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.2.0.0" newVersion="2.2.0.0" />
<bindingRedirect oldVersion="0.0.0.0-2.4.9.0" newVersion="2.4.9.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
Expand Down
4 changes: 4 additions & 0 deletions src/React.Sample.Mvc4/Web.config
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@
<assemblyIdentity name="JavaScriptEngineSwitcher.Msie" publicKeyToken="c608b2a8cc9e4472" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.3.2.0" newVersion="2.3.2.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="MsieJavaScriptEngine" publicKeyToken="a3a2846a37ac0d3e" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.2.2.0" newVersion="2.2.2.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>

Expand Down

0 comments on commit c366e3d

Please sign in to comment.