I have been using StructureMap for about 6 months; you would think that it will start to get easier. This does not seem to be the case.
Here is the first line of my registry:
For<IDbConnection>()
.Singleton()
.Use<SqlConnection>()
.Ctor<string>(WebConfigurationManager.ConnectionStrings["UnifiedConnectionString"].ConnectionString);
It compiles and runs. But when I try to use this interface, like this:
return MsSqlConfiguration.MsSql2008.ConnectionString(((DbConnection)ObjectFactory.GetInstance<IDbConnection>()).ConnectionString);
I get
StructureMap Exception Code: 202
No Default Instance defined for PluginFamily System.Data.IDbConnection, System.Data,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Now something may be missing for me (well, I obviously have), but I do not know how much I can explain how to determine the default instance for IDbConnection. What am I doing wrong?
Edit: At Joshua's request, I am adding the complete code for a minimal case.
, . , SM, . , , , , .
-, :
static void Main(string[] args)
{
Debug.Print("Connection String: {0}", ConfigurationManager.ConnectionStrings["UnifiedConnectionString"].ConnectionString);
(new Bootstrapper()).BootstrapStructureMap();
}
Bootstrapper, :
public class Bootstrapper : IBootstrapper
{
public void BootstrapStructureMap()
{
ObjectFactory.Initialize(x =>
x.Scan(s =>
{
s.WithDefaultConventions();
s.TheCallingAssembly();
s.LookForRegistries();
}
)
);
Debug.Print(ObjectFactory.WhatDoIHave());
try
{
ObjectFactory.AssertConfigurationIsValid();
}
catch (StructureMapConfigurationException ex)
{
string msg = ex.ToString();
throw;
}
catch (Exception ex)
{
string msg = ex.ToString();
throw;
}
InitAutoMapper();
}
, , DependencyRegistry, , , , .
public class DependencyRegistry : Registry
{
public DependencyRegistry()
{
For<IDbConnection>()
.Singleton()
.Use<SqlConnection>()
.Ctor<string>(ConfigurationManager.ConnectionStrings["UnifiedConnectionString"].ConnectionString);
}
}
, , "WhatDoIHave()", SM 205 AssertConfigurationIsValid(). (-) , SqlConnection, " " connectionString\ "InstanceKey".
:
StructureMap.Exceptions.StructureMapConfigurationException:
'a54d3ca1-33b5-4100-82d4-13e458f57a3f' ( System.Data.SqlClient.SqlConnection, System.Data, Version = 2.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089) PluginType System.Data.IDbConnection, System.Data, Version = 2.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089
StructureMap.StructureMapException: StructureMap: 205
"connectionString" "a54d3ca1-33b5-4100-82d4-13e458f57a3f" StructureMap.Pipeline.ConstructorInstance. <.ctor > b__0 ( String) StructureMap.Util.Cache 2.get_Item(KEY key)
at StructureMap.Pipeline.ConstructorInstance.Get(String propertyName, Type pluginType, BuildSession session)
at StructureMap.Pipeline.ConstructorInstance.Get[T](String propertyName, BuildSession session)
at StructureMap.Pipeline.Arguments.Get[T](String propertyName)
at lambda_method(ExecutionScope , IArguments )
at StructureMap.Construction.BuilderCompiler.FuncCompiler 1. < > c__DisplayClass2.b__0 (IArguments args)
... (, )...
: 1 / 0
StructureMap.Container.AssertConfigurationIsValid() StructureMap.ObjectFactory.AssertConfigurationIsValid() ConsoleIoCTest.Bootstrapper.BootstrapStructureMap() U:\dave\VS2008Projects\FMSWeb\FMSWeb\ConsoleIoCTest\Bootstrapper.cs: 29
? . , "connectionString", .ctor?
, , .