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

Dotnet Core test executed in Visual Studio 2017 - Assembly.Load() is incorrectly able to load assemblies from Visual Studio Extensions #632

Closed
jonstelly opened this issue Mar 18, 2017 · 13 comments
Assignees

Comments

@jonstelly
Copy link

jonstelly commented Mar 18, 2017

Description

Attempting to execute dotnet core unit/integration tests that use RavenDB 3.5.3 client. These tests execute and pass using the dotnet CLI, or the ReSharper Test Runner in Visual Studio, but using the Test Explorer, they fail with an error about log4net and AppDomain.add_ProcessExit:

Message: Test method ProcessExitException.RavenException.InitializeStore threw exception: System.TypeInitializationException: The type initializer for 'log4net.Core.LoggerManager' threw an exception. ---> System.MethodAccessException: Attempt by method 'log4net.Core.LoggerManager.RegisterAppDomainEvents()' to access method 'System.AppDomain.add_ProcessExit(System.EventHandler)' failed.

From debugging, I found that the ravendb client attempts to load log4net using Assembly.Load(...). If it's able to load then it uses that library for logging. The issue is that it's finding log4net from the directory of an installed Visual Studio extension. When debugging, I can see that the CodeBase for the log4net assembly loaded by my test is:

file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/Common7/Ide/Extensions/XSNRCEZZ.l1M/log4net.dll

I don't believe test code should be able to load assemblies from extensions loaded into Visual Studio, and I bet in this case, this log4net assembly is built against the full runtime, and not dotnet core or one of the netstandard targets. I believe that explains the message I included above.

This extension directory is the powershell tools for Visual Studio. When I removed that component for Visual Studio, the tests started passing.

Steps to reproduce

  1. Install Visual Studio 2017 and include the PowerShell component when installing
  2. Extract and load the following project
    ProcessExitException.zip
  3. Run the RavenException.InitializeStore test inside Visual Studio

Expected behavior

The test should pass

Actual behavior

It fails as noted above

Environment

Windows 10 15025+
Visual Studio 2017 (with the PowerShell component installed)

Workaround

Remove the PowerShell component (and probably also Xamarin/Mobile tools, I see log4net.dll in that extension also)

@jonstelly
Copy link
Author

jonstelly commented Mar 18, 2017

image
This screenshot was taken during the execution of the tests before I removed PowerShell and Xamarin.

@jonstelly
Copy link
Author

Is this issue not clearly worded? Just wondering since it doesn't seem to have been triaged. To try and simplify I've attached a simpler test that doesn't use the ravendb client.

AssemblyLoadExample.zip

[TestClass]
public class AssemblyLoadTests
{
    [TestMethod]
    public void LoadLogForNet()
    {
        Assert.ThrowsException<FileNotFoundException>(() =>
        {
            var log = Assembly.Load(new AssemblyName("log4net"));
        });
    }
}

This test project has no reference to log4net, so this test should pass. But if the 'PowerShell Tools' extension is installed in Visual Studio, the log4net assembly is found in the extension directory mentioned above, and the assembly is loaded (though it's not even a dotnet core/standard assembly, so any usage of that library fails).

A test passing or failing should not depend on an unrelated Visual Studio extension being installed or not. During test execution, assemblies should not be loaded from extension directories.

@AbhitejJohn
Copy link
Contributor

Interesting. I guess this is happening because of the powershell tools test adapter that the test platforms assembly resolution is automatically pointing to. Since this is .Net core there isn't a separate appdomain where tests are loaded and run from. It looks like the test adapter assembly resolver is creeping into the test run. We need to turn it off after the adapter is loaded.

@spati2
Copy link

spati2 commented Jun 6, 2017

I am facing a similar issue. What is the resolution here?

@himanshu-trivedi-spider
Copy link

I had same issue, and to move on, I installed dependency of log4net and that fixed the issue.

@spati2
Copy link

spati2 commented Jun 7, 2017

That will work, but what if I dont want to use log4net?moreover the version of log4net being used in incompatible with netcore, should that reference even be loaded? Also is there a way we can prevent test adapter assembly resolver from kicking in? @AbhitejJohn ?

@codito
Copy link
Contributor

codito commented Jun 8, 2017

Thanks @jonstelly @spati2 for reporting this. We'll investigate.

@Faizan2304 is this issue same as the desktop adapters nuget? We shouldn't be passing the powershell adapter to netcore test runs.

@spati2
Copy link

spati2 commented Jun 9, 2017

Thanks @codito for responding. So can I expect a fix from you guys on this? Customers on AWS SDK dot net toolkit are encountering this very issue and I am afraid any fix that we add to our sdk would not be a foolproof solution

@codito
Copy link
Contributor

codito commented Jun 10, 2017

Yes, we will fix this.

@Faizan2304
Copy link
Contributor

Faizan2304 commented Jun 12, 2017

@Faizan2304 is this issue same as the desktop adapters nuget? We shouldn't be passing the powershell adapter to netcore test runs.

@codito : This is not adapter nuget issue.
This looks like what @AbhitejJohn explaining in this comment

I need to debug more to find the root cause.

@Faizan2304 Faizan2304 self-assigned this Jun 13, 2017
Faizan2304 pushed a commit to Faizan2304/vstest that referenced this issue Jun 13, 2017
Faizan2304 added a commit that referenced this issue Jun 15, 2017
* Clear cache after discover/execution complete.

* Sort usings

* Fix for issue:
1) #632
2) #844

