Skip to content

Commit 95e237c

Browse files
committed
Fix exceptions in .NET Standard projects other than .NET Core
The System.Runtime.Loader is supported only for .NET Core despite having .NET Standard target, and was causing TypeInitializationExceptions. All usage of this class was moved to separate util class so this exception can be caught properly. Also it seems that DependencyContext.Default is returning null for other than .NET Core projects so null checks were added as well. In Xamarin.Android project DependencyContext.Default throws exception, instead of returning null, and there is no such thing as BaseDirrectory so another null check is required DEVSIX-1726
1 parent defca47 commit 95e237c

File tree

2 files changed

+120
-25
lines changed

2 files changed

+120
-25
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
3+
This file is part of the iText (R) project.
4+
Copyright (c) 1998-2018 iText Group NV
5+
Authors: Bruno Lowagie, Paulo Soares, et al.
6+
7+
This program is free software; you can redistribute it and/or modify
8+
it under the terms of the GNU Affero General Public License version 3
9+
as published by the Free Software Foundation with the addition of the
10+
following permission added to Section 15 as permitted in Section 7(a):
11+
FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
12+
ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT
13+
OF THIRD PARTY RIGHTS
14+
15+
This program is distributed in the hope that it will be useful, but
16+
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17+
or FITNESS FOR A PARTICULAR PURPOSE.
18+
See the GNU Affero General Public License for more details.
19+
You should have received a copy of the GNU Affero General Public License
20+
along with this program; if not, see http://www.gnu.org/licenses or write to
21+
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22+
Boston, MA, 02110-1301 USA, or download the license from the following URL:
23+
http://itextpdf.com/terms-of-use/
24+
25+
The interactive user interfaces in modified source and object code versions
26+
of this program must display Appropriate Legal Notices, as required under
27+
Section 5 of the GNU Affero General Public License.
28+
29+
In accordance with Section 7(b) of the GNU Affero General Public License,
30+
a covered work must retain the producer line in every PDF that is created
31+
or manipulated using iText.
32+
33+
You can be released from the requirements of the license by purchasing
34+
a commercial license. Buying such a license is mandatory as soon as you
35+
develop commercial activities involving the iText software without
36+
disclosing the source code of your own applications.
37+
These activities include: offering paid services to customers as an ASP,
38+
serving PDFs on the fly in a web application, shipping iText with a closed
39+
source product.
40+
41+
For more information, please contact iText Software Corp. at this
42+
43+
*/
44+
using System;
45+
using System.Collections.Generic;
46+
using System.Reflection;
47+
using System.Text;
48+
49+
namespace iText.IO.Util
50+
{
51+
#if NETSTANDARD1_6
52+
/// <summary>This file is a helper class for internal usage only.</summary>
53+
/// <remarks>
54+
/// This file is a helper class for internal usage only.
55+
/// Be aware that it's API and functionality may be changed in future.
56+
/// <br/>
57+
/// <br/>
58+
/// Ussage of this class may throw TypeInitializationException
59+
/// in .NET Standard enviroments that doesn't support System.Runtime.Loader
60+
/// </remarks>
61+
public static class AssemblyLoadContextUtil {
62+
63+
public static AssemblyName GetAssemblyName(String path) {
64+
return System.Runtime.Loader.AssemblyLoadContext.GetAssemblyName(path);
65+
}
66+
67+
public static Assembly LoadFromDefaultContextAssemblyPath(String path) {
68+
return System.Runtime.Loader.AssemblyLoadContext.Default.LoadFromAssemblyPath(path);
69+
}
70+
71+
public static void RegisterUnloadingEvent(Action<object> onUnloading) {
72+
RegisterUnloadingEvent(onUnloading, null);
73+
}
74+
75+
public static void RegisterUnloadingEvent(Action<object> onUnloading, Assembly assembly) {
76+
if (assembly == null) {
77+
System.Runtime.Loader.AssemblyLoadContext.Default.Unloading += onUnloading;
78+
} else {
79+
System.Runtime.Loader.AssemblyLoadContext.GetLoadContext(assembly).Unloading += onUnloading;
80+
}
81+
}
82+
}
83+
#endif
84+
}

