GithubHelp home page GithubHelp logo

Comments (19)

lcalabrese avatar lcalabrese commented on August 30, 2024 7

If you are using the Development environment to test but do not need user secrets, you can use a workaround to test for project.json:

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, reloadOnChange: true);

    if (env.IsDevelopment() && builder.GetFileProvider().GetFileInfo("project.json").Exists)
    {
        builder.AddUserSecrets();
    }
}

from usersecrets.

Tratcher avatar Tratcher commented on August 30, 2024 1

It worked if I added project.json to the content section of project.json so it gets published.

from usersecrets.

muratg avatar muratg commented on August 30, 2024 1

@Bartmax Yeah, that's unfortunately the case. I guess you can remove it before restore in your build script, and copy over after publish.

from usersecrets.

asteffes avatar asteffes commented on August 30, 2024 1

Does this error persist if you supply the UserSecretsId as a parameter to builder.AddUserSecrets("userSecretsId") extension method?

EDIT: I was able to recreate this bug using a "dotnet publish" command without including the project.json in the outputs and running the application.

Passing the UserSecretsId as a parameter to AddUserSecrets() method gets around this exception.

This is the work around I'll be using for deployment to our dev environment.

from usersecrets.

muratg avatar muratg commented on August 30, 2024

Ugh, interesting. Yeah, we don't want to project.json be there just for userSecretsId...

@anurse @davidfowl any idea where we can store this for published applications?

from usersecrets.

analogrelay avatar analogrelay commented on August 30, 2024

config.json? That is going to be a tricky one...

from usersecrets.

muratg avatar muratg commented on August 30, 2024

Let's look into some options in the next milestone.

from usersecrets.

Bartmax avatar Bartmax commented on August 30, 2024

I'm currently hitting this bug. Is there any workaround ? this is a RC2 web app and running dotnet run. If i remove UserSecrets it works as expected.

Unhandled Exception: System.InvalidOperationException: Unable to locate a project.json at 'C:\xxx\bin\Debug\netcoreapp1.0\win10-x64\project.json'.
   at Microsoft.Extensions.Configuration.UserSecrets.PathHelper.GetSecretsPath(String projectPath)
   at Microsoft.Extensions.Configuration.ConfigurationExtensions.AddUserSecrets(IConfigurationBuilder configuration)
   at xxx.Web.Startup..ctor(IHostingEnvironment env)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.Extensions.Internal.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider)
   at Microsoft.AspNetCore.Hosting.Startup.StartupLoader.LoadMethods(Type startupType, IList`1 diagnosticMessages)
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureStartup()
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices()
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
   at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
   at xxx.Web.Startup.Main(String[] args)

from usersecrets.

muratg avatar muratg commented on August 30, 2024

@Bartmax You can move project.json file manually to publish folder (or write a script to to this each time you build.)

from usersecrets.

Bartmax avatar Bartmax commented on August 30, 2024

one issue i find moving the file is that next restore will try to restore the bin folder.
this have the side effect of having more log/errors/warning when dotnet restore.
not sure if it breaks anything. just wanted to let you know.

first lines of dotnet restore, with and without project.json copied over.

dotnet restore -v minimal
log  : Restoring packages for C:\xxx\project.json...

dotnet restore -v minimal
log  : Restoring packages for C:\xxx\bin\Debug\netcoreapp1.0\win10-x64\project.json...
log  : Restoring packages for C:\xxx\project.json...

from usersecrets.

glennc avatar glennc commented on August 30, 2024

To be clear. Are people trying to use UserSecrets in production?

from usersecrets.

muratg avatar muratg commented on August 30, 2024

@glennc That's not supported. The scenario here is you're testing a published app on a dev box.

from usersecrets.

Tratcher avatar Tratcher commented on August 30, 2024

Or they're running in a staging environment and hit an error they want to debug. They enable Development to turn on the DeveloperExceptionPage, and also end up with UserSecrets being turned on.
https://github.com/aspnet/Templates/blob/dev/src/Rules/StarterWeb/IndividualAuth/Startup.cs#L65
https://github.com/aspnet/Templates/blob/dev/src/Rules/StarterWeb/IndividualAuth/Startup.cs#L30

from usersecrets.

glennc avatar glennc commented on August 30, 2024

Yeah, I understand the issue when you want to turn on debug on the server to get some errors. It makes me think this whole setup isn't quite right. IsDevelopment has too many things hanging off it.

from usersecrets.

muratg avatar muratg commented on August 30, 2024

@glennc I agree. Let's talk about this. Perhaps making things more granular makes sense.

from usersecrets.

MicahZoltu avatar MicahZoltu commented on August 30, 2024

I just spent about 4 hours troubleshooting my deploy to find out that in production (Azure App Service) I can't call .AddUserSecrets().

I believe that the AddUserSecrets() extension method should at least be made more resilient to working under adverse conditions, such as a project.json file not being available in the expected location. Not only was this a very frustrating experience (particularly on Azure where startup exceptions are surprisingly hard to find) but also it negatively impacts the startup code readability because

_configuration = new ConfigurationBuilder()
    .SetBasePath(_hostingEnvironment.ContentRootPath)
    .AddUserSecrets()
    .Build();

has to turn into this:

var configurationBuilder = new ConfigurationBuilder()
    .SetBasePath(_hostingEnvironment.ContentRootPath);
if (hostingEnvironment.IsDevelopment())
    configurationBuilder.AddUserSecrets();
_configuration = configurationBuilder.Build();

A couple related issues:
dotnet/aspnetcore#1458
dotnet/aspnetcore#1508

from usersecrets.

MicahZoltu avatar MicahZoltu commented on August 30, 2024

Workaround: Create the following extension method and use it instead of the built-in one:

public static IConfigurationBuilder AddUserSecrets(this IConfigurationBuilder it, IHostingEnvironment hostingEnvironment) => hostingEnvironment.IsDevelopment() ? it.AddUserSecrets() : it;

This doesn't solve the problem of running Development environment in something like staging, just allows you to have a slightly simplified configuration setup.

from usersecrets.

natemcmaster avatar natemcmaster commented on August 30, 2024

Another related issue: dotnet/efcore#5726

from usersecrets.

natemcmaster avatar natemcmaster commented on August 30, 2024

This issue was moved to aspnet/DotNetTools#167

from usersecrets.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.