Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could not load file or assembly 'Microsoft.VisualStudio.ArchitectureTools.PEReader - 16.0.2-preview #2008

Closed
tim-arheit opened this issue May 7, 2019 · 34 comments
Labels

Comments

@tim-arheit
Copy link

tim-arheit commented May 7, 2019

Description

When running the unit tests in an Azure devops build pipeline "(https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/test/vstest?view=azure-devops) using 16.0.2-preview
Many of the unit tests throw the error:

System.AggregateException : One or more errors occurred. (The type initializer for 'Velocity.Common.Processors.Base.Common.ProcessorBaseFactory' threw an exception.)
----> System.TypeInitializationException : The type initializer for 'Velocity.Common.Processors.Base.Common.ProcessorBaseFactory' threw an exception.
----> System.Reflection.ReflectionTypeLoadException : Unable to load one or more of the requested types.
Could not load file or assembly 'Microsoft.VisualStudio.ArchitectureTools.PEReader, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
----> System.IO.FileNotFoundException : Could not load file or assembly 'Microsoft.VisualStudio.ArchitectureTools.PEReader, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.

Reverting back to 16.0.1 solves the issue.

Steps to reproduce

Run unit tests in Azure Devops (Visual Studio Test), after running 'Visual Studio Test Platform Installer' set to Latest (Includes-Pre-release) which installs and causes tests to run on 16.0.2-preview

Expected behavior

No error

Actual behavior

Could not load file or assembly 'Microsoft.VisualStudio.ArchitectureTools.PEReader

Environment

Azure DevOps, Visual Studio 2017 Pro, Windows Server 2016

@singhsarab
Copy link
Contributor

@tim-arheit
Is that the complete stack trace?
Please run the build with System.Debug= true, share the logs with us.
Is it possible for you share some a repro project/link to pipeline with us?

@tim-arheit
Copy link
Author

I have found the offending code which is suppose to load a list of types that implement a particular interface:
`Type type = typeof(ICommand);

        IEnumerable<Type> types = AppDomain.CurrentDomain.GetAssemblies()
            .SelectMany(s => s.GetTypes())
            .Where(p => type.IsAssignableFrom(p) && p.IsClass);`

It works in production, but when running unit tests on 16.0.2 or 16.1.0 it is failing when calling GetTypes on 'Microsoft.VisualStudio.ArchitectureTools.PEReader'. throwing the error:
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.VisualStudio.ArchitectureTools.PEReader, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
File name: 'Microsoft.VisualStudio.ArchitectureTools.PEReader, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

I don't know what changed with the test runner to cause this, but the simple fix is to filter out the assemblies we are calling GetTypes on first.

Thanks

@singhsarab
Copy link
Contributor

singhsarab commented May 9, 2019

@tim-arheit I cannot think of anything that seems related to this, with respect to changes in the test platform.

From your comment I am inferring that this was something in your code that you have fixed now. Is that correct?

@valentinacupac
Copy link

valentinacupac commented May 21, 2019

@singhsarab, today I got the same error.

