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

时间:2016-08-18 06:35:48

标签: .net dapper jit .net-core

使用框架ne​​t46升级到.NET核心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;
        }

此处变量连接的类型为IDbConnection,QueryMultiple是Dapper v1.50.2.0的扩展方法。

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

我认为这是与底层依赖项版本不匹配有关的问题。在使用Maven或类似工具的Java中,我会运行一些依赖性分析来查找依赖树中的版本冲突,但我不知道如何在.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%" ]
      }
    }

1 个答案:

答案 0 :(得分:1)

问题在于您引用了DapperDapper-AsyncDapper-Async不是Dapper的补充,而是替换。这意味着两个包都包含Dapper.dll,因此dotnet对使用哪个包感到困惑,并且由于某种原因,在编译时使用一个而在运行时使用另一个,导致异常。

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