Search code examples
c#entity-frameworktopshelf

Windows service using TopShelf: The underlying provider failed on Open. User logon failure


My project is a window service create by TopShelf. It store data in local database but a error occurre:

System.Data.Entity.Core.EntityException: The underlying provider failed on Open. > System.Data.SqlClient.SqlException: User logon failure 'USER'

My string connection is:

  <connectionStrings>
    <add name="ValeQpsContext" connectionString="Data Source=.\SQLEXPRESS01;Initial Catalog=Vale.Qps;Integrated Security=False;
         User Id=USER; Password=xxxx" 
         providerName="System.Data.SqlClient" />
  </connectionStrings>

My database is a local db using SQL EXPRESS and my service run as local system.

This is method for store data in database:

public static void Insert<T>(T entity)
            where T : class
        {
            try
            {
                using (var ctx = new MyContext())
                {
                    ctx.Entry(entity).State = System.Data.Entity.EntityState.Added;

                    ctx.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

This is a complete inner exception:

The underlying provider failed on Open. System.Data.Entity.Core.EntityException: The underlying provider failed on Open. > System.Data.SqlClient.SqlException: User logon failure 'USER'. em System.Data.ProviderBase.DbConnectionPool.TryGetConnection em System.Data.ProviderBase.DbConnectionPool.TryGetConnection em System.Data.ProviderBase.DbConnectionFactory.TryGetConnection em System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal em System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection em System.Data.SqlClient.SqlConnection.TryOpenInner em System.Data.SqlClient.SqlConnection.TryOpen em System.Data.SqlClient.SqlConnection.Open em System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.<>c.b__13_0 em System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch em System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open em System.Data.Entity.Core.EntityClient.EntityConnection.b__55_0 em System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<>c__DisplayClass2_0.b__0 em System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute em System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute em System.Data.Entity.Core.EntityClient.EntityConnection.Open

The same permissions (USER and password) are define in SSMS to database. Where is error?


Solution

  • I found solution. I changed my connection string to:

      <connectionStrings><add name="MyContext" connectionString="Data Source=.\SQLEXPRESS01;Initial Catalog=MyDatabase;Integrated Security=SSPI;AttachDBFilename=C:\Program Files\Microsoft SQL Server\MSSQL15.SQLEXPRESS01\MSSQL\DATA\MyDatabase.mdf;User Id=DOMAIN\USER; Password=xxxxx" providerName="System.Data.SqlClient" /></connectionStrings>
    

    And i also change commands to install service. Added logon user:

    MyService.exe install -username:DOMAIN\USER -password:xxxx start
    

    So, this is not exacly error, but is ok now.