我有以下代码
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;
}
}
在这段代码中,我想模拟SampleClass的GetString方法的行为。但是,当我为此写鼹鼠时,代码抛出异常,说静态初始化时发生了一些错误。 我无法在这里得到问题。 错误消息:
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)
我怀疑的是,如果我将方法转移到为什么要创建数据库呢? 谁能告诉我这里有什么问题? 谢谢 Ashwani
答案 0 :(得分:0)
moles框架不支持由静态构造函数调用的绕行代码。方法GetString(string)
由类SampleClass
的静态构造函数调用。由于不支持绕行,GetString
表现得并不是因为它没有被mo。这样就调用了你的db工厂。
从版本v0.92.50603.1开始,有一个属性让你“擦除”静态构造函数调用。
从痣手册:
静态构造函数专门用Moles处理。有可能的 指定给定类型的静态构造函数应该是 简单地删除。这是通过 [MolesEraseStaticConstructor]属性如下:
[assembly: MolesEraseStaticConstructor(typeof(MyStatic))]
class MyStatic {
static MyStatic() {
throw new Exception(); // needs moling…
}
}