【问题标题】:Getting TypeLoadException when starting ASP.Net Core App启动 ASP.Net Core App 时出现 TypeLoadException
【发布时间】:2017-01-30 08:41:18
【问题描述】:

我有一个 ASP.Net Core 应用程序,当我尝试启动它时,以下行会抛出 System.TypeLoadException

    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

        if (env.IsEnvironment("Development"))
        {
            // This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately.
            builder.AddApplicationInsightsSettings(developerMode: true);
        }

        builder.AddEnvironmentVariables();
        Configuration = builder.Build(); // <-- exception is thrown here
    }

异常内容如下:

Could not load type 'System.Runtime.Serialization.ISerializable' from assembly 'System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.

这就是我的 project.json 的样子:

{
  "dependencies": {
    "adremes.Common": "1.1.0",
    "adremes.Data": "1.1.2",
    "AutoMapper": "5.2.0",
    "Microsoft.AspNetCore.Hosting.Abstractions": "1.1.0",
    "Microsoft.AspNetCore.Routing": "1.1.0",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.1.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.1.0",
    "Microsoft.AspNetCore.SignalR.Server": "0.2.0-preview2-22683",
    "Microsoft.AspNetCore.WebSockets": "1.1.0-preview1-23121",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.1.0",
    "Microsoft.Extensions.Configuration.FileExtensions": "1.1.0",
    "Microsoft.Extensions.Configuration.Json": "1.1.0",
    "Microsoft.Extensions.Logging": "1.1.0",
    "Microsoft.Extensions.Logging.Console": "1.1.0",
    "Microsoft.Extensions.Logging.Debug": "1.1.0",
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.1.0",
    "Microsoft.NETCore.App": "1.1.0",
    "AutoMapper.Collection": "2.1.2",
    "Microsoft.AspNetCore.Identity": "1.1.0",
    "DocumentFormat.OpenXml": "2.7.0-vnext0061",
    "AspNet.Security.OpenIdConnect.Server": "1.0.0-beta7-final",
    "AspNet.Security.OAuth.Validation": "1.0.0-alpha3-final",
    "Microsoft.ApplicationInsights.AspNetCore": "2.0.0",
    "Microsoft.AspNetCore.Mvc": "1.1.1"
  },

  "tools": {
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview4-final"
  },

  "frameworks": {
    "netcoreapp1.0": {
      "dependencies": {
        "adremes.Exchange.Common": {
          "target": "project"
        },
        "adremes.Exchange.Data": {
          "target": "project"
        },
        "adremes.Exchange.Services": {
          "target": "project"
        }
      },
      "imports": [
        "dnxcore50"
      ]
    }
  },
  "runtimes": {
    "win10-x64": {}
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true,
    "allowUnsafe": true
  },

  "runtimeOptions": {
    "configProperties": {
      "System.GC.Server": true
    }
  },

  "publishOptions": {
    "include": [
      "wwwroot",
      "**/*.cshtml",
      "appsettings.json",
      "web.config"
    ]
  },

  "scripts": {
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}

引用的项目都是netstandard1.5

我尝试将"System.Runtime.Serialization.Primitives": "4.3.0" 添加到我的依赖项中,但没有成功。

有人知道我必须做什么才能成功启动我的项目吗?

【问题讨论】:

  • 解决方案的目标框架是什么? 4.0 或更高版本。如果更高,请尝试设置为 4.0。
  • 目标是netcoreapp1.0;因为我使用的是 ASP Core,所以无法将其设置为 4.0、4.5 等。
  • 您是否尝试过将其作为依赖程序集添加到配置中。
  • 这是 .Net Core,没有 .csproj 来添加依赖组件。但是如上所述,我尝试将其添加到 project.json 中但没有成功。
  • 对不起,我不熟悉 .Net Core。也没有“Web.config”吗?这就是配置设置的地方

标签: c# asp.net asp.net-core asp.net-core-mvc .net-core


【解决方案1】:

问题出在 project.json 的依赖项部分。

线

"Microsoft.NETCore.App": "1.1.0",

应该是

"Microsoft.NETCore.App": {
  "type": "platform",
  "version": "1.1.0"
},

然后这会在启动时产生错误(从控制台使用“dotnet run”来查看错误消息)。

错误提示缺少 1.1.0 的 Microsoft.NETCore.App。

https://www.microsoft.com/net/download/core#/current 安装它然后解决了问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-18
    • 2016-07-27
    • 1970-01-01
    • 1970-01-01
    • 2020-01-10
    • 1970-01-01
    • 1970-01-01
    • 2019-08-17
    相关资源
    最近更新 更多