开发者

Strange StructureMap behaviour in .Net 4.0

开发者 https://www.devze.com 2023-01-16 10:35 出处:网络
I have a piece of code that I can run in four ways Debug build - in the debugger - works just fine Debug build - outside debugger - fails

I have a piece of code that I can run in four ways

  1. Debug build - in the debugger - works just fine

  2. Debug build - outside debugger - fails

  3. Release build - in the debugger - fails

  4. Release build - outside debugger - fails.

The error code is gives in all cases is 202 - something to do with parameterless constructors, call stack at the bottom.

It fails at the same point in each case, when it is building the first object it is asked for.

I can't build the source with debug because I am using .Net 4.0 and don't seem to have the .Net 2.0 SD开发者_如何学JAVAK installed in order to build it.

I am starting to run out of ideas....

Exception:

Test method DataLoaders.Integration.ContactMappingTests.EntityContactMappingTest threw exception: 
StructureMap.StructureMapException: StructureMap Exception Code:  202
No Default Instance defined for PluginFamily System.Uri, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

Callstack:

StructureMap.BuildSession.<.ctor>b__0(Type t)
StructureMap.Util.Cache`2.get_Item(KEY key)
StructureMap.BuildSession.CreateInstance(Type pluginType)
StructureMap.Pipeline.DefaultInstance.build(Type pluginType, BuildSession session)
StructureMap.Pipeline.Instance.createRawObject(Type pluginType, BuildSession session)
StructureMap.Pipeline.Instance.Build(Type pluginType, BuildSession session)
StructureMap.Pipeline.ConstructorInstance.Get[T](String propertyName, BuildSession session)
StructureMap.Pipeline.Arguments.Get[T](String propertyName)
lambda_method(Closure , IArguments )
StructureMap.Construction.BuilderCompiler.FuncCompiler`1.<>c__DisplayClass2.<CreateBuilder>b__0(IArguments args)
StructureMap.Construction.InstanceBuilder.BuildInstance(IArguments args)
StructureMap.Pipeline.ConstructorInstance.Build(Type pluginType, BuildSession session, IInstanceBuilder builder)
StructureMap.Pipeline.ConstructorInstance.build(Type pluginType, BuildSession session)
StructureMap.Pipeline.SmartInstance`1.build(Type pluginType, BuildSession session)
StructureMap.Pipeline.Instance.createRawObject(Type pluginType, BuildSession session)
StructureMap.Pipeline.Instance.Build(Type pluginType, BuildSession session)
StructureMap.Pipeline.ObjectBuilder.Resolve(Type pluginType, Instance instance, BuildSession session)
StructureMap.BuildSession.CreateInstance(Type pluginType, Instance instance)
StructureMap.BuildSession.<>c__DisplayClass3.<.ctor>b__1()
StructureMap.BuildSession.CreateInstance(Type pluginType)
StructureMap.Container.GetInstance[T]()
StructureMap.ObjectFactory.GetInstance[PLUGINTYPE]()
DataLoaders.Processor.ProcessData(DataConfig config) in D:\TFS\Main\Source\DataLoaders\DataLoaders\Processor.cs: line 20


I can't explain why it works in one case and not the others.

However, I would start with looking at the System.Uri class. 4.0 added constructors to the class, which would absolutely change the way it gets resolved by the container. I believe that StructureMap will always try to use the greediest constructor by default.

The API may have changed since this post, but this get your started at least: http://codebetter.com/blogs/jeremy.miller/archive/2009/01/07/choosing-the-constructor-function-in-structuremap-without-attributes.aspx


After painful trail and error i have could reproduce the error and the reason this was happening is because of multiple registration of IRestClient inside an single instance of structure map container.

NOTE: there should be only one registration of IRestClient.

I have achieved this by removing all the duplicate registration and adding it to the parent application that is responsible for bootstrapping the app!

This is not an ideal situation but i guess we are stuck with this even with Restsharp v105.2.3.0 and StructureMap v2.6.4.0

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号