Last week I started updating EPiServer platform to version 8. The nuget packages were installed without problems, but the update-EPiDatabase powershell command failed because of SQL execution timeout. I was able to run all SQL script via SQL Management Studio. And I did it, but on my local machine only. Of course, I had to upgrade EPiServer database on other developers computers, test, staging and production servers. I was thinking about how to inject custom command with increased timeout.
Running one of the scripts (EPiServer 8.6.0.sql) took about 2,5 minutes. The time was that long because tblActivity table contains more than 3 mln rows. It’s not possible to increase CommandTimeout directly through ConnectionString. This property have to be assigned on the Command object.

EPiDeploy architecture

I looked into EpiDeploy tool code and found out that fortunately it use DbProviderFactory class for creating instance of database connection. DbProviderFactory reads providerName from connectionstring, map this information to assembly type and then instantinate the database connection. Usually System.Data.SqlClient is used as a provider.

With given SqlConnection provider the epiupdate tool is able to create SqlCommand object. By default SqlCommand timeout is 30 seconds which is not enough. My idea was to replace the factory via providername.

Custom connection factory

The custom provider will be defined in new class library. It will contains just two classes:

  • ConfigurableSqlClientFactory – custom connection factory
  • ConfigurableSqlConnection – custom connection

The new provider class should inherit from DbProviderFactory class and implement all necessary methods. We cannot simply derive from SqlClientFactory because this class is sealed.
Most of the methods will reuse implementations from System.Data.SqlClient namespace. The only two exceptions are CreateConnection and CreateCommand methods.

We also need to create ConfigurableSqlConnection class that will work similar to SqlConnection class. The CreateDbCommand method is protected in SqlConnection class, so it will be executed by reflection.

Project output DLL should be placed in epideploy tool directory. Then the application will load library on demand.

Configuring factory

Now the custom factory should be used in EPiServer ConnectionString. The providerName will be set to “ConfigurableConnection”. This name has to be correctly mapped to a .NET type in the application.
Mapping is stored in application configuration file under DbProviderFactories section. So the next step is to create epideploy.exe.config manually and configure provider mapping.

In the end the directory should contain application configuration file and custom provider library.

custom command provider

Full implementation of custom provider classes are available on Gist.