itext/itext.io/itext/io/util/ResourceUtil.cs

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ source product.
4848
using System.Linq;
4949
using System.Reflection;
5050
#if NETSTANDARD1_6
51-
using System.Runtime.Loader;
5251
using Microsoft.DotNet.PlatformAbstractions;
5352
using Microsoft.Extensions.DependencyModel;
5453
#endif
@@ -134,19 +133,23 @@ public static Stream GetResourceStream(string key, Type definedClassType) {
134133
}
135134
}
136135
#else
137-
string runtimeId = RuntimeEnvironment.GetRuntimeIdentifier();
138-
IEnumerable<AssemblyName> loadedAssemblies = DependencyContext.Default.GetRuntimeAssemblyNames(runtimeId).ToList();
139-
foreach (AssemblyName assemblyName in loadedAssemblies) {
140-
if (assemblyName.Name.StartsWith("itext")) {
141-
try {
142-
Assembly assembly = Assembly.Load(assemblyName);
143-
istr = SearchResourceInAssembly(key, assembly);
144-
if (istr != null) {
145-
return istr;
136+
try {
137+
if (DependencyContext.Default != null) {
138+
string runtimeId = RuntimeEnvironment.GetRuntimeIdentifier();
139+
IEnumerable<AssemblyName> loadedAssemblies = DependencyContext.Default.GetRuntimeAssemblyNames(runtimeId).ToList();
140+
foreach (AssemblyName assemblyName in loadedAssemblies) {
141+
if (assemblyName.Name.StartsWith("itext")) {
142+
try {
143+
Assembly assembly = Assembly.Load(assemblyName);
144+
istr = SearchResourceInAssembly(key, assembly);
145+
if (istr != null) {
146+
return istr;
147+
}
148+
} catch { }
146149
}
147-
} catch { }
150+
}
148151
}
149-
}
152+
} catch { }
150153
#endif
151154

152155
return istr;
@@ -170,7 +173,7 @@ private static Stream SearchResourceInAssembly(string key, Object obj) {
170173
#if !NETSTANDARD1_6
171174
istr = Assembly.LoadFrom(dir).GetManifestResourceStream(key);
172175
#else
173-
istr = AssemblyLoadContext.Default.LoadFromAssemblyPath(key).GetManifestResourceStream(key);
176+
istr = AssemblyLoadContextUtil.LoadFromDefaultContextAssemblyPath(key).GetManifestResourceStream(key);
174177
#endif
175178
}
176179
catch
@@ -234,21 +237,29 @@ private static void LoadITextResourceAssemblies() {
234237
}
235238
#else
236239
string runtimeId = RuntimeEnvironment.GetRuntimeIdentifier();
237-
List<AssemblyName> loadedAssemblies = DependencyContext.Default.GetRuntimeAssemblyNames(runtimeId).ToList();
240+
List<AssemblyName> loadedAssemblies = null;
241+
try {
242+
loadedAssemblies = DependencyContext.Default?.GetRuntimeAssemblyNames(runtimeId).ToList();
243+
} catch { }
244+
if (loadedAssemblies == null) {
245+
loadedAssemblies = new List<AssemblyName>();
246+
}
238247

239-
var referencedPaths = Directory.GetFiles(FileUtil.GetBaseDirectory(), "*.dll");
240-
foreach (String path in referencedPaths)
241-
{
242-
try
248+
if (FileUtil.GetBaseDirectory() != null) {
249+
var referencedPaths = Directory.GetFiles(FileUtil.GetBaseDirectory(), "*.dll");
250+
foreach (String path in referencedPaths)
243251
{
244-
AssemblyName name = AssemblyLoadContext.GetAssemblyName(path);
245-
if (iTextResourceAssemblyNames.Contains(name.Name) && !loadedAssemblies.Any(assembly => assembly.Name.Equals(name.Name))) {
246-
Assembly newAssembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(path);
247-
loadedAssemblies.Add(newAssembly.GetName());
252+
try
253+
{
254+
AssemblyName name = AssemblyLoadContextUtil.GetAssemblyName(path);
255+
if (iTextResourceAssemblyNames.Contains(name.Name) && !loadedAssemblies.Any(assembly => assembly.Name.Equals(name.Name))) {
256+
Assembly newAssembly = AssemblyLoadContextUtil.LoadFromDefaultContextAssemblyPath(path);
257+
loadedAssemblies.Add(newAssembly.GetName());
258+
}
259+
}
260+
catch
261+
{
248262
}
249-
}
250-
catch
251-
{
252263
}
253264
}
254265
#endif

0 commit comments

Comments
 (0)