简体   繁体   中英

Database connection not working when called through wcf service

My application has a DAL layer with a working local database. I have tests interacting with the db and they work fine.

I also have a wcf service that uses the functions provided by my DAL. However, when the client application calls the service, no data gets retrieved. Instead, I get an exception:

Message = "The underlying provider failed on Open."

"System.Data.Entity"

StackTrace = "at System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean openCondition, DbConnection storeConnectionToOpen, DbConnection originalConnection, String exceptionCode, String attemptedOperation, Boolean& closeStoreConnectionOnFailure)\\r\\n ...

With an inner exception:

"Unable to connect to any of the specified MySQL hosts."

{"No such host is known"}

" at MySql.Data.MySqlClient.NativeDriver.Open()\\r\\n at MySql.Data.MySqlClient.Driver.Open()\\r\\n at MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings)\\r\\n at MySql.Data.MySqlClient.MySqlPool.GetPooledConnection()\\r\\n at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver()\\r\\n at MySql.Data.MySqlClient.MySqlPool.GetConnection()\\r\\n at MySql.Data.MySqlClient.MySqlConnection.Open()\\r\\n at System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean openCondition, DbConnection storeConnectionToOpen, DbConnection originalConnection, String exceptionCode, String attemptedOperation, Boolean& closeStoreConnectionOnFailure)"

I verified that my connection string is the same as what my tests use.

<add name="loansEntities" connectionString="metadata=res://*/LoansDbModel.csdl|res://*/LoansDbModel.ssdl|res://*/LoansDbModel.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot;server=localhost:3306;User Id=root;password=123456;database=dbname;Persist Security Info=True&quot;" providerName="System.Data.EntityClient"/>

The database is a local mysql database, the user and database both exist. The services are also local, running in IIS Express.

Why am I getting this exception?

This could be due to several reasons please check below points:

  • If you are using System.Data.SqlClient in your connection then change it to System.Data.Odbc
  • Check database name
  • Check you port is open or not. (probably 3306)
  • Change your bind-address in your my.ini file (from 127.0.0.1 to 0.0.0.0)
  • Check the user your trying to connect to server has necessary privileges

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM