2016-09-07 157 views
1

我是Asp.Net Core的新手。在創建一個簡單的MVC項目時,我遇到了以下錯誤。ASP.NET Core System.TypeLoadException DependencyInjection.Extensions.ServiceCollectionExtensions錯誤

System.TypeLoadException未能加載類型 'Microsoft.Extensions.DependencyInjection.Extensions.ServiceCollectionExtensions' 從程序集「Microsoft.Extensions.DependencyInjection.Abstractions, 版本= 1.0.0.0,文化=中性公鑰= adb9793829ddae60' 。在 Microsoft.Extensions.DependencyInjection.OptionsServiceCollectionExtensions.AddOptions(IServiceCollection 服務)在 Microsoft.Extensions.DependencyInjection.MvcCoreServiceCollectionExtensions.AddMvcCore(IServiceCollection 服務,行動setupAction)在 Microsoft.Extensions.DependencyInjection.MvcServiceCollectionExtensions.AddMvc(IServiceCollection 服務,動作setupAction)在 POC.Startup.ConfigureServices(IServiceCollection服務)

System.Reflection.TargetInvocationException 異常已通過調用的目標引發異常 。在 System.RuntimeMethodHandle.InvokeMethod(對象目標,對象[] 參數,簽名Sig,布爾構造函數)在 System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(obj對象, 對象[]參數,對象[]參數)在 系統.Reflection.RuntimeMethodInfo.Invoke在 微軟(obj對象,的BindingFlags invokeAttr,粘結劑粘結劑,對象[]參數,CultureInfo的培養物) 在 Microsoft.AspNetCore.Hosting.Internal.ConfigureServicesBuilder.Invoke(對象 例如,IServiceCollection exportServices) .AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices() at 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"} 
       )); 
     } 
    } 
} 

我在啓動時service.AddMvc()線得到錯誤。 cs文件 請爲此建議一個解決方案。

回答

1

您是否嘗試過使用這種依賴對MVC在project.json

"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的依賴導致衝突。

+0

是的,這導致了我的衝突。謝謝 –