>中出现'System.TypeLoadException' Microsoft.AspNet.Mvc.Core.dll

时间:2016-10-17 07:48:54

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

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

  

发生了'System.TypeLoadException'类型的异常   Microsoft.AspNet.Mvc.Core.dll但未在用户代码中处理

     

其他信息:无法加载类型   'Microsoft.Extensions.DependencyInjection.Extensions.ServiceCollectionExtensions'   来自程序集'Microsoft.Extensions.DependencyInjection.Abstractions,   Version = 1.0.0.0,Culture = neutral,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();
        }
    }

3 个答案:

答案 0 :(得分:1)

删除"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,因此无需直接引用它。

答案 1 :(得分:0)

您在依赖项中缺少“Microsoft.Extensions.DependencyInjection.Abstractions”:“1.0.0”。

答案 2 :(得分:0)

您的project.json看起来像是在使用ASP.NET Core RC2,但从那时起,您已升级到已发布的版本。

文档说,为了让一切都在RC2中运行,你应该

  

如果您使用RC2定位.NET Core,则需要将导入添加到project.json,作为一些不支持.NET Standard的EF Core依赖项的临时解决方法。现在可以删除它们。

来源:ASP.NET Core documentation

删除

"imports": [
    "dotnet5.6",
    "portable-net45+win8"
]
来自project.json