6

When starting dotnet dll

 cd /
 dotnet /tmp/donetproject/donetproject.dll

the code

.AddJsonFile("hostsettings.json", optional: true)

will look at

/hostsettings.json

not

/tmp/donetproject/hostsettings.json

setting the GetCurrentDirectory has no effect

.SetBasePath(Directory.GetCurrentDirectory())

public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }
    public static IWebHostBuilder CreateWebHostBuilder(string[] args)
    {
        var config = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("hostsettings.json", optional: true)
            .AddCommandLine(args)
            .Build();

        return WebHost.CreateDefaultBuilder(args)
            .UseUrls()
            .UseConfiguration(config)
                .UseStartup<Startup>();
    }
}
Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61
Mohamed Elrashid
  • 8,125
  • 6
  • 31
  • 46
  • 1
    You want to the set the [location of the assembly](https://stackoverflow.com/questions/52797/how-do-i-get-the-path-of-the-assembly-the-code-is-in), not the location of your current workdirectory. That said, its probably better to set the workdirectory to the assembly directory, e. g. `cd /tmp/donetproject/` – Christian Gollhardt Jan 11 '19 at 01:55
  • thanks .SetBasePath(AssemblyDirectory) works – Mohamed Elrashid Jan 11 '19 at 02:51
  • can you add you add Answer so i will accept it here the code https://gist.github.com/Elrashid/1d8859d961b5035af66c11515db460e4 – Mohamed Elrashid Jan 11 '19 at 02:56

1 Answers1

7

You are currently setting the work directory

.SetBasePath(Directory.GetCurrentDirectory())

This is the directory, where you start the process /, more specificaly cd /. The directory you realy want, is the directory of your assembly:

.SetBasePath(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location))

Probably you should consider to change you working directory to the directory of your application instead, e. g. cd /tmp/donetproject/.

Christian Gollhardt
  • 16,510
  • 17
  • 74
  • 111
  • The problem with changing the working directory eg cd /tmp/donetproject/ is that it is inconvenient. I frequently test console apps by just dragging the exe to a console window which calls the exe using the full path. Automation scripts also frequently use full paths. Getting the directory of the assembly means the application can be called from anywhere. – Louise Eggleton Oct 07 '22 at 13:39