My tests were working in the past, but today after I updated my Visual Studio to the latest version (I have VS 2019 and updated it today to Version 16.1, and after that update, my tests started failing with the same error.

In my code I also have the line of code, which I used to tell AutoMapper to scan all asemblies (in the past it worked):

AppDomain.CurrentDomain.GetAssemblies().

As can be seen below, error is same as posted by @tim-arheit above:

System.Reflection.ReflectionTypeLoadException : Unable to load one or more of the requested types.
Could not load file or assembly 'Microsoft.VisualStudio.ArchitectureTools.PEReader, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.

Thanks,
Valentina

Result StackTrace:	
----- Inner Stack Trace #1 (System.Reflection.ReflectionTypeLoadException) -----
   at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
   at System.Reflection.RuntimeAssembly.get_DefinedTypes()
   at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.ToArray()
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at AutoMapper.ServiceCollectionExtensions.AddAutoMapperClasses(IServiceCollection services, Action`2 configAction, IEnumerable`1 assembliesToScan, ServiceLifetime serviceLifetime) in C:\projects\automapper-extensions-microsoft-dependencyinjectio\src\AutoMapper.Extensions.Microsoft.DependencyInjection\ServiceCollectionExtensions.cs:line 82
   at Optivem.NorthwindLite.Web.Startup.ConfigureServices(IServiceCollection services) in D:\GitHub\optivem\framework-dotnetcore\samples\NorthwindLite\src\Optivem.NorthwindLite.Web\Startup.cs:line 82
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices()
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.Initialize()
   at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
   at Microsoft.AspNetCore.TestHost.TestServer..ctor(IWebHostBuilder builder, IFeatureCollection featureCollection)
   at Optivem.Test.Xunit.AspNetCore.BaseTestClient`1..ctor() in D:\GitHub\optivem\framework-dotnetcore\test\Base\Xunit.AspNetCore\BaseTestClient.cs:line 18
   at Optivem.Test.Xunit.AspNetCore.BaseTestJsonClient`1..ctor()
   at Optivem.NorthwindLite.Web.Test.Fixture.TestClient..ctor() in D:\GitHub\optivem\framework-dotnetcore\samples\NorthwindLite\test\Optivem.NorthwindLite.Web.Test\Fixture\TestClient.cs:line 19
----- Inner Stack Trace #2 (Xunit.Sdk.TestClassException) -----
Result Message:	
System.AggregateException : One or more errors occurred. (Unable to load one or more of the requested types.
Could not load file or assembly 'Microsoft.VisualStudio.ArchitectureTools.PEReader, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.) (The following constructor parameters did not have matching fixture data: TestClient client)
---- System.Reflection.ReflectionTypeLoadException : Unable to load one or more of the requested types.
Could not load file or assembly 'Microsoft.VisualStudio.ArchitectureTools.PEReader, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
---- The following constructor parameters did not have matching fixture data: TestClient client

@dukeofharen
Copy link

@singhsarab, today I got the same error.

My tests were working in the past, but today after I updated my Visual Studio to the latest version (I have VS 2019 and updated it today to Version 16.1, and after that update, my tests started failing with the same error.

In my code I also have the line of code, which I used to tell AutoMapper to scan all asemblies (in the past it worked):

AppDomain.CurrentDomain.GetAssemblies().

As can be seen below, error is same as posted by @tim-arheit above:

System.Reflection.ReflectionTypeLoadException : Unable to load one or more of the requested types.
Could not load file or assembly 'Microsoft.VisualStudio.ArchitectureTools.PEReader, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.

Thanks,
Valentina

Result StackTrace:	
----- Inner Stack Trace #1 (System.Reflection.ReflectionTypeLoadException) -----
   at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
   at System.Reflection.RuntimeAssembly.get_DefinedTypes()
   at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.ToArray()
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at AutoMapper.ServiceCollectionExtensions.AddAutoMapperClasses(IServiceCollection services, Action`2 configAction, IEnumerable`1 assembliesToScan, ServiceLifetime serviceLifetime) in C:\projects\automapper-extensions-microsoft-dependencyinjectio\src\AutoMapper.Extensions.Microsoft.DependencyInjection\ServiceCollectionExtensions.cs:line 82
   at Optivem.NorthwindLite.Web.Startup.ConfigureServices(IServiceCollection services) in D:\GitHub\optivem\framework-dotnetcore\samples\NorthwindLite\src\Optivem.NorthwindLite.Web\Startup.cs:line 82
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices()
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.Initialize()
   at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
   at Microsoft.AspNetCore.TestHost.TestServer..ctor(IWebHostBuilder builder, IFeatureCollection featureCollection)
   at Optivem.Test.Xunit.AspNetCore.BaseTestClient`1..ctor() in D:\GitHub\optivem\framework-dotnetcore\test\Base\Xunit.AspNetCore\BaseTestClient.cs:line 18
   at Optivem.Test.Xunit.AspNetCore.BaseTestJsonClient`1..ctor()
   at Optivem.NorthwindLite.Web.Test.Fixture.TestClient..ctor() in D:\GitHub\optivem\framework-dotnetcore\samples\NorthwindLite\test\Optivem.NorthwindLite.Web.Test\Fixture\TestClient.cs:line 19
----- Inner Stack Trace #2 (Xunit.Sdk.TestClassException) -----
Result Message:	
System.AggregateException : One or more errors occurred. (Unable to load one or more of the requested types.
Could not load file or assembly 'Microsoft.VisualStudio.ArchitectureTools.PEReader, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.) (The following constructor parameters did not have matching fixture data: TestClient client)
---- System.Reflection.ReflectionTypeLoadException : Unable to load one or more of the requested types.
Could not load file or assembly 'Microsoft.VisualStudio.ArchitectureTools.PEReader, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
---- The following constructor parameters did not have matching fixture data: TestClient client

I've also just updated Visuals Studio 2019 and also got this exception when running my unit tests. Most of them ran fine, but I have some integration tests where I query the assemblies in the AppDomain for adding them to my IServiceCollection. For some reason, when running the unit tests from within Visual Studio, my code tries to get the types within the namespace Microsoft.VisualStudio.ArchitectureTools.PEReader, but the file can't be loaded. I've circumvented this issue by explicitly filtering the assemblies on a substring. Now, it works fine. (still, a dirty fix for something that shouldn't be a problem at all, but I'm happy it works.

My broken code:

public static IEnumerable<Type> GetImplementations<TInterface>()
{
    var types = AppDomain.CurrentDomain
        .GetAssemblies()
        .SelectMany(s => s.GetTypes())
        .Where(p => typeof(TInterface).IsAssignableFrom(p) && !p.IsInterface && !p.IsAbstract)
        .ToArray();

    return types;
}

My working code:

public static IEnumerable<Type> GetImplementations<TInterface>(string assemblyFilter = "")
{
    var assemblies = AppDomain.CurrentDomain
        .GetAssemblies();
    if (!string.IsNullOrWhiteSpace(assemblyFilter))
    {
        assemblies = assemblies.Where(a => a.FullName.Contains(assemblyFilter)).ToArray();
    }

    var types = assemblies
        .SelectMany(s => s.GetTypes())
        .Where(p => typeof(TInterface).IsAssignableFrom(p) && !p.IsInterface && !p.IsAbstract)
        .ToArray();

    return types;
}

@valentinacupac
Copy link

Based on all the above, it seems that the quick fix in all 3 cases seems to be additional filtering of assemblies to prevent scanning the assemblies that are referencing Microsoft.VisualStudio.ArchitectureTools.PEReader, Version=16.0.0.0

The interesting question is why it's searching for Microsoft.VisualStudio.ArchitectureTools.PEReader Version=16.0.0.0.... Perhaps that's why the tests work with VS 16.0 and tests fail with VS 16.1...

@kasnti
Copy link

kasnti commented May 22, 2019

I also got the same exception,AutoMapper's dependency injection test can work in VS16.0, but there is a problem in VS16.1.
I have the line of code, which I used to tell AutoMapper to scan all asemblies (in the past it worked):
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
so Instead of scanning all asemblies, I specified to provide the assembly containing the 'Profile' to AutoMapper:
services.AddAutoMapper(Assembly.Load("Models"));
The "Models" project contains AutoMapper Profile configuration files.
it works fine.

@StevenRasmussen
Copy link

Just wanted to add that I'm now getting this exception as well. Same scenario where I am scanning all assemblies and their types in a unit test.

@benizri-ofir
Copy link

the same issue for me, the unit-test used to pass before moving to visual studio 2019 16.1.1

my code:
m_fieldValidatorTypes = AppDomain.CurrentDomain.GetAssemblies() .SelectMany(s => s.GetTypes()) .Where(t => type.IsAssignableFrom(t) && !t.IsAbstract && !t.IsInterface) .ToDictionary(ft => ft.FullName, ft => ft);

getting this exception:
System.Reflection.ReflectionTypeLoadException: 'Unable to load one or more of the requested types. Could not load file or assembly 'Microsoft.VisualStudio.ArchitectureTools.PEReader, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.'

@EskilSaatvedt
Copy link

We have the same problem. It was mentioned in the release notes for 16.1.1 but is persisting. Rollback to visual studio 2017 works for now.

vip32 added a commit to vip32/FunctionMonkey that referenced this issue May 29, 2019
the new vs2019 (16.2.0) test explorer has an issue with GetAssemblies when it tries to load internal test assemblies
microsoft/vstest#2008

with this fix the Microsoft assemblies are not loaded when an integration test runs (FunctionMonkey.Testing.AbstractAcceptanceTest)
@StevenRasmussen
Copy link

@EskilSaatvedt - I'm not seeing it anywhere in the release notes for 16.1.1. I am hoping that a fix makes it into 16.1.2 or 16.2 though.

@olivermue
Copy link

Just ran into the same error after update of Visual Studio to the latest version. After some search I found, that all the failing tests are doing a scan on the loaded assemblies of the application domain. After digging a little deeper, the following code produces the error:

[Fact]
public void TryToAccessTypesOfTraceCollectorShouldNotFail()
{
    var failingAssembly = AppDomain.CurrentDomain.GetAssemblies()
        .FirstOrDefault(assembly => assembly.FullName.StartsWith("Microsoft.VisualStudio.TraceDataCollector"));

    if (failingAssembly != null)
    {
        var typeInfos = failingAssembly.DefinedTypes;
    }
}

Be aware that this error really only occurs if you run the test within Visual Studio 16.1.1 through the Test Explorer.

@olivermue
Copy link

And just for the people searching for an easy to use solution for their tests. Replace

AppDomain.CurrentDomain.GetAssemblies()

with

AppDomain.CurrentDomain.GetAssemblies().Where(assembly => !assembly.FullName.StartsWith("Microsoft.VisualStudio.TraceDataCollector"))

Maybe you get an error, cause the return value of this call is changed to IEnumerable<Assembly> instead of being Assembly[]. Perhaps a good day to make the corresponding functions less demanding and changing their signatures accordingly. If you really need that array then add .ToArray().

gyuwon added a commit to loom/loom-dotnet that referenced this issue May 31, 2019
Bypass the problem that the test runner cannot load types from assembly
'Microsoft.VisualStuduo.TraceDataCollector'.

microsoft/vstest#2008
dukeofharen pushed a commit to dukeofharen/httplaceholder that referenced this issue May 31, 2019
- Change logo in dark and light mode.
- (Sadly) stupid bug fix to fix the integration unit tests. See microsoft/vstest#2008
- Description for all API endpoints for Swagger file.
gyuwon added a commit to loom/loom-dotnet that referenced this issue Jun 2, 2019
Bypass the problem that the test runner cannot load types from assembly
'Microsoft.VisualStuduo.TraceDataCollector'.

microsoft/vstest#2008
@cltshivash cltshivash added the bug label Jun 4, 2019
@xygon
Copy link

xygon commented Jun 5, 2019

I am also getting this error when running tests after updating to Visual Studio 16.1.1

The code under test is using mediatr and that is scanning assemblies looking for classes that implement specific interfaces an that is where the error gets thrown.

The tests run fine from the command line.

@olivermue
Copy link

@xygon When initializing MediatR in asp core you use the extension method services.AddMediatR(). This will scan the whole application domain. Instead call services.AddMediatR(assemblies) and for the assemblies variable you provide the list of assemblies excluding the TraceDataCollector like shown in my answer.

@olivermue
Copy link

Just updated to 16.1.2 and tried again. Unfortunately the error is NOT fixed. So hoping for the next version.

@TheMadStrater
Copy link

Just updated to 16.1.2 and tried again. Unfortunately the error is NOT fixed. So hoping for the next version.

I agree, this issue persists!

@vagisha-nidhi
Copy link
Contributor

@olivermue Can you please check if the issue still persists with 16.2.0-preview-20190606-02?

@olivermue
Copy link

olivermue commented Jun 11, 2019

Took my unit test from above and created a new xUnit test project with VS Community 16.2.0 Preview 1.0. The error is still:

System.Reflection.ReflectionTypeLoadException : Unable to load one or more of the requested types.
    Could not load file or assembly 'Microsoft.VisualStudio.ArchitectureTools.PEReader, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Das System kann die angegebene Datei nicht finden.

@vagisha-nidhi
Copy link
Contributor

@olivermue The fix will be available in VS 16.2.0 Preview 3.
Meanwhile, if you are running the unit tests in an Azure devops build pipeline, you can simply select 16.2.0-preview-20190606-02 version in the tools installer.

@divinci
Copy link

divinci commented Jun 20, 2019

Perhaps a good day to make the corresponding functions less demanding and changing their signatures accordingly.

Yes @olivermue !

@vipetrul
Copy link

Getting the same exception when Debugging NUnit tests that utilize TestCaseSource attribute.

@olivermue
Copy link

Tested with VS Community 16.2.0 Preview 3.0 and the error is FIXED.

@singhsarab
Copy link
Contributor

@olivermue Thanks for confirming.
Thanks everyone for your patience.

@timmsch
Copy link

timmsch commented Jul 22, 2019

The issue still seems to exist, even when using the Visual Studio Test Platform Installer.
Version 16.2.0 (stable)
Error details: Could not load file or assembly 'Microsoft.TeamFoundation.TestManagement.Controller, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

@jjxtra
Copy link

jjxtra commented Jul 22, 2019

Why is this closed? I get this error in latest visual studio 2019 when running unit tests, calling assembly.GetTypes()... No such issue in VS 2017.

Should not be closed until a non-preview release is out that fixes the issue.

@jjxtra
Copy link

jjxtra commented Jul 22, 2019

/// <summary>
/// Get all types from all assemblies
/// </summary>
/// <returns>List of all types</returns>
public static List<Type> GetAllTypes()
{
    Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
    List<Type> allTypes = new List<Type>();
    foreach (Assembly assembly in assemblies)
    {
        try
        {
            // some assemblys throw in unit tests in VS 2019, bug in MSFT...
            allTypes.AddRange(assembly.GetTypes());
        }
        catch
        {
        }
    }
    return allTypes;
}

@jannikbuschke
Copy link

I ran into this issue with the following code

services.AddAutoMapper(cfg => {  
  cfg.AddCollectionMappers();
});

Explicitly adding the assembly fixed it for me:

services.AddAutoMapper(cfg => {
  cfg.AddCollectionMappers();
}, typeof(Startup).Assembly);

VS 16.1.5
dotnetcore 2.2

@bencollinsuk
Copy link

I still get this problem - it prevents my unit tests from running.

Microsoft Visual Studio Enterprise 2019
Version 16.1.6
VisualStudio.16.Release/16.1.6+29102.190

@olivermue
Copy link

olivermue commented Aug 14, 2019

Yes, in Enterprise this error still exists (just tested it again, also with 16.1.6). Due to the fact, that it is solved in VS Community 16.2.0 Preview 3.0 I would guess we have to wait till the Enterprise Version 16.2.x arrives.

And in case you use Azure DevOps, you have to wait till Microsoft releases a new hosted agent image. Depending on how Microsoft names this agent, you then have also to configure your build pipeline to use this new version, but after taking a look at the Microsoft docs, changes are high, that they make an in-place update but you never know (and especially you don't know when).

So my current advice would be to implement my suggested workaround above and re-try it at the end of the year to check if the error still persists. If not revert the workaround and if yes make the next try two month later.

@pwen090
Copy link

pwen090 commented Aug 19, 2019

I am also not clear on what version fixes this and agree this should not have been closed under a preview release. Is there any confirmation that Visual Studio Professional 2019 version 16.2.2 fixes this issue?

@TheMadStrater
Copy link

This isn't fixed on any of my versions of VS...not sure why this isn't a trivial fix?!?

@xygon
Copy link

xygon commented Aug 20, 2019

I have Visual Studio Professional 16.2.2 and the issue is fixed.

@olivermue
Copy link

Just installed Visual Studio Enterprise 2019 Version 16.2.3 and I can confirm the error is fixed. So the only bogus version is within Azure DevOps the agent pool image Hosted Windows 2019 with VS2019 image, cause it contains VisualStudio/16.1.6+29102.190

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests