SQLServer CLR: The type Database cannot be constructed

Topics: Data Access Application Block
Sep 7, 2011 at 4:53 PM

I am trying to install an application on a local instance of SQLServer 2008 which logs to the database.  It works just fine locally, and it works on the database without only a rolling file logger.  I've tried setting the configuration using config files and using code, but neither approach seems to be working.

My app.configs are completely commented out.

The code to generate configuration is as follows:

 

public class AppLogger
{
        static AppLogger()
        {
            var configSource = PopulateUsingExistingCode();
            EnterpriseLibraryContainer.Current =  EnterpriseLibraryContainer.CreateDefaultContainer(configSource);
        }
private static IConfigurationSource PopulateUsingExistingCode() { var builder = new ConfigurationSourceBuilder(); var configureLogging = builder.ConfigureLogging(); configureLogging.SpecialSources.LoggingErrorsAndWarningsCategory .SendTo .SetupLoggingBehavior(SourceLevels.All, "LoggingErrorsListener", LOGGING_ERRORS_LOG_FILENAME); configureLogging .WithOptions .LogToCategoryNamed(FRAMEWORK_LOGGER_NAME) .SendTo .SetupLoggingBehavior(BASE_LEVEL, "RollingListener", FRAMEWORK_LOG_FILENAME); builder.ConfigureData().ForDatabaseNamed("LoggingDb").ThatIs .ASqlDatabase().WithConnectionString( "Data Source=(local)\\;Database=Data_Sync_Dev;Initial Catalog=Logging;" + "Trusted_Connection=True;").AsDefault(); configureLogging.SpecialSources.AllEventsCategory .SendTo .Database("DbListener").UseDatabase("LoggingDb").WithAddCategoryStoredProcedure("AddCategory"). WithWriteLogStoredProcedure("WriteLog") .FormatWith( new FormatterBuilder() .TextFormatterNamed("TextFormatter") .UsingTemplate(LOGGING_TEMPLATE) ); var configSource = new DictionaryConfigurationSource(); builder.UpdateConfigurationWithReplace(configSource); return configSource; } }

The error I get when I try to log anything (within the code executed by usp_MyProc) is:

System.TypeInitializationException: The type initializer for 'Framework.Logging.AppLogger' threw an exception. ---> Microsoft.Practices.ServiceLocation.ActivationException: Activation error occured while trying to get instance of type LogWriter, key "" ---> Microsoft.Practices.Unity.ResolutionFailedException: Resolution of the dependency failed, type = "Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter", name = "(none)".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The type Database cannot be constructed. You must configure the container to supply this value.
-----------------------------------------------
At the time of the exception, the container was:

  Resolving Microsoft.Practices.EnterpriseLibrary.Logging.LogWriterImpl,LogWriter.__default__ (mapped from Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter, (none))
  Resolving parameter "structureHolder" of constructor Microsoft.Practices.EnterpriseLibrary.Logging.LogWriterImpl(Microsoft.Practices.EnterpriseLibrary.Logging.LogWriterStructureHolder structureHolder, Microsoft.Practices.EnterpriseLibrary.Logging.Instrumentation.ILoggingInstrumentationProvider instrumentationProvider, Microsoft.Practices.EnterpriseLibrary.Logging.ILoggingUpdateCoordinator updateCoordinator)
    Resolving Microsoft.Practices.EnterpriseLibrary.Logging.LogWriterStructureHolder,LogWriterStructureHolder.__default__ (mapped from Microsoft.Practices.EnterpriseLibrary.Logging.LogWriterStructureHolder, (none))
    Resolving parameter "allEventsTraceSource" of constructor Microsoft.Practices.EnterpriseLibrary.Logging.LogWriterStructureHolder(System.Collections.Generic.IEnumerable`1[[Microsoft.Practices.EnterpriseLibrary.Logging.Filters.ILogFilter, Microsoft.Practice...
2011-09-07 11:38:27.41 spid22s     The activated proc '[dbo].[usp_MyProc]' running on queue 'Data_Sync_Dev.dbo.MyQueue' output the following:  'A .NET Framework error occurred during execution of user-defined routine or aggregate "usp_MyProc": 

 

Sep 7, 2011 at 4:56 PM

To clarify, the AppLogger code is:

public class AppLogger
{
        static AppLogger()
        {
            var configSource = PopulateUsingExistingCode();
           EnterpriseLibraryContainer.ConfigureContainer(configurator, configSource);

            EnterpriseLibraryContainer.Current =  EnterpriseLibraryContainer.CreateDefaultContainer(configSource);
            LogWriter = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();
        }

        private static IConfigurationSource PopulateUsingExistingCode()
        {
            var builder = new ConfigurationSourceBuilder();

            var configureLogging = builder.ConfigureLogging();
            configureLogging.SpecialSources.LoggingErrorsAndWarningsCategory
                .SendTo
                .SetupLoggingBehavior(SourceLevels.All, "LoggingErrorsListener", LOGGING_ERRORS_LOG_FILENAME);

            configureLogging
                .WithOptions
                .LogToCategoryNamed(FRAMEWORK_LOGGER_NAME)
                .SendTo
                .SetupLoggingBehavior(BASE_LEVEL, "RollingListener", FRAMEWORK_LOG_FILENAME);

            builder.ConfigureData().ForDatabaseNamed("LoggingDb").ThatIs
                .ASqlDatabase().WithConnectionString(
                    "Data Source=(local)\\;Database=Data_Sync_Dev;Initial Catalog=Logging;" +
                    "Trusted_Connection=True;").AsDefault();

            configureLogging.SpecialSources.AllEventsCategory
                .SendTo
                .Database("DbListener").UseDatabase("LoggingDb").WithAddCategoryStoredProcedure("AddCategory").
                WithWriteLogStoredProcedure("WriteLog")
                .FormatWith(
                    new FormatterBuilder()
                        .TextFormatterNamed("TextFormatter")
                        .UsingTemplate(LOGGING_TEMPLATE)
                );
            
            var configSource = new DictionaryConfigurationSource();
            builder.UpdateConfigurationWithReplace(configSource);
            return configSource;
        }
}

Sep 8, 2011 at 5:01 AM

Hi,

I tried to copy your code to my side but it requires the configurator. It works fine when I commented it out:

EnterpriseLibraryContainer.ConfigureContainer(configurator, configSource);

What configurator did you used by the way?

 

Noel Angelo Bolasoc
Avanade Software
Avanade, Inc.
entlib.support@avanade.com

 

 

 

 

 

Sep 8, 2011 at 2:49 PM

I’m sorry. I removed a lot of code that was not relevant to the task, and it looks like I forgot to keep that piece.

The initializer should be:

static AppLogger()

{

var configSource = PopulateUsingExistingCode();

EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);

}

I wonder if the problem is a product of this restriction of the SQL Server CLR:

At runtime, the code assembly is checked for the following conditions. If any of these conditions are found, the managed code will not be allowed to run and an exception will be thrown:

Loading an assembly—either explicitly by calling the System.Reflection.Assembly.Load() method from a byte array, or implicitly through the use of Reflection.Emit namespace—is not permitted.

From: AvanadeSupport [email removed]
Sent: Thursday, September 08, 2011 12:01 AM
To: Charles Koppelman
Subject: Re: SQLServer CLR: The type Database cannot be constructed [entlib:271787]

From: AvanadeSupport

Hi,

I tried to copy your code to my side but it requires the configurator. It works fine when I commented it out:

EnterpriseLibraryContainer.ConfigureContainer(configurator, configSource);

What configurator did you used by the way?

Noel Angelo Bolasoc
Avanade Software
Avanade, Inc.
entlib.support@avanade.com

Sep 11, 2011 at 11:30 AM

Hi,

I'm not really sure about your question but it seems like its more of an SQL Server related rather than Entlib. You can try to ask this over SQL Forums like this one: http://social.msdn.microsoft.com/Forums/en/category/sqlserver

 

Noel Angelo Bolasoc
Avanade Software
Avanade, Inc.
entlib.support@avanade.com