Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

'GCHandle value belongs to a different domain' when bootstrapping Kestrel in test #101

Closed
AlexZeitler opened this issue May 16, 2015 · 2 comments

Comments

@AlexZeitler
Copy link

I'm bootstrapping a new WebListener / Kestrel based host in an integration test like this:

    public class CustomerControllerIntegrationTest
    {
        [Fact]
        public void ShouldReturnCustomer()
        {
            var config = new Configuration();
            config.AddCommandLine(new[] { "--server.urls", "http://localhost:5001" });

            var serverFactoryLocation = string.Empty;
            if(!IsMono()) {
                serverFactoryLocation = "Microsoft.AspNet.Server.WebListener";
            } else {
                serverFactoryLocation = "Kestrel";
            }
            var context = new HostingContext()
            {
                Configuration = config,
                ServerFactoryLocation = serverFactoryLocation,
                ApplicationName = "AspNet5IntegrationTesting",
                StartupMethods = new StartupMethods(builder => builder.UseMvc(), services =>
                {
                    services.AddMvc();
                    return services.BuildServiceProvider();
                })
            };


            using (new HostingEngine().Start(context))
            {
                var client = new HttpClient();
                var customers = client.GetAsync("http://localhost:5001/api/customers").Result
                    .Content.ReadAsAsync<List<Customer>>().Result;

                Assert.Equal(customers[0].CompanyName,"PDMLab");
            }
        }

        public static bool IsMono()
        {
            return Type.GetType("Mono.Runtime") != null;
        }

    }

Running the test on DNX Beta 4 on Windows, everything is fine.
Running the test on Ubuntu 14.04, it fails with this error:

xUnit.net DNX test runner (64-bit DNX 4.5.1)
Copyright (C) 2015 Outercurve Foundation.

Discovering: AspNet5IntegrationTesting
Discovered:  AspNet5IntegrationTesting
Starting:    AspNet5IntegrationTesting
   AspNet5IntegrationTesting.Tests.CustomerControllerIntegrationTest.ShouldReturnCustomer [FAIL]
      System.ArgumentException : GCHandle value belongs to a different domain
      Stack Trace:
           at System.Runtime.InteropServices.GCHandle.op_Explicit (IntPtr value) [0x00000] in <filename unknown>:0 
           at System.Runtime.InteropServices.GCHandle.FromIntPtr (IntPtr value) [0x00000] in <filename unknown>:0 
           at Microsoft.AspNet.Server.Kestrel.Networking.UvMemory.DestroyMemory (IntPtr memory) [0x00000] in <filename unknown>:0 
           at Microsoft.AspNet.Server.Kestrel.Networking.UvLoopHandle.ReleaseHandle () [0x00000] in <filename unknown>:0 
           at System.Runtime.InteropServices.SafeHandle.RunRelease () [0x00000] in <filename unknown>:0 
           at System.Runtime.InteropServices.SafeHandle.Dispose (Boolean disposing) [0x00000] in <filename unknown>:0 
           at System.Runtime.InteropServices.SafeHandle.Dispose () [0x00000] in <filename unknown>:0 
           at Microsoft.AspNet.Server.Kestrel.KestrelThread.ThreadStart (System.Object parameter) [0x00000] in <filename unknown>:0 
         --- End of stack trace from previous location where exception was thrown ---
           at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <filename unknown>:0 
           at Microsoft.AspNet.Server.Kestrel.KestrelThread.Stop (TimeSpan timeout) [0x00000] in <filename unknown>:0 
           at Microsoft.AspNet.Server.Kestrel.KestrelEngine.Dispose () [0x00000] in <filename unknown>:0 
           at Kestrel.ServerFactory+<>c__DisplayClass3_0.<Start>b__1 () [0x00000] in <filename unknown>:0 
           at Microsoft.AspNet.Server.Kestrel.Disposable.Dispose (Boolean disposing) [0x00000] in <filename unknown>:0 
           at Microsoft.AspNet.Server.Kestrel.Disposable.Dispose () [0x00000] in <filename unknown>:0 
           at Microsoft.AspNet.Hosting.HostingEngine+<>c__DisplayClass9_0.<Start>b__1 () [0x00000] in <filename unknown>:0 
           at Microsoft.AspNet.Hosting.HostingEngine+Disposable.Dispose () [0x00000] in <filename unknown>:0 
           at AspNet5IntegrationTesting.Tests.CustomerControllerIntegrationTest.ShouldReturnCustomer () [0x00000] in <filename unknown>:0 
           at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&)
           at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0 
Finished:    AspNet5IntegrationTesting

=== TEST EXECUTION SUMMARY ===
   AspNet5IntegrationTesting  Total: 1, Errors: 0, Failed: 1, Skipped: 0, Time: 0.955s

The full repo reproducing the error can be found here

@Tragetaschen
Copy link
Contributor

That's a known issue in Kestrel: #9
Start here for an explanation

@AlexZeitler
Copy link
Author

Thanks for both the information and the fix 👍

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

No branches or pull requests

2 participants