Developer forum

Forum » Development » Implementing a custom DatabaseConnectionProvider

Implementing a custom DatabaseConnectionProvider

Martijn van Tongeren
Reply

We've implemented the IDatabaseConnectionProvider interface to create a custom database connection provider which reads the connection string from the web.config.

All seems to be working fine for regular requests, but when building a repository index the follow error occures:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

 at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
   at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
   at System.Data.SqlClient.SqlConnection.Open()
   at Dynamicweb.Indexing.Builders.SqlIndexBuilder.Build(IIndexWriter writer, Tracker tracker)
   at Dynamicweb.Indexing.Index.Build(String instance, String name, Tracker tracker)
   at Dynamicweb.Indexing.IndexService.<>c__DisplayClass14_0.<BuildIndex>b__0(Tracker tracker)

When putting the database-credentials back into DW's config xml it does work.


Replies

 
Nicolai Pedersen
Reply

Seems like the IndexBuilders are not using the Connection providers - they are run inside a seperate thread and have no context - at the time they where first made, the Connection in Dynamicweb required a context that was not there - hence the different implementation. You might run into the same issue in integration jobs.

This could be a workaround:

You can make a subscriber that listens to Dynamicweb.Standard.Application.AfterDynamicwebStart and overwrite the connection string in the taskmanager context:

TaskManager.Context = New Tasks.Context() From
                                                                   {
                                                                      {"Database.ConnectionString", Database.ConnectionString}
                                                                   }

Can be found in Dynamicweb.Diagnostics.Tasks namespace

Can I ask why you want to create a provider and add the connection string in web.config - since Dynamicweb already have a .config file?

BR Nicolai

 
Martijn van Tongeren
Reply

Thanks for the workaround. I'll implement it that way for now.

Will this issue be resolved in a next release? Because having a custom DatabaseConnectionProvider isn't very useful when it isn't used everywhere.

The reason why we have a custom provider that uses the web.config instead of a Dynamicweb .config file is that we host sites in Azure where we can easily overriide web.config connection strings per environment (slot).
We're using continuous integration and if we'd be using Dynamicweb's .config files we would need to overwrite specific config-files during the build process per environment. This would also mean the build-files would differ per environment, which is not the case when using the connection strings defined in the web.config.

 

You must be logged in to post in the forum