使用 Dapper 升级到 .net core 1.0 后出现 MissingMethodException

MissingMethodException after upgrade to .net core 1.0 while using Dapper

使用框架 net46 升级到 .NET core 1.0 后,出现以下异常:

System.MissingMethodException was unhandled by user code
HResult=-2146233069
Message=Method not found: 'System.Collections.Generic.IEnumerable`1<!!0> GridReader.Read(Boolean)'.
Source=Sdm.Web
StackTrace:
   at Sdm.Web.SqlAccess.SqlMgr.GetNumbersForMainScreen(TenantId tenantId)
   at Sdm.Web.Test.TestDrivers.SqlMgrTestDriver.TestGetNumbersForMainScreen() in ...\Unittest\SqlMgrTestDriver.cs:line 37
InnerException: (null)

它发生在 运行 时间,就在尝试进入名为 GetNumbersForMainScreen 的方法之前,该方法包含以下代码行:

     using (SqlMapper.GridReader result = connection.QueryMultiple(SqlGetNumbersForMainScreen, new { recent = recent, tid = tenantId.Id }))
        {
            var val = new NumbersForMain()
            {
                AlarmsCriticalCount = result.Read<int>().Single(),
                AlarmsNoncriticalCount = result.Read<int>().Single(),
                RoomsCount = result.Read<int>().Single(),
                UnitsOnlineCount = result.Read<int>().Single(),
                UnitsCount = result.Read<int>().Single(),
            };
            val.UnitsOfflineCount = val.UnitsCount - val.UnitsOnlineCount;

            return val;
        }

这里变量connection是IDbConnection类型,QueryMultiple是Dapper v1.50.2.0的扩展方法

对此我很疑惑。代码编译得很好,但在 运行 时我被告知,该方法不存在。

我认为这是与底层依赖项版本不匹配相关的问题。在 Java 使用 Maven 或类似工具时,我会 运行 进行一些依赖分析,以查找依赖树中的版本控制冲突,但我不知道如何解决 .NET 中的此类问题。

任何有关导致此问题的原因或我如何解决此类问题的建议都将不胜感激。

编辑 2016-08-25: 我的 project.json 看起来像这样:

    {
      "userSecretsId": "XXX",
      "buildOptions": {
        "emitEntryPoint": true,
        "preserveCompilationContext": true
      },
      "dependencies": {
        "Dapper": "1.50.2",
        "Dapper-Async": "1.3.0",
        "EntityFramework": "6.1.3",
        "Microsoft.EntityFrameworkCore.SqlServer.Design": "1.0.0",  
        "Microsoft.AspNetCore.Authentication.OpenIdConnect": "1.0.0",
        "Microsoft.AspNetCore.Authentication.Cookies": "1.0.0",
        "Microsoft.AspNetCore.Diagnostics.Elm": "0.1.0",
        "Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore": "1.0.0",
        "Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0",
        "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
        "Microsoft.AspNetCore.Mvc": "1.0.0",
        "Microsoft.AspNetCore.Mvc.TagHelpers": "1.0.0",
        "Microsoft.AspNetCore.Razor.Tools": {
          "version": "1.0.0-preview2-final",
          "type": "build"
        },
        "Microsoft.AspNetCore.StaticFiles": "1.0.0",
        "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
        "Microsoft.Extensions.Configuration.Json": "1.0.0",
        "Microsoft.Extensions.Configuration.UserSecrets": "1.0.0",
        "Microsoft.Extensions.Logging": "1.0.0",
        "Microsoft.Extensions.Logging.Console": "1.0.0",
        "Microsoft.Extensions.Logging.Debug": "1.0.0",
        "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0",
        "Microsoft.Extensions.Caching.Abstractions": "1.0.0",
        "Microsoft.Extensions.Caching.Memory": "1.0.0",
        "Newtonsoft.Json": "9.0.1",
        "Microsoft.AspNetCore.Authentication.JwtBearer": "1.0.0",
        "Microsoft.AspNetCore.Authorization": "1.0.0",
        "Microsoft.Azure.Devices": "1.0.5",
        "Microsoft.AspNetCore.HttpOverrides": "1.0.0",
        "EFAttributeConfig": "1.0.0",
        "Twilio": "4.5.0",
        "Sendgrid": "6.3.4",
        "Microsoft.AspNetCore.Routing": "1.0.0",
        "Microsoft.AspNetCore.Hosting": "1.0.0",
        "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
        "log4net": "2.0.5"
      },
      "tools": {
        "BundlerMinifier.Core": "2.0.238",
        "Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final",
        "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
      },

      "frameworks": {
        "net46": { }
      },
      "publishOptions": {
        "include": [
          "wwwroot",
          "Views",
          "appsettings.json",
          "web.config",
          "log4net.xml"
        ], 
        "exclude": [
            "**.user",
            "**.vspscc"
        ]    
      },

      "scripts": {
        "prepublish": [ "bower install", "dotnet bundle" ],
        "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
      }
    }

问题是您同时引用了 DapperDapper-AsyncDapper-Async 不是 Dapper 的补充,而是 的替代 。这意味着两个包都包含 Dapper.dll,因此 dotnet 不知道该使用哪个包,并且出于某种原因,在编译时使用一个而在运行时使用另一个,从而导致异常。

解决方案是使用 DapperDapper-Async,但不能同时使用两者。至于用哪一个,我不知道,因为我对Dapper 不熟悉。但我的猜测是你应该使用 Dapper,因为 Dapper-Async 已经很长时间没有更新了。