I am trying to run a background service as service.
It works fine if i run from the .exe ....
i add it as service with sc.exe create ".NET Joke Service" binpath="C:\Path\To\App.WindowsService.exe"
on start i get error :
Value cannot be null. (Parameter 'connectionString')
Exception:
System.ArgumentNullException: Value cannot be null. (Parameter 'connectionString')
at Microsoft.EntityFrameworkCore.Utilities.Check.NotEmpty(String value, String parameterName)
at Microsoft.EntityFrameworkCore.SqlServerDbContextOptionsExtensions.UseSqlServer(DbContextOptionsBuilder optionsBuilder, String connectionString, Action`1 sqlServerOptionsAction)
at Program.<>c__DisplayClass0_0.<$>b__1(DbContextOptionsBuilder options) in C:\Users\User\source\repos\MVCWat\WorkerService\Program.cs:line 21
if i write the conn string directly in the options of program.cs without trying to retrieve it from json, error moves on the next
value im trying to retrieve from appsettings.json appsettings.json is
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"ConnectionStrings": {
"AppDb": "server=localhost\\SQL2019;Trusted_Connection=True;database=myDb"
},
"paths": {
"Ordrinipath": "D:\\aro\\Wat\\",
"OrdriniLavpath": "D:\\aro\\Wat\\",
"OrdriniClosedpath": "D:\\aro\\Wat\\",
"OrdriniArchivepath": "D:\\aro\\Wat\\ARCH\\",
"PICpath": "D:\\aro\\Wat\\"
},
"time": {
"refreshRate" : 10000
}
}
program.cs is :
IHost host = Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
IConfiguration configuration = hostContext.Configuration;
var connectionString = configuration.GetConnectionString("AppDb");
services.AddDbContext<MyConnContext>(
options => options.UseSqlServer(connectionString));
services.AddHostedService<WorkerOrAp>();
services.AddHostedService<WorkerOLav>();
services.AddHostedService<WorkerOrusi>();
services.AddScoped<IDMSDo, DmsDo>();
services.AddScoped<IXWat, XWatService>();
})
.Build();
await host.RunAsync();
AFTER ADDING TO PROGRAM
Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
event errors :
application:
Login failed for user 'NT Service\SQLTELEMETRY'. Reason: Could not find a login matching the name provided. [CLIENT: ]
system: error: The .wj service failed to start due to the following error: The service did not respond to the start or control request in a timely fashion. warning: The application-specific permission settings do not grant Local Activation permission for the COM Server application with CLSID {15698135-4EFAF-698A-B65C-44A5B7FAFFA44} and APPID {845A2E3D-11A3-CD3A-25AB-11AA2364FA2AC2FF} to the user DESKTOP-OP9LL57K\USER SID (S-1-6-12-658921568-1238569127-4569258915-7812) from address LocalHost (Using LRPC) running in the application container Unavailable SID (Unavailable). This security permission can be modified using the Component Services administrative tool.
CodePudding user response:
Win32 services start with their current working directory set to a system directory, not where there process executable is.
It's common to do something like this at the beginning of Main
:
Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
CodePudding user response:
You are not posting all of the code regarding the issue.
I see two problems that could be the source:
- right-click on
appsettings.json
and Copy to Output must read "Copy Always" - Check
apssetings.Development.json
andappsettings.Production.json
, if you have a different build you must make sure theAppDb
exists in the final execution in your build directory. - You are running the service, you must make sure that
binpath="C:\Path\To\
hasappsettings.json
and the AppDb exists in there.
I hope it helps Cheers