开发者

Unity setting problem when defined in code

I have problem when asking for default ILogger from Unity container. I have this setting defined开发者_StackOverflow in code (its VB.net)

Dim container As IUnityContainer
...
container.RegisterType(Of ILogger, NullLogger)()
container.RegisterType(Of ILogger, EntLibLogger)("EL")

When I am getting ILogger from container I may have different name, like:

 Ioc.Resolve(Of ILogger)("MyLogger")

However this raises error as the mapping is not set for 'MyLogger'. Can I force container to return type which was registered without name? Actually when I used setting from web.config it worked.

Any tips most welcome. Thanks. Cheers, X.


The registered instance is registered without a name, so it is not intended to be a default/fallback instance if no entry is found.

But you could write an extension for the container to hook into the resolving process. Or you could simply write an Extension-Method for the interface IUnityContainer which handles this fallback behavior.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