0

I have following code

public class SampleClass
    {
        static string sampleString = GetString("sample");

        public static string GetString(string p)
        {
            return DatabaseInteraction.GetData(p);
        }
    }

    public static class DatabaseInteraction
    {
        public static string GetData(string p)
        {
            ///In actual implementation it is call to database
            return string.Empty;
        }
    }

In this code I want to mock the behavior of GetString method of SampleClass. But, when I am writing moles for that, the code is throwing exception saying some error occurred in static initialization. I am not able to get the problem here. Error Message:

System.TypeInitializationException: The type initializer for 'MDatabaseInteraction
.GetString' threw an exception. ---> System.Configuration.ConfigurationErrorsException: The requested database dbPortalOracle is not defined in configuration.

StackTrace:
           at Microsoft.Practices.EnterpriseLibrary.Data.DatabaseConfigurationView.ValidateConnectionStringSettings(String name, ConnectionStringSettings connectionStringSettings)
            at Microsoft.Practices.EnterpriseLibrary.Data.DatabaseConfigurationView.GetConnectionStringSettings(String name)
            at Microsoft.Practices.EnterpriseLibrary.Data.DatabaseCustomFactory.CreateObject(IBuilderContext context, String name, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
            at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.ConfiguredObjectStrategy.BuildUp(IBuilderContext context, Type t, Object existing, String id)
            at Microsoft.Practices.ObjectBuilder.BuilderStrategy.BuildUp(IBuilderContext context, Type typeToBuild, Object existing, String idToBuild)
            at Microsoft.Practices.ObjectBuilder.SingletonStrategy.BuildUp(IBuilderContext context, Type typeToBuild, Object existing, String idToBuild)
            at Microsoft.Practices.ObjectBuilder.BuilderStrategy.BuildUp(IBuilderContext context, Type typeToBuild, Object existing, String idToBuild)
            at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.ConfigurationNameMappingStrategy.BuildUp(IBuilderContext context, Type t, Object existing, String id)
            at Microsoft.Practices.ObjectBuilder.BuilderBase`1.DoBuildUp(IReadWriteLocator locator, Type typeToBuild, String idToBuild, Object existing, PolicyList[] transientPolicies)
            at Microsoft.Practices.ObjectBuilder.BuilderBase`1.BuildUp(IReadWriteLocator locator, Type typeToBuild, String idToBuild, Object existing, PolicyList[] transientPolicies)
            at Microsoft.Practices.ObjectBuilder.BuilderBase`1.BuildUp[TTypeToBuild](IReadWriteLocator locator, String idToBuild, Object existing, PolicyList[] transientPolicies)
            at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.EnterpriseLibraryFactory.BuildUp[T](IReadWriteLocator locator, String id, IConfigurationSource configurationSource)
            at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.EnterpriseLibraryFactory.BuildUp[T](String id, IConfigurationSource configurationSource)
            at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.NameTypeFactoryBase`1.Create(String name)
            at Microsoft.Practices.EnterpriseLibrary.Data.DatabaseFactory.CreateDatabase(String name)

My doubt here is if I moled the method then why it is going to database creation? Can anybody tell me what is the problem here? Thanks Ashwani

Ashwani K
  • 7,880
  • 19
  • 63
  • 102
  • 1
    I'm sure the exception didn't actually just say "some error occurred in static initialization" - I suspect it had a full stack trace, including the nested exception... please post the exact exception. – Jon Skeet Oct 07 '11 at 09:04
  • Well, it looks like somewhere you've got a configuration referring to "dbPortalOracle" but you haven't given details about that elsehwere. It's hard to know more without knowing about your configuration. Look at what the type initializer for MDatabaseInteraction actually does... – Jon Skeet Oct 10 '11 at 13:10

1 Answers1

0

The moles framework does not support detouring code which is called by a static constructor. The method GetString(string) is called by the static constructor of the class SampleClass. Since detouring is no supported GetString behaves as it was not moled. Thus your db factory is called.

Since version v0.92.50603.1 there is an attribute that let's you "erase" static constructor calls.

From the moles manual:

Static constructors are treated specially with Moles. It is possible to specify that the static constructor of a given type should be simply be erased. This is done through the [MolesEraseStaticConstructor] attribute as follows:

[assembly: MolesEraseStaticConstructor(typeof(MyStatic))]
class MyStatic {
    static MyStatic() {
        throw new Exception(); // needs moling…
    }
} 
Stefan
  • 14,530
  • 4
  • 55
  • 62