Thomas Wagner - 15 - Mar 25, 2015

Has anyone encountered this before: 
Not allowed to change the ‘ConnectionString’ property. 
The connection’s current state is open at System.Data.SqlClient.SqlConnection.ConnectionString_Set(DbConnectionPoolKey key)
at System.Data.SqlClient.SqlConnection.set_ConnectionString(String value)   
at Services.EntitiesConnectionFactory.OpenDbConnection() in c:\CI_WS\Ws\11537\MySourceCode\CapsETC\Services\Global.asax.cs:line 79

The line in question is the IDbConnection OpenDbConnection()
call thats used by ServiceStack. 

No but the exception is suggesting you’re trying to open a connection that’s already opened?

Thomas Wagner:

Right. I think I am getting closer to the problem. We use llblgen as an ORM for everything except the user auth. Seems that there is a contention for a given connection between llblgen and servicestack. 
In a worst case scenario I may have to replace the ORMLite user auth piece with an equivalent call that runs through llblgen. Its possible to swap one for the other right ? 

Sounds odd, not sure why/how LLBGen would be messing with SqlServer connection pool, is it overriding SqlServer ADO.NET connection factory or something else invasive? OrmLite is just getting a new Connection from the pool, so I’m surprised there would be a conflict. 

For Swapping the UserAuthRepo, you could implement a custom version of OrmLiteAuthRepository that uses LLBGen instead: https://github.com/ServiceStack/ServiceStack/blob/master/src/ServiceStack.Server/Auth/OrmLiteAuthRepository.cs

Thomas Wagner:

Not sure. Its only a theory and I have my guys working on a load test to try and repro. Seems it only comes up when there are a number of users on the system. Ideally I would love to be able to see it happen on a dev box. I realize its a stretch especially since every data access call in llblgen has using { } specifically intended to help free it up when the call goes out of scope. Thanks for the link !
 

Thomas Wagner:

I don’t know what to think anymore. Literally we keep getting the following message - and it only happens when there are multipkle

ErrorMessage:Not allowed to change the ‘ConnectionString’ property. The connection’s current state is open. InvalidOperationException Not allowed to change the ‘ConnectionString’ property. The connection’s current state is open. stackTrace [ProjectBatchReq: 3/25/2015 8:01:04 PM]:\n[REQUEST: {projectId:322}]\nSystem.InvalidOperationException: Not allowed to change the ‘ConnectionString’ property. The connection’s current state is open.\r\n at System.Data.SqlClient.SqlConnection.ConnectionString_Set(DbConnectionPoolKey key)\r\n   at System.Data.SqlClient.SqlConnection.set_ConnectionString(String value)\r\n   at Services.EntitiesConnectionFactory.OpenDbConnection() in c:\CI_WS\Ws\11661\MySourceCode\CapsETC\Services\Global.asax.cs:line 79\r\n 

And that line 79 points to 

public class EntitiesConnectionFactory : IDbConnectionFactory
    {
        private static SqlConnection connection = null;

        #region IDbConnectionFactory Members

        public IDbConnection CreateDbConnection()
        {
            if (connection == null)
            {
                connection = new SqlConnection();
                OrmLiteConfig.DialectProvider = new SqlServerOrmLiteDialectProvider();
            }

            return connection;
        }

        public IDbConnection OpenDbConnection()
        {

            if (connection == null)
            {
                CreateDbConnection();
            }
            if (connection.ConnectionString.IsNullOrEmpty())
            {
                connection.ConnectionString = ConfigurationManager.ConnectionStrings[“AuthRepositoryConnectionString”].ConnectionString;
            }

            if (connection.State != ConnectionState.Open)
            {
                connection.Open();
            }

            return connection;
        }

        #endregion
    }

Why are you creating your own SqlConnection? i.e. why aren’t you using OrmLiteConnectionFactory and dbFactory.OpenConnection()? and Why are you using a static SqlConnection? They’re not threadsafe so you should be getting a new Connection everytime you need it, never re-use the same connection across multiple threads.

Thomas Wagner:

See I should have posted this before!
Thank you Demis

Thomas Wagner:

We fixed the implementation and have no more errors. Thanks again Demis.