* nitpick: Spelling

* Addressed PR comment

* spelling correction

* nitpick: function name

* Removed unused usings
@Faizan2304
Copy link
Contributor

Fixed will come in next preview of visual studio.

Faizan2304 added a commit to Faizan2304/vstest that referenced this issue Jun 15, 2017
)

* Clear cache after discover/execution complete.

* Sort usings

* Fix for issue:
1) microsoft#632
2) microsoft#844

* nitpick: Spelling

* Addressed PR comment

* spelling correction

* nitpick: function name

* Removed unused usings

(cherry picked from commit 0eef86b)
Faizan2304 added a commit that referenced this issue Jun 15, 2017
* Clear extensions cache after discover/execution complete. (#853)

* Clear cache after discover/execution complete.

* Sort usings

* Fix for issue:
1) #632
2) #844

* nitpick: Spelling

* Addressed PR comment

* spelling correction

* nitpick: function name

* Removed unused usings

(cherry picked from commit 0eef86b)

* Add portable pdb support for net46. (#856)

(cherry picked from commit c70c0c8)

* Install shared farmework compatible with particular version of CLI (#857)

(cherry picked from commit 322bf71)
livarcocc added a commit to dotnet/cli that referenced this issue Jun 20, 2017
…se/2.0.0-to-master-20170620-070043

* dotnet/release/2.0.0: (34 commits)
  Simple clean up to the store tests
  Updating xlf files for the Configurer.
  Removing NuGetConfig from the first run experience and replacing the sentinel with the FirstUseNoticeSentinel when needed.
  Moving the expanding fallback folder to the dotnet folder hive. The folder will be expanded at sdk/NuGetFallbackFolder.
  Fixing CRLF issue.
  Updating xlf and fixing line ending problems with one file.
  Add rhel and Debian download link
  Fix race in telemetry msbuild arg tests
  Avoid repeating the first-run message if lzma archive is missing
  Use fluent helper for line-end normalization
  Update CoreSetup to preview3-25419-01
  Updating Roslyn to 2.3.0-beta3-61816-04
  Update CoreSetup to preview3-25418-01
  insert new tpv2 which fixes following issue microsoft/vstest#632 microsoft/vstest#844 microsoft/vstest#847 microsoft/vstest#840 microsoft/vstest#843
  Windows installer refresh
  Update SDK to 1.1.0-alpha-20170615-3
  Updating NuGet to 4.3.0-preview3-4168
  MSBuild 15.3.388
  Make dotnet-sln-add.Tests pass on localized setup
  Make dotnet-sln-list.Tests pass on localized setup
  ...
livarcocc added a commit to dotnet/cli that referenced this issue Jun 21, 2017
…se/2.0.0-to-master-20170621-070039

* dotnet/release/2.0.0: (35 commits)
  Fixing an issue in the RepoDirectoriesProvider where it assumed that the only directory under SDK was the SDK directories. This is no longer true now that we moved the NuGet fallback folder there.
  Simple clean up to the store tests
  Updating xlf files for the Configurer.
  Removing NuGetConfig from the first run experience and replacing the sentinel with the FirstUseNoticeSentinel when needed.
  Moving the expanding fallback folder to the dotnet folder hive. The folder will be expanded at sdk/NuGetFallbackFolder.
  Fixing CRLF issue.
  Updating xlf and fixing line ending problems with one file.
  Add rhel and Debian download link
  Fix race in telemetry msbuild arg tests
  Avoid repeating the first-run message if lzma archive is missing
  Use fluent helper for line-end normalization
  Update CoreSetup to preview3-25419-01
  Updating Roslyn to 2.3.0-beta3-61816-04
  Update CoreSetup to preview3-25418-01
  insert new tpv2 which fixes following issue microsoft/vstest#632 microsoft/vstest#844 microsoft/vstest#847 microsoft/vstest#840 microsoft/vstest#843
  Windows installer refresh
  Update SDK to 1.1.0-alpha-20170615-3
  Updating NuGet to 4.3.0-preview3-4168
  MSBuild 15.3.388
  Make dotnet-sln-add.Tests pass on localized setup
  ...
@somildutt
Copy link

somildutt commented Jun 28, 2017

Hello

I am getting below mentioned errors when implementing Log4Net in Xamarine Visual Studio 2017 project.
Can we use log4net for UWP or Xamarine ?


System.TypeInitializationException occurred
HResult=0x80131534
Message=The type initializer for 'My Project.CatchInfo' threw an exception.
Source=
Inner Exception 1:
TypeInitializationException: The type initializer for 'log4net.Core.LoggerManager' threw an exception.
Inner Exception 2:
MethodAccessException: Attempt by method 'log4net.Core.LoggerManager.RegisterAppDomainEvents()' to access method 'System.AppDomain.add_ProcessExit(System.EventHandler)' failed.


@spati2
Copy link

spati2 commented Jun 29, 2017

When will the preview be available?
@Faizan2304

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

No branches or pull requests

7 participants