Aditya Pewekar Aditya Pewekar - 2 months ago 48
ASP.NET (C#) Question

ASP.NET Core System.TypeLoadException DependencyInjection.Extensions.ServiceCollectionExtensions error

I am new to Asp.Net Core. While creating a simple MVC project I got following error.


System.TypeLoadException Could not load type
'Microsoft.Extensions.DependencyInjection.Extensions.ServiceCollectionExtensions'
from assembly 'Microsoft.Extensions.DependencyInjection.Abstractions,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. at
Microsoft.Extensions.DependencyInjection.OptionsServiceCollectionExtensions.AddOptions(IServiceCollection
services) at
Microsoft.Extensions.DependencyInjection.MvcCoreServiceCollectionExtensions.AddMvcCore(IServiceCollection
services, Action setupAction) at
Microsoft.Extensions.DependencyInjection.MvcServiceCollectionExtensions.AddMvc(IServiceCollection
services, Action setupAction) at
POC.Startup.ConfigureServices(IServiceCollection services)

System.Reflection.TargetInvocationException
Exception has been thrown
by the target of an invocation. at
System.RuntimeMethodHandle.InvokeMethod(object target, Object[]
arguments, Signature sig, bool constructor) at
System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(object obj,
Object[] parameters, Object[] arguments) at
System.Reflection.RuntimeMethodInfo.Invoke(object obj, BindingFlags
invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at
Microsoft.AspNetCore.Hosting.Internal.ConfigureServicesBuilder.Invoke(object
instance, IServiceCollection exportServices) at
Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices()
at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()


Following is my Project.json file

{
"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%" ]
}
}


Following is my Startup.cs file

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"}
));
}
}
}


I do get the error at the service.AddMvc() line in startup.cs file
Please do suggest a solution for this.

Answer

Have you tried using this dependency for MVC in project.json:

"Microsoft.AspNetCore.Mvc": "1.0.0"

instead of these?

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

It's possible that using AspNetCore with AspNet for your MVC dependencies leads to conflicts.