Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I can't figure out how to define the default constructor (when it exists overloads) for a type in StructureMap (version 2.5) by code.

I want to get an instance of a service and the container has to inject a Linq2Sql data context instance into it.

I wrote this in my 'bootstrapper' method :

ForRequestedType<MyDataContext>().TheDefault.Is.OfConcreteType<MyDataContext>();

When I run my app, I got this error :

StructureMap Exception Code: 202
No Default Instance defined for PluginFamily MyNamespace.Data.SqlRepository.MyDataContext, MyNamespace.Data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

If I comment out all Linq2Sql generated contructors that I don't need, it works fine.

Update : Oh, and I forgot to say that I would not use the [StructureMap.DefaultConstructor] attribute.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
340 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can specify a constructor with the ConstructedBy(). Please try this:

ForRequestedType<MyDataContext>().TheDefault.
Is.ConstructedBy(() => new MyDataContext());

This worked for me.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...