mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-02-16 18:47:57 -05:00
Fix MEF loader issues with framework assemblies (#253)
This is fixing a crashing bug in previous check-in so I'm pushing in. Please leave comments on the commit and email if there is feedback. * Fix MEF loader issues with framework assemblies * Fix broken test from changed behavior * Fix braces to C# convention
This commit is contained in:
@@ -29,6 +29,15 @@ namespace Microsoft.SqlTools.ServiceLayer.Extensibility
|
|||||||
|
|
||||||
public static ExtensionServiceProvider CreateDefaultServiceProvider()
|
public static ExtensionServiceProvider CreateDefaultServiceProvider()
|
||||||
{
|
{
|
||||||
|
// only allow loading MEF dependencies from our assemblies until we can
|
||||||
|
// better seperate out framework assemblies and extension assemblies
|
||||||
|
string[] inclusionList =
|
||||||
|
{
|
||||||
|
"microsoft.sqltools.credentials.dll",
|
||||||
|
"microsoft.sqltools.hosting.dll",
|
||||||
|
"microsoft.sqltools.servicelayer.dll"
|
||||||
|
};
|
||||||
|
|
||||||
string assemblyPath = typeof(ExtensionStore).GetTypeInfo().Assembly.Location;
|
string assemblyPath = typeof(ExtensionStore).GetTypeInfo().Assembly.Location;
|
||||||
string directory = Path.GetDirectoryName(assemblyPath);
|
string directory = Path.GetDirectoryName(assemblyPath);
|
||||||
|
|
||||||
@@ -38,13 +47,29 @@ namespace Microsoft.SqlTools.ServiceLayer.Extensibility
|
|||||||
List<Assembly> assemblies = new List<Assembly>();
|
List<Assembly> assemblies = new List<Assembly>();
|
||||||
foreach (var path in assemblyPaths)
|
foreach (var path in assemblyPaths)
|
||||||
{
|
{
|
||||||
|
// skip DLL files not in inclusion list
|
||||||
|
bool isInList = false;
|
||||||
|
foreach (var item in inclusionList)
|
||||||
|
{
|
||||||
|
if (path.EndsWith(item, StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
isInList = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isInList)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
assemblies.Add(
|
assemblies.Add(
|
||||||
context.LoadFromAssemblyName(
|
context.LoadFromAssemblyName(
|
||||||
AssemblyLoadContext.GetAssemblyName(path)));
|
AssemblyLoadContext.GetAssemblyName(path)));
|
||||||
}
|
}
|
||||||
catch (System.BadImageFormatException)
|
catch (Exception)
|
||||||
{
|
{
|
||||||
// we expect exceptions trying to scan all DLLs since directory contains native libraries
|
// we expect exceptions trying to scan all DLLs since directory contains native libraries
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,12 +42,12 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.Extensibility
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void CreateDefaultServiceProviderShouldFindTypesInAllAssemblies()
|
public void CreateDefaultServiceProviderShouldFindTypesInAllKnownAssemblies()
|
||||||
{
|
{
|
||||||
// Given a default ExtensionServiceProvider
|
// Given a default ExtensionServiceProvider
|
||||||
// Then should not find exports from a different assembly
|
// Then we should not find exports from a test assembly
|
||||||
ExtensionServiceProvider serviceProvider = ExtensionServiceProvider.CreateDefaultServiceProvider();
|
ExtensionServiceProvider serviceProvider = ExtensionServiceProvider.CreateDefaultServiceProvider();
|
||||||
Assert.NotEmpty(serviceProvider.GetServices<MyExportType>());
|
Assert.Empty(serviceProvider.GetServices<MyExportType>());
|
||||||
|
|
||||||
// But should find exports that are defined in the main assembly
|
// But should find exports that are defined in the main assembly
|
||||||
Assert.NotEmpty(serviceProvider.GetServices<ASTNodeFormatterFactory>());
|
Assert.NotEmpty(serviceProvider.GetServices<ASTNodeFormatterFactory>());
|
||||||
|
|||||||
Reference in New Issue
Block a user