Configuration icon indicating copy to clipboard operation
Configuration copied to clipboard

System.IO.FileLoadException at ConfigurationBuilder.Build()

Open stratever opened this issue 10 years ago • 4 comments

Exception:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.IO.FileLoadException: Could not load file or assembly 'Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) ---> System.IO.FileLoadException: Could not load file or assembly 'Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

StackTrace:

at Microsoft.Extensions.Configuration.Json.JsonConfigurationProvider.Load(Stream stream) at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load() at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build() at FNWebAPI.Startup.BuildAppSettingsConfiguration() in D:\svn\Front\src\Web\FNWebAPI\Trunk\src\FNWebAPI\Startup.cs:line 186 at FNWebAPI.Startup.ConfigureServices(IServiceCollection services) in D:\svn\Front\src\Web\FNWebAPI\Trunk\src\FNWebAPI\Startup.cs:line 52 --- End of inner exception stack trace --- at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean 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 System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) at Microsoft.AspNetCore.Hosting.Startup.ConfigureServicesBuilder.Invoke(Object instance, IServiceCollection exportServices) at Microsoft.AspNetCore.Hosting.Startup.ConfigureServicesBuilder.<>c__DisplayClass4_0.b__0(IServiceCollection services) at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices() at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()

project.json:

{
  "dependencies": {
    "FNWebAPI.Authentication": "1.0.0-*",
    "FNWebAPI.Interfaces": "1.0.0-*",
    "FNWebAPI.Model": "1.0.0-*",
    "FNWebAPI.ServiceAgent": "1.0.0-*",
    "Microsoft.AspNetCore.Mvc": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.Binder": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.Json": "1.0.0-rc2-final",
    "Microsoft.Extensions.Logging": "1.0.0-rc2-final",
    "Serilog.Extensions.Logging": "1.0.0-rc2-10104",
    "Serilog.Sinks.RollingFile": "2.0.0-rc-703"
  },

  "tools": {
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": {
      "version": "1.0.0-preview1-final",
      "imports": "portable-net45+win8+dnxcore50"
    }
  },

  "frameworks": {
    "net46": {}
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "runtimeOptions": {
    "gcServer": true
  }
}

Code:

var builder = new ConfigurationBuilder()
                .SetBasePath(_env.ContentRootPath)
                .AddJsonFile("appsettings.json");
                //.AddJsonFile($"appsettings.{_env.EnvironmentName}.json");

            builder.AddEnvironmentVariables();

            Configuration = builder.Build();

dotnet cli version: 1.0.0-preview1-002702

I got this error after migratig from RC1 to RC2 (ASP.Net Core 1.0). I tried to cleanup the nuget cache directory and rebuild the soultion but it didn't help. In an other solution which contains a project tragets netcoreapp1.0, everything works fine. Any help would be much appreciated .

stratever avatar May 30 '16 08:05 stratever

I got same error in RC2 project type ASP.NET Core Web application (.NET Framework) | Web API and in Startup class constructor:

System.IO.FileLoadException Could not load file or assembly 'Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

public Startup(IHostingEnvironment env)
{
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
        .AddEnvironmentVariables();
    Configuration = builder.Build();
}
{
  "dependencies": {
    "Microsoft.AspNetCore.Mvc": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.Json": "1.0.0-rc2-final",
    "Microsoft.Extensions.Logging": "1.0.0-rc2-final",
    "Microsoft.Extensions.Logging.Console": "1.0.0-rc2-final",
    "Microsoft.Extensions.Logging.Debug": "1.0.0-rc2-final"
  },

  "tools": {
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": {
      "version": "1.0.0-preview1-final",
      "imports": "portable-net45+win8+dnxcore50"
    }
  },

  "frameworks": {
    "net46": {
      "dependencies": {
        "Microservice": {
          "target": "project"
        }
      }
    }
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "publishOptions": {
    "include": [
      "wwwroot",
      "Views",
      "appsettings.json",
      "web.config"
    ]
  },

  "scripts": {
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}

mo-esmp avatar Jun 20 '16 08:06 mo-esmp

This problem related to the dotnet. Check the following:

  • After build, look for a configuration file in the ouput filder with .config extension
  • Check the assembly bindings, when I had this problem, my .config was the following:
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="8.0.0.0" newVersion="6.0.0.0" />
        <bindingRedirect oldVersion="7.0.0.0" newVersion="6.0.0.0" />
        <bindingRedirect oldVersion="6.0.0.0" newVersion="8.0.0.0" />
        <bindingRedirect oldVersion="7.0.0.0" newVersion="8.0.0.0" />
      </dependentAssembly>

As you can see: 8 -> 6 and 6 -> 8 is confusing and also blocking. To solve the problem, please make sure that the Newtonsoft.Json.dll in your build output folder has the correct version (8.0<). If not, try to clean up your nuget cache folder.

In my case, a 3rd party package with Ns.Json 6.x dependency caused the problem but removing the package didn't solve the problem, I had to recreate my whole project and after that It worked.

stratever avatar Jun 20 '16 08:06 stratever

thank you @stratever, I referenced a class library project which has dependency on Newtonsoft.Json ver7. I installed latest version on both projects and problem solved.

mo-esmp avatar Jun 20 '16 09:06 mo-esmp

System.FormatException: 'Could not parse the JSON file. Error on line number '5': '} "Logging": {'.'

isahyarima avatar Oct 29 '18 14:10 isahyarima