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, TaskCompletionSource
1 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