Skip to content

Commit

Permalink
Adding logging to identify when shared assemblies are resolved from p…
Browse files Browse the repository at this point in the history
…roviders
  • Loading branch information
fabiocav committed Dec 6, 2018
1 parent 11b9c3e commit 610011d
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Reflection;
using System.Runtime.Loader;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;

namespace Microsoft.Azure.WebJobs.Script.Description
{
Expand All @@ -17,10 +18,12 @@ public DirectSharedAssemblyProvider(Assembly assembly)
_assembly = assembly;
}

public bool TryResolveAssembly(string assemblyName, AssemblyLoadContext targetContext, out Assembly assembly)
public bool TryResolveAssembly(string assemblyName, AssemblyLoadContext targetContext, ILogger logger, out Assembly assembly)
{
if (string.Compare(AssemblyNameCache.GetName(_assembly).Name, assemblyName, StringComparison.OrdinalIgnoreCase) == 0)
{
logger.LogInformation($"{nameof(DirectSharedAssemblyProvider)} resolved shared assembly '{assemblyName}'");

assembly = _assembly;
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Runtime.Loader;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Azure.WebJobs.Script.Extensibility;
using Microsoft.Extensions.Logging;

namespace Microsoft.Azure.WebJobs.Script.Description
{
Expand All @@ -27,7 +28,7 @@ public ExtensionSharedAssemblyProvider(ICollection<IScriptBindingProvider> bindi
_bindingProviders = bindingProviders ?? throw new ArgumentNullException(nameof(bindingProviders));
}

public bool TryResolveAssembly(string assemblyName, AssemblyLoadContext targetContext, out Assembly assembly)
public bool TryResolveAssembly(string assemblyName, AssemblyLoadContext targetContext, ILogger logger, out Assembly assembly)
{
assembly = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@

using System.Reflection;
using System.Runtime.Loader;
using Microsoft.Extensions.Logging;

namespace Microsoft.Azure.WebJobs.Script.Description
{
internal interface ISharedAssemblyProvider
{
bool TryResolveAssembly(string assemblyName, AssemblyLoadContext targetContext, out Assembly assembly);
bool TryResolveAssembly(string assemblyName, AssemblyLoadContext targetContext, ILogger logger, out Assembly assembly);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;
using System.Text.RegularExpressions;
using Microsoft.Extensions.Logging;

namespace Microsoft.Azure.WebJobs.Script.Description
{
Expand Down Expand Up @@ -45,7 +45,7 @@ private static string GetPrivateBinPath()
return binPath ?? AppDomain.CurrentDomain.BaseDirectory;
}

public bool TryResolveAssembly(string assemblyName, AssemblyLoadContext targetContext, out Assembly assembly)
public bool TryResolveAssembly(string assemblyName, AssemblyLoadContext targetContext, ILogger logger, out Assembly assembly)
{
assembly = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Scripting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;

namespace Microsoft.Azure.WebJobs.Script.Description
{
Expand Down Expand Up @@ -80,7 +81,7 @@ public ScriptFunctionMetadataResolver(string scriptFilePath, ICollection<IScript
var scriptResolver = ScriptMetadataResolver.Default.WithSearchPaths(_privateAssembliesPath);
_scriptResolver = new CacheMetadataResolver(scriptResolver);
_extensionSharedAssemblyProvider = new ExtensionSharedAssemblyProvider(bindingProviders);
_logger = logger;
_logger = logger ?? NullLogger.Instance;
}

public ScriptOptions CreateScriptOptions()
Expand Down Expand Up @@ -165,8 +166,8 @@ public override ImmutableArray<PortableExecutableReference> ResolveReference(str
{
Assembly assembly = null;

if (SharedAssemblyProviders.Any(p => p.TryResolveAssembly(reference, AssemblyLoadContext.Default, out assembly)) ||
_extensionSharedAssemblyProvider.TryResolveAssembly(reference, AssemblyLoadContext.Default, out assembly))
if (SharedAssemblyProviders.Any(p => p.TryResolveAssembly(reference, AssemblyLoadContext.Default, _logger, out assembly)) ||
_extensionSharedAssemblyProvider.TryResolveAssembly(reference, AssemblyLoadContext.Default, _logger, out assembly))
{
result = ImmutableArray.Create(MetadataReference.CreateFromFile(assembly.Location));
}
Expand Down Expand Up @@ -231,7 +232,7 @@ public Assembly ResolveAssembly(AssemblyName assemblyName, FunctionAssemblyLoadC
}
else
{
_extensionSharedAssemblyProvider.TryResolveAssembly(assemblyName.FullName, FunctionAssemblyLoadContext.Shared, out assembly);
_extensionSharedAssemblyProvider.TryResolveAssembly(assemblyName.FullName, FunctionAssemblyLoadContext.Shared, _logger, out assembly);
}

return assembly;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.Azure.WebJobs.Script.Description;
using Microsoft.Azure.WebJobs.Script.Extensibility;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Newtonsoft.Json.Linq;
using Xunit;
Expand All @@ -26,7 +27,7 @@ public void TryResolveAssembly_ResolvesProviderAssembly()
var provider = new ExtensionSharedAssemblyProvider(bindingProviders);

Assembly assembly;
bool result = provider.TryResolveAssembly(typeof(TestBindingProvider).Assembly.GetName().Name, AssemblyLoadContext.Default, out assembly);
bool result = provider.TryResolveAssembly(typeof(TestBindingProvider).Assembly.GetName().Name, AssemblyLoadContext.Default, NullLogger.Instance, out assembly);

Assert.True(result);
Assert.NotNull(assembly);
Expand Down

0 comments on commit 610011d

Please sign in to comment.