0

I'm looking to access the database from a Windows service, for which I have several projects within the solution. The Win service, by itself, initiates a timer, which executes an action every X seconds. This action is inside a class library (Business) which in turn has access to a DAL layer (in another Library class) that accesses the database.

I usually use this scheme, to access the data in the database, exceptionally in this particular case, my user interface is a windows service. This service has an app.config file, where you define the connection string for the database.

I already verified in another project (Winforms) that the connection string is working (it returns the data from the database). For the installation of the service, generate an installer in the same solution.

All this compiles without problems. And the service is installed correctly, but when I start, it tells me that the timer is running running every time you enter the database with the method described above.

My problem is that when I access, I get a message that tells me that I can not access the database. This is the message

System.Data.DataException: An exception occurred while initializing the database. See the InnerException for details.

System.Data.Entity.Core.EntityException: The underlying provider failed on Open.

System.Data.SqlClient.SqlException: No se puede abrir la base de datos "CertusAccuro_dev" solicitada por el inicio de sesión. Error de inicio de sesión.
Error de inicio de sesión del usuario 'NT AUTHORITY\SYSTEM'.

en System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) en System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) en System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) en System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) en System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) en System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource1 retry) en System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource1 retry) en System.Data.SqlClient.SqlConnection.Open() en System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.b__36(DbConnection t, DbConnectionInterceptionContext c) en System.Data.Entity.Infrastructure.Interception.InternalDispatcher1.Dispatch[TTarget,TInterceptionContext](TTarget target, Action2 operation, TInterceptionContext interceptionContext, Action3 executing, Action3 executed) en System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open(DbConnection connection, DbInterceptionContext interceptionContext) en System.Data.Entity.Core.EntityClient.EntityConnection.b__2() en System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<>c__DisplayClass1.b__0() en System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func1 operation) en System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation) en System.Data.Entity.Core.EntityClient.EntityConnection.Open() --- Fin del seguimiento de la pila de la excepción interna --- en System.Data.Entity.Core.EntityClient.EntityConnection.Open() en System.Data.Entity.Core.Objects.ObjectContext.EnsureConnection(Boolean shouldMonitorTransactions) en System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess) en System.Data.Entity.Core.Objects.ObjectQuery1.<>c__DisplayClass7.<GetResults>b__5() en System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func1 operation) en System.Data.Entity.Core.Objects.ObjectQuery1.GetResults(Nullable1 forMergeOption) en System.Data.Entity.Core.Objects.ObjectQuery1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0() en System.Data.Entity.Internal.LazyEnumerator1.MoveNext() en System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable1 source) en System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__1[TResult](IEnumerable1 sequence) en System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable1 query, Expression queryRoot) en System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[TResult](Expression expression) en System.Data.Entity.Internal.Linq.DbQueryProvider.Execute[TResult](Expression expression) en System.Linq.Queryable.FirstOrDefault[TSource](IQueryable1 source) en System.Data.Entity.Internal.EdmMetadataRepository.QueryForModelHash(Func`2 createContext) en System.Data.Entity.Internal.InternalContext.QueryForModelHash() en System.Data.Entity.Internal.ModelCompatibilityChecker.CompatibleWithModel(InternalContext internalContext, ModelHashCalculator modelHashCalculator, Boolean throwIfNoMetadata, DatabaseExi

The error translated to English means:

User login error 'NT AUTHORITY\SYSTEM'.

Is the use of the app.config in the Windows service correct? Or is there another way to access the connection string to access the database?

Help please, I'm a little lost on this topic.

Thanks in advance

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    I suspect that you are using Windows login to authenticate on your DB. You need to create a user and grant access to the DB to the SYSTEM user. – Adam Nov 24 '17 at 02:43
  • Your windows services is executing under `NT AUTHORITY\SYSTEM` and you are using Windows Authentication. And `NT AUTHORITY\SYSTEM` does not have access to the DB. You can execute the windows service under another account with access to the DB OR use SQL authentication – Squirrel Nov 24 '17 at 02:59
  • _[Why running a service as Local System is bad on windows?](https://stackoverflow.com/questions/1730486/why-running-a-service-as-local-system-is-bad-on-windows)_ and [dangerous, don't use!](https://stackoverflow.com/a/510225/585968) –  Nov 24 '17 at 03:03
  • thanks, i already fixed this problems. – Luis Gabriel Fabres Nov 24 '17 at 13:13

1 Answers1

0

The error translated to English means:

User login error 'NT AUTHORITY\SYSTEM'.

No, the error translated to English is

Login failed for user 'NT AUTHORITY\SYSTEM'. Reason: Failed to open the explicitly specified database 'CertusAccuro_dev'.

And this means that your login NT AUTHORITY\SYSTEM is not mapped to this database, or the database is not ONLINE.

The first reason seems to be most probable, so to fix it you should connect to your server as sysadmin and execute this code:

use CertusAccuro_dev;
go

create user [NT AUTHORITY\SYSTEM] from login [NT AUTHORITY\SYSTEM];
sepupic
  • 8,409
  • 1
  • 9
  • 20