在 TeamCity 中通过 Console Runner 使用 NUnit 3 时找不到 ServiceStack 许可证

ServiceStack License not found when using NUnit 3 through Console Runner in TeamCity

我使用的是有效的许可证密钥。但我不断收到此错误:

ServiceStack.LicenseException : The free-quota limit on '10 ServiceStack Operations' has been reached. Please see https://servicestack.net to upgrade to a commercial license or visit https://github.com/ServiceStackV3/ServiceStackV3 to revert back to the free ServiceStack v3.

到目前为止,我已经尝试了以下方法:

当通过 TeamCity 构建参数将我的密钥添加为系统环境变量时,我反而从 JsConfig 收到异常:

System.TypeInitializationException : The type initializer for 'ServiceStack.Text.JsConfig' threw an exception.
  ----> System.TypeInitializationException : The type initializer for 'ServiceStack.LicenseUtils' threw an exception.
  ----> System.IO.FileLoadException : Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
   at ServiceStack.Text.JsConfig.InitStatics()
   at ServiceStack.AppHostHttpListenerPoolBase..ctor(String serviceName, Int32 poolSize, Assembly[] assembliesWithServices) in C:\BuildAgent\work81147c480f4a2f\src\ServiceStack\AppHostHttpListenerPoolBase.cs:line 75
   at ServiceStack.AppSelfHostBase..ctor(String serviceName, Assembly[] assembliesWithServices) in C:\BuildAgent\work81147c480f4a2f\src\ServiceStack\AppSelfHostBase.cs:line 13
   at Alstra.SG.Tests.AppSelfHost..ctor() in C:\TeamCity\buildAgent\work\a0903bf22b2d1e1c\Test\AppSelfHost.cs:line 31
   at Alstra.SG.Tests.Private.BaseIntegrationTest..ctor() in C:\TeamCity\buildAgent\work\a0903bf22b2d1e1c\Test\Private\BaseIntegrationTest.cs:line 16
   at Alstra.SG.Tests.Private.Integration.RespondentServiceTests..ctor()
--TypeInitializationException
   at ServiceStack.LicenseUtils.Init()
   at ServiceStack.Text.JsConfig..cctor() in C:\BuildAgent\work2418dcce86a188\src\ServiceStack.Text\JsConfig.cs:line 21
--FileLoadException
   at System.MemoryExtensions.AsSpan(String text)
   at ServiceStack.Text.Jsv.JsvReader`1.Parse(String value) in C:\BuildAgent\work2418dcce86a188\src\ServiceStack.Text\Jsv\JsvReader.Generic.cs:line 81
   at ServiceStack.Text.TypeSerializer.DeserializeFromString[T](String value) in C:\BuildAgent\work2418dcce86a188\src\ServiceStack.Text\TypeSerializer.cs:line 67
   at ServiceStack.LicenseUtils.ToLicenseKeyFallback(String licenseKeyText) in C:\BuildAgent\work2418dcce86a188\src\ServiceStack.Text\LicenseUtils.cs:line 446
   at ServiceStack.LicenseUtils.RegisterLicense(String licenseKeyText) in C:\BuildAgent\work2418dcce86a188\src\ServiceStack.Text\LicenseUtils.cs:line 252
   at ServiceStack.Net45PclExport.RegisterLicenseFromConfig() in C:\BuildAgent\work2418dcce86a188\src\ServiceStack.Text\PclExport.Net45.cs:line 140
   at ServiceStack.LicenseUtils..cctor() in C:\BuildAgent\work2418dcce86a188\src\ServiceStack.Text\LicenseUtils.cs:line 135

在 运行 AppHost.Init 之前通过调用 Licensing.RegisterLicense 添加我的密钥时,我得到以下异常:

System.IO.FileLoadException : Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
   at System.MemoryExtensions.AsSpan(String text)
   at ServiceStack.Text.Jsv.JsvReader`1.Parse(String value) in C:\BuildAgent\work2418dcce86a188\src\ServiceStack.Text\Jsv\JsvReader.Generic.cs:line 81
   at ServiceStack.Text.TypeSerializer.DeserializeFromString[T](String value) in C:\BuildAgent\work2418dcce86a188\src\ServiceStack.Text\TypeSerializer.cs:line 67
   at ServiceStack.LicenseUtils.ToLicenseKeyFallback(String licenseKeyText) in C:\BuildAgent\work2418dcce86a188\src\ServiceStack.Text\LicenseUtils.cs:line 446
   at ServiceStack.LicenseUtils.RegisterLicense(String licenseKeyText) in C:\BuildAgent\work2418dcce86a188\src\ServiceStack.Text\LicenseUtils.cs:line 252
   at Alstra.SG.Tests.Private.BaseIntegrationTest..ctor() in C:\TeamCity\buildAgent\work\a0903bf22b2d1e1c\Test\Private\BaseIntegrationTest.cs:line 17
   at Alstra.SG.Tests.Private.Integration.ActivityServiceTests..ctor()

很高兴得到一些帮助!

我相信您使用的 ServiceStack 版本是使用与您在测试项目中使用的 System.Runtime.CompilerServices.Unsafe 不同的版本构建的,因为 bindingredirect 在此上下文中不起作用。 参见 Does redirecting assembly binding work for unit testing with a test runner?

作为解决方法,您可以尝试将 Nuget 包管理器中的 System.Runtime.CompilerServices.Unsafe 更改为 4.5.2。

此版本与缺少的程序集匹配,Version=4.0.4.1。

编辑: 如果您在 NUnit 构建步骤中使用 NUnit 3,则可以指定 "Path to application configuration file: " 指向您的 app.config。 这应该可以解决根本问题并让您使用最新版本的 Nuget 包(因为随后将应用正确的 bindingRedirects)。