【问题标题】:What's the correct way to load Microsoft.SqlServer.Types before unit tests在单元测试之前加载 Microsoft.SqlServer.Types 的正确方法是什么
【发布时间】:2019-04-04 05:12:41
【问题描述】:

我正在尝试使用 NUnit 为依赖于 DbGeography.PointFromText(text, 4326) 的代码编写一些单元测试,这需要加载 Microsoft.SqlServer.Types。我正在尝试使用以下方式加载它:

[OneTimeSetUp]
public void Startup()
{
    SqlServerTypes.Utilities.LoadNativeAssemblies(AppDomain.CurrentDomain.BaseDirectory);
}

我也尝试在[Setup] 方法中使用加载它们。当调用使用DbGeography 的代码时,我得到了一个可爱的异常:

System.InvalidOperationException : 空间类型和函数不是 可用于此提供程序,因为程序集 找不到 'Microsoft.SqlServer.Types' 版本 10 或更高版本。

我确实安装了Microsoft.SqlServer.Types Nuget 包,它在我的 Web API 2.2 应用程序中运行良好且非常好。

【问题讨论】:

  • 您的测试项目中是否也安装了 nuget pkg?
  • 是的,在测试项目中也安装了。

标签: c# sql-server nunit


【解决方案1】:

我最终安装了 Microsoft SQL Server CLR 类型,并且我的单元测试能够找到必要的类型。

【讨论】:

    【解决方案2】:

    你可以在系统上安装这些库,但是还需要记住安装在构建服务器和生产等上。

    或者,已成功安装 Microsoft.SqlServer.Types NuGet 并按照说明加载程序集。对于 xunit,我将其包含在 TextFixure ctor 中。

            SqlServerTypes.Utilities.LoadNativeAssemblies(AppDomain.CurrentDomain.BaseDirectory);
    

    另外,发现我需要添加 bindingRedirects:

      <dependentAssembly>
        <assemblyIdentity name="Microsoft.SqlServer.Types" publicKeyToken="89845dcd8080cc91" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-14.0.0.0" newVersion="14.0.0.0" />
      </dependentAssembly>
    

    【讨论】:

      猜你喜欢
      • 2012-10-03
      • 2017-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-06
      • 1970-01-01
      • 2016-03-25
      • 2014-09-01
      相关资源
      最近更新 更多