question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

global tool - appsettings.json

See original GitHub issue

Steps to reproduce

I am developing a global tool, and the first thing it does is in it’s entry point, is load some configuration from a local appsettings.json file.

var config = new ConfigurationBuilder()
	.SetBasePath(Directory.GetCurrentDirectory())
	.AddJsonFile("appsettings.json")
        .AddCommandLine(args)																	.Build();

My csproj looks like this:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.1</TargetFramework>    
    <AssemblyName>dotnet-foo</AssemblyName>
    <RuntimeIdentifiers>win-x64;win-x86;osx.10.10-x64;osx.10.11-x64;ubuntu.14.04-x64;ubuntu.16.04-x64;ubuntu.16.10-x64;centos.7-x64;rhel.7.2-x64;debian.8-x64;fedora.24-x64;opensuse.42.1-x64</RuntimeIdentifiers>
    <PackageType>DotnetCliTool</PackageType>
    <IsPackable>true</IsPackable>
    <PackAsTool>true</PackAsTool>
  </PropertyGroup>

<ItemGroup>
    <None Update="appsettings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>

I dotnet pack the tool and can see the appsettings.json file included along side the .dll inside the nuget package. I upload the nuget package to an internal feed. I install the tool somewhere using: dotnet tool install dotnet-foo --tool-path ./my-tool --add-source “https://packages.foo.com/nuget/App/

The tool installs successfully. However when I run the tool, it crashes as it cannot find the appsettings.json file. Looking in the tool install directory, there is no appsettings.json file.

Expected behaviour

Tool can be distributed with content files such as appsettings.json

Actual behavior

Tool is installed missing content files such as appsettings.json.

Environment data

dotnet --info output:

Version: 2.1.401 Commit: 91b1c13032

Runtime Environment: OS Name: Windows OS Version: 10.0.17134 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\2.1.401\

Host (useful for support): Version: 2.1.3 Commit: 124038c13e

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:13 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
wli3commented, Oct 28, 2018

@dazinator in the program you explicitly state the BasePath is the current directory. And once you install it on your path the CurrentDirectory depends on what is your shell at. CLI does carry the appsettings.json file but it is stored in the same directory of dlls. To access it you need to read it like the following

using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.IO;

namespace appsettingrepro
{
    class Program
    {
        static void Main(string[] args)
        {
            string assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

            var config = new ConfigurationBuilder()
	        .SetBasePath(assemblyFolder) // !!
	        .AddJsonFile("appsettings.json")
            .Build();

            var dict = new Dictionary<string, string>();
            config.GetSection("section0").Bind(dict);
            Console.WriteLine(dict["key0"]);
        }
    }
}

1reaction
dazinatorcommented, Oct 27, 2018

It looks like you installed it using --tool-path not -g for global.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Converting web.config files to appsettings.json with a . ...
If you're new to global tools, I suggest reading my previous post on building a .NET Core global tool, as well as Nate...
Read more >
Dotnet tool convert appsettings (.json) to Azure AppService ...
NET tools. Tool convert appsettings (.json) files to Azure AppService Application Settings json name-value format (support bulk updating) or Docker Compose ...
Read more >
global.json overview - .NET CLI
Learn how to use the global.json file to set the . ... NET SDK option (under Tools > Options > Environment > Preview...
Read more >
Converting web.config files to appsettings.json with a .NET ...
NET Core global tool to easily convert configuration stored in web.config files to the JSON format more commonly used for configuration in ASP....
Read more >
Configuration in ASP.NET Core
Settings files, such as appsettings.json; Environment variables ... The Secret Manager tool uses the File configuration provider to store ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found