Skip to content

Commit c366e3d

Browse files
committed
Add assembly redirects to MSBuild
1 parent 1972c59 commit c366e3d

File tree

4 files changed

+94
-1
lines changed

4 files changed

+94
-1
lines changed

Diff for: src/React.MSBuild/AssemblyBindingRedirect.cs

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright (c) 2017-Present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
using System;
11+
using System.Collections.Generic;
12+
using System.Diagnostics;
13+
using System.IO;
14+
using System.Reflection;
15+
16+
namespace React.MSBuild
17+
{
18+
/// <summary>
19+
/// Hacks around the fact that it's not possible to do assembly binding redirects in MSBuild.
20+
///
21+
/// https://github.com/Microsoft/msbuild/issues/1309
22+
/// http://blog.slaks.net/2013-12-25/redirecting-assembly-loads-at-runtime/
23+
/// </summary>
24+
public static class AssemblyBindingRedirect
25+
{
26+
/// <summary>
27+
/// Redirects that have been configured
28+
/// </summary>
29+
private static readonly Dictionary<string, Version> _redirects = new Dictionary<string, Version>();
30+
31+
static AssemblyBindingRedirect()
32+
{
33+
// This is in a static constructor because it needs to run as early as possible
34+
ConfigureRedirect("JavaScriptEngineSwitcher.Core");
35+
AppDomain.CurrentDomain.AssemblyResolve += ResolveAssembly;
36+
}
37+
38+
/// <summary>
39+
/// Enables assembly binding redirects
40+
/// </summary>
41+
public static void Enable()
42+
{
43+
// Intentionally empty. This is just meant to ensure the static constructor
44+
// has run.
45+
}
46+
47+
/// <summary>
48+
/// Configures a redirect for the specified assembly. Redirects to the version in the bin directory.
49+
/// </summary>
50+
/// <param name="name">Name of the assembly to redirect</param>
51+
private static void ConfigureRedirect(string name)
52+
{
53+
var currentAssemblyPath = Assembly.GetExecutingAssembly().Location;
54+
var redirectAssemblyPath = Path.Combine(
55+
Path.GetDirectoryName(currentAssemblyPath),
56+
name + ".dll"
57+
);
58+
59+
try
60+
{
61+
var realAssembly = Assembly.LoadFile(redirectAssemblyPath);
62+
var version = realAssembly.GetName().Version;
63+
_redirects[name] = version;
64+
}
65+
catch (Exception ex)
66+
{
67+
Trace.WriteLine("Warning: Could not determine version of " + name + " to use! " + ex.Message);
68+
}
69+
}
70+
71+
/// <summary>
72+
/// Overrides assembly resolution to redirect if necessary.
73+
/// </summary>
74+
private static Assembly ResolveAssembly(object sender, ResolveEventArgs args)
75+
{
76+
var requestedAssembly = new AssemblyName(args.Name);
77+
78+
if (_redirects.ContainsKey(requestedAssembly.Name) && requestedAssembly.Version != _redirects[requestedAssembly.Name])
79+
{
80+
requestedAssembly.Version = _redirects[requestedAssembly.Name];
81+
return Assembly.Load(requestedAssembly);
82+
}
83+
return null;
84+
}
85+
}
86+
}

Diff for: src/React.MSBuild/MSBuildHost.cs

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
using System;
1111
using System.Diagnostics;
12+
using System.Reflection;
1213

1314
namespace React.MSBuild
1415
{
@@ -36,6 +37,8 @@ public static bool EnsureInitialized()
3637
/// <returns></returns>
3738
private static bool Initialize()
3839
{
40+
AssemblyBindingRedirect.Enable();
41+
3942
// All "per-request" registrations should be singletons in MSBuild, since there's no
4043
// such thing as a "request"
4144
Initializer.Initialize(requestLifetimeRegistration: registration => registration.AsSingleton());

Diff for: src/React.Sample.Cassette/Web.config

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
</dependentAssembly>
6363
<dependentAssembly>
6464
<assemblyIdentity name="JavaScriptEngineSwitcher.Core" publicKeyToken="c608b2a8cc9e4472" culture="neutral" />
65-
<bindingRedirect oldVersion="0.0.0.0-2.2.0.0" newVersion="2.2.0.0" />
65+
<bindingRedirect oldVersion="0.0.0.0-2.4.9.0" newVersion="2.4.9.0" />
6666
</dependentAssembly>
6767
</assemblyBinding>
6868
</runtime>

Diff for: src/React.Sample.Mvc4/Web.config

+4
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@
9292
<assemblyIdentity name="JavaScriptEngineSwitcher.Msie" publicKeyToken="c608b2a8cc9e4472" culture="neutral" />
9393
<bindingRedirect oldVersion="0.0.0.0-2.3.2.0" newVersion="2.3.2.0" />
9494
</dependentAssembly>
95+
<dependentAssembly>
96+
<assemblyIdentity name="MsieJavaScriptEngine" publicKeyToken="a3a2846a37ac0d3e" culture="neutral" />
97+
<bindingRedirect oldVersion="0.0.0.0-2.2.2.0" newVersion="2.2.2.0" />
98+
</dependentAssembly>
9599
</assemblyBinding>
96100
</runtime>
97101

0 commit comments

Comments
 (0)