4

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
  • 23,159
  • 5
  • 39
  • 56
Mohamed Elrashid
  • 7,157
  • 6
  • 27
  • 45
  • 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

5

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
  • 15,685
  • 16
  • 74
  • 107