Testing Entity Framework via in-memory option in Visual Studio 2015: solving the dreaded "attempt to access unloaded AppDomain" issue

90 views
Skip to first unread message

Craeg Strong

unread,
Sep 11, 2016, 6:53:11 AM9/11/16
to SpecFlow
I am using SpecFlow to test a .NET Core application that uses Entity Framework Core.
I am using the in-memory option as in:

        private static DbContextOptions<HighFiveContext> CreateNewContextOptions()

        {

            // Create a fresh service provider, and therefore a fresh 

            // InMemory database instance.

            var serviceProvider = new ServiceCollection()

                .AddEntityFrameworkInMemoryDatabase()

                .BuildServiceProvider();


            // Create a new options instance telling the context to use an

            // InMemory database and the new service provider.

            var builder = new DbContextOptionsBuilder<HighFiveContext>();

            builder.UseInMemoryDatabase()

                   .UseInternalServiceProvider(serviceProvider);


            return builder.Options;

        }


I found that I was consistently getting test errors "attempt to access unloaded AppDomain"
I tried forcing single-threading, locking, various other things all to no avail.
This is what worked for me: I created and loaded the following test settings file:
myproject.runnsettings

hope this is helpful for someone else!

<RunSettings>

  <!-- Without these settings, the tests fail with "attempt to access unloaded AppDomain exception -->

  <!-- CKS: Note everything runs fine under resharper test runner  -->

  <MSTest>

    <ForcedLegacyMode>true</ForcedLegacyMode>

    <InProcMode>true</InProcMode>

  </MSTest>

</RunSettings>

Reply all
Reply to author
Forward
0 new messages