Guerrilla
Guerrilla

Reputation: 14906

AppSelfHoseBase generates error when starting in .net 5

I am trying to create an apphost in the testing project of a project created from .net 5.0 react template.

I am getting the error:

OneTimeSetUp: System.TypeLoadException : Could not load type 'Microsoft.Extensions.Primitives.InplaceStringBuilder' from assembly 'Microsoft.Extensions.Primitives, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.

And the breakpoint inside Configure() isn't hitting.

Same code seems to work fine in a .net 3.1 project.

Here is gist of app host:

    public class MainAppHost : AppSelfHostBase
    {
        public MainAppHost() : base(nameof(MainAppHost), typeof(MyServices).Assembly) { }


        public override void Configure(Container container)
        {
            //having this blank still triggers error
        }
    }

Seems error is thrown on AppHost.Start(url).

Stack trace:

   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
   at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) in /_/src/coreclr/src/System.Private.CoreLib/src/System/Reflection/RuntimeConstructorInfo.cs:line 375
   at System.Reflection.ConstructorInfo.Invoke(Object[] parameters) in /_/src/libraries/System.Private.CoreLib/src/System/Reflection/ConstructorInfo.cs:line 18
   at NUnit.Framework.Internal.Reflect.Construct(Type type, Object[] arguments) in /_/src/NUnitFramework/framework/Internal/Reflect.cs:line 113
   at NUnit.Framework.Internal.TypeWrapper.Construct(Object[] args) in /_/src/NUnitFramework/framework/Internal/TypeWrapper.cs:line 252
   at NUnit.Framework.Internal.Commands.ConstructFixtureCommand.<.ctor>b__0_0(TestExecutionContext context) in /_/src/NUnitFramework/framework/Internal/Commands/ConstructFixtureCommand.cs:line 51
   at NUnit.Framework.Internal.Commands.BeforeTestCommand.Execute(TestExecutionContext context) in /_/src/NUnitFramework/framework/Internal/Commands/BeforeTestCommand.cs:line 48
   at NUnit.Framework.Internal.Execution.CompositeWorkItem.PerformOneTimeSetUp() in /_/src/NUnitFramework/framework/Internal/Execution/CompositeWorkItem.cs:line 262

I saw a similar issue on NUnit github caused by 3.1 and 5.0 installed on same system so I uninstalled all older versions of SDK but it made no difference.

A simple NUnit test without the apphost works fine:

    public class SimpleTestClass
    {
        [Test]
        public void SimpleTest()
        {
            Assert.That(1 + 1 == 2);
        }
    }

But if I try to create the AppHost then I get the error:

    public class SimpleTestClass
    {
        public SimpleTestClass()
        {
            var AppHost = new MainAppHost()
                .Init()
                .Start("http://localhost:5619/");
        }

        [Test]
        public void SimpleTest()
        {
            Assert.That(1 + 1 == 2);
        }
    }

The testing and service layer both target .net 5.0 and the project runs fine, I just can't create an AppHost for testing.

Any idea what I am doing wrong?

edit:

I found exact reproduction steps:

  1. x new react-spa TestAppHost
  2. update all packages
  3. run default integration test it will work
  4. right click testing project and select "User Secrets", install prompted package.
  5. run same integration test it now fails with error. Nunit tests without AppHost will still work fine.

Here is project file:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <DebugType>portable</DebugType>
    <OutputType>Library</OutputType>
    <UserSecretsId>1f094c52-e2b1-44e1-8e3a-9cf5189d8800</UserSecretsId>
  </PropertyGroup>

  <ItemGroup>
    <ProjectReference Include="..\TestAppHost.ServiceInterface\TestAppHost.ServiceInterface.csproj" />
    <ProjectReference Include="..\TestAppHost.ServiceModel\TestAppHost.ServiceModel.csproj" />
    <PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="5.0.0" />

    <PackageReference Include="NUnit" Version="3.13.2" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
    <PackageReference Include="NUnit3TestAdapter" Version="3.17.*" />
    <PackageReference Include="ServiceStack" Version="5.*" />
    <PackageReference Include="ServiceStack.Kestrel" Version="5.*" />
  </ItemGroup>

</Project>

Upvotes: 1

Views: 103

Answers (1)

mythz
mythz

Reputation: 143369

Edit after repro steps provided. The issue is with referencing v5.0.0 of UserSecrets as ServiceStack's .NET Standard 2.0 builds used in .NET 5.0 Apps only references v2.2.0 which you can change it to to resolve the issue, i.e:

<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="2.2.0" />

When .NET 6 LTS is out we'll provide a new framework target for all ServiceStack libraries targeting net6.0 which will let you reference the latest dependency versions.


Unfortunately I've never seen this issue or know how to repro it.

Here's a simple way to create an NUnit Integration test project:

$ mkdir SimpleTestCase && cd SimpleTestCase
$ x mix init-test
$ dotnet test

By default this runs the integration test in both .NET 5.0 and .NET Framework v4.7.2, which should result in the test passing in both .NET platforms:

enter image description here

You can change it to only run in .NET 5.0 by replacing:

<TargetFrameworks>net5.0;net472</TargetFrameworks>

with

<TargetFramework>net5.0</TargetFramework>

If that fails, find which versions of .NET Core you have installed:

$ dotnet --list-sdks
3.1.120 [C:\Program Files\dotnet\sdk]
3.1.414 [C:\Program Files\dotnet\sdk]
5.0.104 [C:\Program Files\dotnet\sdk]

Then create a global.json in your project directory with the latest v5 you have installed, e.g:

{
  "sdk": {
    "version": "5.0.104"
  }
}

This ensures that specific .NET version is used to run/test your project, which you can verify by running:

$ dotnet --version

Upvotes: 1

Related Questions