user4266661
user4266661

Reputation:

UserSecrets.dll not finding project.json

I'm converting an aspnet5/rc-1 website to the released aspnet core. I'm running into a problem involving user secrets in the development environment.

The call to AddUserSecrets, in the Startup constructor, throws an exception:

public Startup( IHostingEnvironment env )
{
    // Set up configuration sources.
    var builder = new ConfigurationBuilder()
        .AddJsonFile( "appsettings.json" )
        .AddJsonFile( $"appsettings.{env.EnvironmentName}.json", optional: true );

    if( env.IsDevelopment() )
    {
        // this line blows up
        builder.AddUserSecrets();
    }

    builder.AddEnvironmentVariables();
    Configuration = builder.Build();

The error message is something to the effect that the project.json file could not be found in the bin/Debug/netcoreapp1.0 folder...which is weird, because I wouldn't expect to find the project.json file in the distributable executable folder in the first place.

This all worked fine under rc1, so something has changed.

Additional Info

Here is the exception detail:

System.InvalidOperationException was unhandled by user code
HResult=-2146233079 Message=Unable to locate a project.json at 'C:\Programming\SpeedView\src\SpeedView\bin\Debug\netcoreapp1.0\'.
Source=Microsoft.Extensions.Configuration.UserSecrets StackTrace: at Microsoft.Extensions.Configuration.UserSecrets.PathHelper.GetSecretsPath(IFileProvider provider) at Microsoft.Extensions.Configuration.ConfigurationExtensions.AddUserSecrets(IConfigurationBuilder configuration) at SpeedView.Startup..ctor(IHostingEnvironment env) in C:\Programming\SpeedView\src\SpeedView\Startup.cs:line 48
InnerException:

Here is project.json file:

{
  "userSecretsId": "aspnet5-SpeedView-f526a1b2-f58b-4e04-b189-8442609eff8c",
  "version": "1.0.0-*",
  "compilationOptions": {
    "emitEntryPoint": true
  },

  "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.0",
      "type": "platform"
    },
    "Microsoft.AspNetCore.Antiforgery": "1.0.0",
    "Microsoft.AspNetCore.Authentication.Cookies": "1.0.0",
    "Microsoft.AspNetCore.Diagnostics": "1.0.0",
    "Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore": "1.0.0",
    "Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0",
    "Microsoft.AspNetCore.Mvc": "1.0.0",
    "Microsoft.AspNetCore.Razor.Tools": {
      "version": "1.0.0-preview2-final",
      "type": "build"
    },
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
    "Microsoft.AspNetCore.StaticFiles": "1.0.0",
    "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0",
    "Microsoft.EntityFrameworkCore.SqlServer.Design": {
      "version": "1.0.0",
      "type": "build"
    },
    "Microsoft.EntityFrameworkCore.Tools": {
      "version": "1.0.0-preview2-final",
      "type": "build"
    },
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
    "Microsoft.Extensions.Configuration.Json": "1.0.0",
    "Microsoft.Extensions.Configuration.UserSecrets": "1.0.0",
    "Microsoft.Extensions.Logging": "1.0.0",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "Microsoft.Extensions.Logging.Debug": "1.0.0",
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0",
    "Microsoft.VisualStudio.Web.CodeGeneration.Tools": {
      "version": "1.0.0-preview2-final",
      "type": "build"
    },
    "Microsoft.VisualStudio.Web.CodeGenerators.Mvc": {
      "version": "1.0.0-preview2-final",
      "type": "build"
    },
    "AutoMapper": "5.0.2",
    "Serilog.Extensions.Logging": "1.1.0",
    "Serilog.Sinks.ColoredConsole": "2.0.0",
    "Serilog.Sinks.Literate": "2.0.0",
    "Serilog.Sinks.RollingFile": "2.2.0",
    "Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final"
  },

  "tools": {
    "BundlerMinifier.Core": "2.0.238",
    "Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final",
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final",
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",
    "Microsoft.Extensions.SecretManager.Tools": "1.0.0-preview2-final",
    "Microsoft.VisualStudio.Web.CodeGeneration.Tools": {
      "version": "1.0.0-preview2-final",
      "imports": [
        "portable-net45+win8"
      ]
    }
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dotnet5.6",
        "portable-net45+win8"
      ]
    }
  },

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

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

  "scripts": {
    "prepublish": [ "npm install", "bower install", "gulp siteprep" ]
  }
}

Upvotes: 0

Views: 311

Answers (1)

Gerardo Grignoli
Gerardo Grignoli

Reputation: 15167

Don't forget to set your BasePath:

public Startup( IHostingEnvironment env )
{
 // Set up configuration sources.
 var builder = new ConfigurationBuilder()
    .SetBasePath(env.ContentRootPath) // Add this line <-----
    .AddJsonFile( "appsettings.json" )
    .AddJsonFile( $"appsettings.{env.EnvironmentName}.json", optional: true );

Upvotes: 1

Related Questions