`Unhandled Exception: System.ArgumentNullException: value cannot be a null error.` when starting` dotnet run` by running a script

I am trying to start a dotnet application using dotnet run --configuration Release on a Startup / Reboot system server. I am using the init.d script to get the same.

My running script located in /etc/init.d/myscript contains the following:

 #!/bin/sh /home/user/myscripts/botScript.sh 

Contents of botScript.sh :

 #!/bin/bash cd /home/user/bot/ nohup dotnet run --configuration Release & 

When my server starts or restarts the launch, the script starts, but dotnet run does not work. I get the following errors:

 Unhandled Exception: System.ArgumentNullException: Value cannot be null. Parameter name: path1 at System.IO.Path.Combine(String path1, String path2, String path3) at Microsoft.DotNet.ProjectModel.Resolution.PackageDependencyProvider.ResolvePackagesPath(String rootDirectory, GlobalSettings settings) at Microsoft.DotNet.Configurer.NuGetCacheSentinel.get_NuGetCachePath() at Microsoft.DotNet.Configurer.NuGetCacheSentinel.Exists() at Microsoft.DotNet.Configurer.DotnetFirstTimeUseConfigurer.ShouldPrimeNugetCache() at Microsoft.DotNet.Configurer.DotnetFirstTimeUseConfigurer.Configure() at Microsoft.DotNet.Cli.Program.ConfigureDotNetForFirstTimeUse(INuGetCacheSentinel nugetCacheSentinel) at Microsoft.DotNet.Cli.Program.ProcessArgs(String[] args, ITelemetry telemetryClient) at Microsoft.DotNet.Cli.Program.Main(String[] args) 

But all the other commands work fine in that the script and even just working dotnet works dotnet too (I checked it). It is just that dotnet run not working.

Yes, but when I run scripts, both myscript and botScript.sh , after entering my server, it works fine without errors.

Please can someone help me on this?

+5
source share
2 answers

Obviously, the error is in the PackageDependencyResolver , but to find a workaround, consider the order in which the packages folder is found:

  • global.json { "packages": "..." }
  • NUGET_PACKAGES environment variable.
  • {DefaultLocalRuntimeHomeDir}\packages

Your code drops to case number 3 above. To work around this, create an environment variable called NUGET_PACKAGES that points to your package folder. For example, in botScript.sh :

 #!/bin/bash cd /home/user/bot/ export NUGET_PACKAGES="/home/user/.nuget/packages" # <=== nohup dotnet run --configuration Release & 

See PackageDependencyResolver.cs and PackageDependencyResolver for more information . cs

+3
source

Do not start dotnet with root user

If it is launched from init.d, it is called using the root user . Running the program with root will lead to serious security problems for your computer, especially with the .net kernel, which is still in a difficult development, and yet security is not .

Starting a service with a specific user

Your error is probably due to the fact that no no packages are specified for the root directory, but when started manually, this is with your user , which has the directory /home/user/.nuget/packages .

  • From the asp.net core documentation, you can configure the service and specify the user who will start dotnet
  • Just use the su command to start dotnet with the specified user

Supervisor Users

Here is an excerpt from the supervisor for my own site

/etc/supervisor/conf.d/mysite.conf

 [program:mysite] command=/usr/bin/dotnet /var/www/mysite/MySite.dll directory=/var/www/mysite autostart=true autorestart=true stderr_logfile=/var/log/mysite.err.log stdout_logfile=/var/log/mysite.out.log environment=ASPNETCORE_ENVIRONMENT=Production user=www-data <=== SPECIFY A USER HERE stopsignal=INT 

Do not use dotnet run outside of development / debugging purpose

As dotnet --help says, the start command compiles and runs the project.

 run Compiles and immediately executes a .NET project 

Compiling wil requires additional steps, and then processing power, and therefore additional time (every time it starts, there are no code changes). It also requires recovery and build to work (which requires a normal developer user). It is also more prone to failures due to extra steps that may fail.

Instead, compile once with your developer user, and then run your program using a service user such as www-data (which is a kind of built-in user for websites).

 dotnet MyCompiledAssembly.dll 
+2
source

Source: https://habr.com/ru/post/1260381/


All Articles