ASP.NET Core System.TypeLoadException DependencyInjection.Extensions.ServiceCollectionExtensions错误

时间:2016-09-07 11:12:10

标签: asp.net-core

我是Asp.Net Core的新手。在创建一个简单的MVC项目时,我遇到了以下错误。

  

System.TypeLoadException无法加载类型   ' Microsoft.Extensions.DependencyInjection.Extensions.ServiceCollectionExtensions'   来自assembly' Microsoft.Extensions.DependencyInjection.Abstractions,   Version = 1.0.0.0,Culture = neutral,PublicKeyToken = adb9793829ddae60'。在   Microsoft.Extensions.DependencyInjection.OptionsServiceCollectionExtensions.AddOptions(IServiceCollection   服务)   Microsoft.Extensions.DependencyInjection.MvcCoreServiceCollectionExtensions.AddMvcCore(IServiceCollection   服务,行动设置行动)   Microsoft.Extensions.DependencyInjection.MvcServiceCollectionExtensions.AddMvc(IServiceCollection   服务,行动设置行动)   POC.Startup.ConfigureServices(IServiceCollection服务)

     

System.Reflection.TargetInvocationException   抛出异常   通过调用的目标。在   System.RuntimeMethodHandle.InvokeMethod(object target,Object []   参数,签名sig,bool构造函数)at   System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(object obj,   Object []参数,Object []参数)at   System.Reflection.RuntimeMethodInfo.Invoke(object obj,BindingFlags   invokeAttr,Binder binder,Object []参数,CultureInfo文化)   在   Microsoft.AspNetCore.Hosting.Internal.ConfigureServicesBuilder.Invoke(对象   实例,IServiceCollection exportServices)at   Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices()   在Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()

以下是我的Project.json文件

{
  "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.0",
      "type": "platform"
    },
    "Microsoft.AspNetCore.Diagnostics": "1.0.0",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final",
    "Microsoft.AspNet.Server.Kestrel":"1.0.0-rc1-final",
    "Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
    "Microsoft.AspNet.Mvc.Core": "6.0.0-rc1-final"
  },

  "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文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection;

namespace POC
{
    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)
        {
            app.UseMvc( m=> 
            m.MapRoute(
                name:"Default",
                template:"{controller}/{action}/{id?}",
                defaults:new { controller="Home", action="index"}
                ));
        }
    }
}

我确实在startup.cs文件的service.AddMvc()行收到错误。 请为此建议解决方案。

1 个答案:

答案 0 :(得分:1)

您是否尝试在 project.json 中使用此依赖关系MVC:

"Microsoft.AspNetCore.Mvc": "1.0.0"

而不是这些?

"Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
"Microsoft.AspNet.Mvc.Core": "6.0.0-rc1-final"

AspNetCoreAspNet用于MVC依赖关系可能会导致冲突。