'System.TypeLoadException' occurred in > Microsof

本文关键字:gt Microsof in TypeLoadException System occurred | 更新日期: 2023-09-27 18:13:17

我正在尝试创建示例asp.net核心应用程序。但是在调试时抛出一个错误:

类型为'System '的异常。类型异常'发生在microsoft . asp.net . mvc . core .dll,但未在用户代码中处理

附加信息:无法加载类型"Microsoft.Extensions.DependencyInjection.Extensions.ServiceCollectionExtensions"Microsoft.Extensions.DependencyInjection.Abstractions版本=1.0.0.0,文化=中性,PublicKeyToken=adb9793829ddae60'.

怎么了?

project.json

{
  "dependencies": {
    "Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
    "Microsoft.AspNetCore.Diagnostics": "1.0.0",
    "Microsoft.AspNetCore.Mvc.Core": "1.0.1",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
    "Microsoft.AspNetCore.StaticFiles": "1.0.0",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "Microsoft.NETCore.App": {
      "version": "1.0.1",
      "type": "platform"
    }
  },
  "tools": {
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
  },
  "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dotnet5.6",
        "portable-net45+win8"
      ]
    }
  },
  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },
  "runtimeOptions": {
    "configProperties": {
      "System.GC.Server": true
    }
  },
  "publishOptions": {
    "include": [
      "wwwroot",
      "web.config"
    ]
  },
  "scripts": {
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}

Startup.cs

public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
        }
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole();
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseStaticFiles();
            app.UseMvc();
        }
    }

'System.TypeLoadException' occurred in > Microsof

去掉"Microsoft.AspNetCore.Mvc.Core": "1.0.1""Microsoft.AspNet.Mvc": "6.0.0-rc1-final",用"Microsoft.AspNetCore.Mvc": "1.0.1"代替

最终版本
{
  "dependencies": {
    "Microsoft.AspNetCore.Diagnostics": "1.0.0",
    "Microsoft.AspNetCore.Mvc": "1.0.1",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
    "Microsoft.AspNetCore.StaticFiles": "1.0.0",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "Microsoft.NETCore.App": {
      "version": "1.0.1",
      "type": "platform"
    }
  }

Microsoft.AspNet.Mvc已经引用了Microsoft.AspNet.Mvc.Core,所以不需要直接引用它。

您缺少"Microsoft.Extensions.DependencyInjection. "

你的project.json看起来像你在使用ASP。. NET Core RC2,但从那以后你升级到发布版本。

文档说,为了使一切工作在RC2你应该

如果你的目标是。net Core和RC2,你需要添加导入到项目中。json作为一些EF Core不支持。net标准的依赖的临时解决方案。这些现在可以被删除。

来源:ASP。Core文档

所以删除

"imports": [
    "dotnet5.6",
    "portable-net45+win8"
]

from your project.json