Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// ------------------------------------------------------------

using System;

namespace Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.AssemblyLoader
{

#if NETFRAMEWORK
/// <summary>
/// Creates a separate app domain.
/// </summary>
/// <typeparam name="T">The type for which instance needs to be created in new app domain.</typeparam>
internal sealed class AppDomainCreator<T> : MarshalByRefObject, IDisposable
{
/// <summary>
/// Initializes new instance of <see cref="AppDomainCreator{T}"/>.
/// </summary>
public AppDomainCreator()
{
var setupInformation = AppDomain.CurrentDomain.SetupInformation;

// Specify that the new domain should make a copy to the shadow directory, preventing
// the locking of the assemblies.
setupInformation.ShadowCopyFiles = "true";

// The privateBinPath is a ; separated list of paths located in the base path of the
// application where the CLR will attempt to locate assemblies during the load process.
// Here we add the location where we will copy dlls.
setupInformation.PrivateBinPath += ";DefaultGenerationBin";

// Setup the domain.
Domain = AppDomain.CreateDomain(
"AppDomain" + Guid.NewGuid(),
AppDomain.CurrentDomain.Evidence,
setupInformation );

var type = typeof( T );

Object = (T) Domain.CreateInstanceAndUnwrap( type.Assembly.FullName, type.FullName );
}

/// <summary>
/// Gets the <see cref="AppDomain"/>.
/// </summary>
public AppDomain Domain { get; private set; }

/// <summary>
/// Gets the instance of the object created in the new app domain.
/// </summary>
public T Object { get; }

/// <summary>
/// Unloads the app domain.
/// </summary>
public void Dispose()
{
if ( Domain != null )
{
AppDomain.Unload( Domain );
Domain = null;
}
}
}
#endif
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// ------------------------------------------------------------

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

namespace Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.AssemblyLoader
{
/// <summary>
/// Contains reusable methods for loading assembly.
/// </summary>
internal static class AssemblyLoadUtility
{
/// <summary>
/// Try load the assembly by using provided assembly by name and version if provided.
/// </summary>
/// <param name="assemblyPaths">The assembly paths.</param>
/// <param name="assemblyName">The assembly name to load.It's not assembly full name.</param>
/// <param name="assemblyVersion">The assembly version to load.</param>
/// <returns>The assembly if able to load, otherwise null.</returns>
internal static Assembly TryLoadByNameOrVersion(
IList<string> assemblyPaths,
string assemblyName,
string assemblyVersion = null)
{
var assemblyPath = assemblyPaths.FirstOrDefault(path => path.EndsWith(assemblyName + ".dll"));

@JoshuaWhatley Joshua Whatley (JoshuaWhatley) May 9, 2019 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EndsWith? wouldn't we want an exact match? #WontFix

@JoshuaWhatley Joshua Whatley (JoshuaWhatley) May 10, 2019 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that the assemblyName passed in is not necessarily the full assemblyName. Could you update the param description so that this info is reflected. #Resolved

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

b/c we are checking against assembly path, so path would be like //test/test.dll and we watch to match test.dll with the path, so ends with.


In reply to: 282706378 [](ancestors = 282706378)


if (!string.IsNullOrWhiteSpace(assemblyPath))
{
if(!string.IsNullOrWhiteSpace(assemblyVersion))
{
try
{
var info = FileVersionInfo.GetVersionInfo(assemblyPath);
if (info.FileVersion.StartsWith(assemblyVersion))
{
return Assembly.LoadFrom(assemblyPath);
}
}
catch (Exception)
{
// Do nothing.
}
}
else
{
return Assembly.LoadFrom(assemblyPath);
}

}

return null;
}
}
}
Loading