StructureMap Exception Code: 400

305 views
Skip to first unread message

Atlantez

unread,
Mar 24, 2009, 10:19:29 AM3/24/09
to structuremap-users
Hello everyone,

Pretty virgin user of StructureMap but so far everything has been
squeaky clean and pretty straight forward. I have had some error codes
before but they were listed on the site so I could figure out exactly
what was wrong.

However so far I have been able to find very very little information
about Code 400 and what might cause this. When I take a look at the
complete stacktrace it shows me this (The exception is given to me
through the WCF service that I called, the services are using
StructureMap to do their work):

System.ServiceModel.FaultException`1
[System.ServiceModel.ExceptionDetail]:
StructureMap Exception Code: 400
Unknown error (Fault Detail is equal to An ExceptionDetail, likely
created by IncludeExceptionDetailInFaults=true, whose value is:
StructureMap.StructureMapException: StructureMap Exception Code: 400
Unknown error ----> System.ArgumentException: An item with the same
key has already been added.
at System.ThrowHelper.ThrowArgumentException(ExceptionResource
resource)
at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue
value, Boolean add)
at StructureMap.Util.Cache`2.Retrieve(KEY key)
at StructureMap.Pipeline.ConfiguredInstanceBase`1.build(Type
pluginType, BuildSession session)
at StructureMap.Pipeline.SmartInstance`1.build(Type pluginType,
BuildSession session)
at StructureMap.Pipeline.Instance.createRawObject(Type pluginType,
BuildSession session)
--- End of inner ExceptionDetail stack trace ---
at StructureMap.Pipeline.Instance.createRawObject(Type pluginType,
BuildSession session)
at StructureMap.Pipeline.Instance.Build(Type pluginType,
BuildSession session)
at StructureMap.Pipeline.BuildPol...).

So from this (truncated) stack trace I can quickly see that a key has
already been added to the dictionary, great! .....hmmmm cheered too
soon, exception message is not telling me WHICH key that was! So now I
still don't know where in my code or config I need to look for my
silly mistake! (Recompiling SM source doesn't seem such a great idea
to me...).

So I guess my question would be:

1) How can I find out exactly what part is causing this exception (I
get it rarely if not maybe once a day) ?

2) Should StructureMap not check first if the key exists before
attempting to add it? (This is from my perspective, since I have no
real clue as to how or why SM does what it does, so my logic != your
logic ;-)

3) Could I be that I am using the wrong type of caching for WCF?

For completeness sake here is the relevant code that I have for
StructureMap:

In my singleton object I call this code:

ObjectFactory.Initialize(x =>
{
// Not sure if this line is usefull to me just copied
it from the examples sorry :-\
x.Scan(scanner =>
{
scanner.TheCallingAssembly();
scanner.WithDefaultConventions();
});

// These lines are referring to the registry classes
that contain the PluggedTypes
x.AddRegistry(new ServiceRegistry());
x.AddRegistry(new RepositoryRegistry());
});

In ServiceRegistry.cs I have this code:

ForRequestedType<ICardExchangeService>
().TheDefault.Is.OfConcreteType<CardExchangeService>();
ForRequestedType<IRequestService>
().TheDefault.Is.OfConcreteType<RequestService>();
ForRequestedType<IEntityService>
().TheDefault.Is.OfConcreteType<EntityService>();

In RepositoryRegistry.cs I have this code:

ForRequestedType<DataContext>().TheDefault.IsThis(new
GatewayDataContext(ConnectionString));
ForRequestedType<IDataContext>
().TheDefault.Is.OfConcreteType<LinqToSqlContext>();

ForRequestedType<IRepository>
().TheDefault.Is.OfConcreteType<BaseRepository>();
ForRequestedType<IRequestRepository>
().TheDefault.Is.OfConcreteType<RequestRepository>();
ForRequestedType<ICardExchangeRepository>
().TheDefault.Is.OfConcreteType<CardExchangeRepository>();

Not sure what else I could include to provide relevant information to
the error that I am getting. Right now I feel "stuck" because I don't
know how or where to look for additional information to figure out why
that error is coming up once every blue moon.

Thanks alot for your time and effort :)

~ Josh

John Teague

unread,
Mar 24, 2009, 11:02:46 AM3/24/09
to structure...@googlegroups.com
start with printing ObjectFactory.WhatDoIHave() to see what is in the container.  That's the first step.  I will look closer at your setup and see if anything jumps out at me.

Atlantez

unread,
Mar 25, 2009, 5:07:59 AM3/25/09
to structuremap-users
Thank you for youu reply John,

I can understand the reasons for printing out the contents, but I am
not entirely sure how that could assist me in finding out which key it
is trying to add again?

Surely the 400 Exception would be raised before I would have had the
chance to print out it's contents? Since SM does not allow duplicate
keys, printing out the WhatDoIHave() would never list the same
PluggedType twice right? Would have been so helpfull if that 400
exception would say: An item with the same key 'KEYNAME' has already
been added.

At least that would have told me the PluggedType straight up. Should I
be clearing out the registered registries before adding them? Since I
am using this in conjunction with WCF (I have 3 services running that
all use the same singleton instance of InstanceFactory). Could it be
that when Service A initializes ObjectFactory and then if Service B or
C is called after it's trying to initialize it again? Thus causing it
to re-add the registries? All I do in the singleton object is this:

public class InstanceFactory
{
private static readonly InstanceFactory _instance = new
InstanceFactory();

private InstanceFactory()
{
Initialize();
}

public static T GetInstance<T>()
{
return _instance.Create<T>();
}

private T Create<T>()
{
return ObjectFactory.GetInstance<T>();
}

private void Initialize()
{
ObjectFactory.Initialize(x =>
{
x.Scan(scanner =>
{
scanner.TheCallingAssembly();
scanner.WithDefaultConventions();
});

x.AddRegistry(new ServiceRegistry());
x.AddRegistry(new RepositoryRegistry());
});
}
}

In each of my services I perform a call to InstanceFactory to get and
use the designated service of choice:

public Card AddCard(CardInfo request)
{
var cardService =
InstanceFactory.GetInstance<ICardExchangeService>();
return cardService.AddCard(request);
}

These are the only places InstanceFactory is used and thus
ObjectFactory.GetInstance<T>() is called. It might very well be that
in conjunction with WCF having a singleton instance to deal with
ObjectFactory is a bad bad choice or perhaps I should add explicit
Cache rules for each PluggedType that are more in line with WCF
behavior that I am un aware off?

Sorry for the long responses, I have issues with keeping things
short ;-)
> > ~ Josh- Hide quoted text -
>
> - Show quoted text -

JamesSpibey

unread,
Mar 30, 2009, 7:55:50 AM3/30/09
to structuremap-users
I'm having the same issue - would you mind taking a look at the output
from WhatDoIHave() for our system?

Thanks

James

-------------------

Message:
===========================================================================================================
Configuration Sources:

0) Registry: StructureMap.InitializationExpression,StructureMap
1) Registry:
Contoso.Catalogue.Services.CatalogueRegistry,CatalogueBusinessLogic
2) Registry:
Contoso.Catalogue.Data.DBServiceRegistry,CatalogueRepositorySqlServer


=========================================================================================================================================================================================================================================================================================================================================================================================================================================
PluginType
Name
Description
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Contoso.Catalogue.Services.IContextService, CatalogueBusinessLogic,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Contoso.Catalogue.Services.WebContextService, CatalogueBusinessLogic,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null Configured
Contoso.Catalogue.Services.WebContextService, CatalogueBusinessLogic,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
Built by: StructureMap.Pipeline.SingletonPolicy /
StructureMap.Pipeline.BuildPolicy

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Contoso.BaseClasses.ICacheService, BaseClasses,
Version=1.0.3376.13777, Culture=neutral,
PublicKeyToken=null
0d4b3352-6bfc-4d8a-
b8fb-16b6f5dc6e52
Instance is created by Func<object> function: System.Func`2
[StructureMap.IContext,Contoso.BaseClasses.ICacheService]
Built by: StructureMap.Pipeline.BuildPolicy

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Contoso.Catalogue.Services.IConfigService, CatalogueBusinessLogic,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Contoso.Catalogue.Services.ConfigService, CatalogueBusinessLogic,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null Configured
Contoso.Catalogue.Services.ConfigService, CatalogueBusinessLogic,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Built by: StructureMap.Pipeline.BuildPolicy

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Contoso.BaseClasses.IExceptionManager, BaseClasses,
Version=1.0.3376.13777, Culture=neutral,
PublicKeyToken=null ExceptionManager,
CatalogueWeb, Version=1.0.3376.13778, Culture=neutral,
PublicKeyToken=null
Configured ExceptionManager, CatalogueWeb, Version=1.0.3376.13778,
Culture=neutral,
PublicKeyToken=null
Built by: StructureMap.Pipeline.BuildPolicy

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
IMVCExceptionManager, CatalogueWeb, Version=1.0.3376.13778,
Culture=neutral,
PublicKeyToken=null
ExceptionManager, CatalogueWeb, Version=1.0.3376.13778,
Culture=neutral,
PublicKeyToken=null
Configured ExceptionManager, CatalogueWeb, Version=1.0.3376.13778,
Culture=neutral,
PublicKeyToken=null
Built by: StructureMap.Pipeline.BuildPolicy

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Contoso.BaseClasses.ILogger, BaseClasses, Version=1.0.3376.13777,
Culture=neutral,
PublicKeyToken=null
Contoso.BaseClasses.Log4NetLogger, BaseClasses,
Version=1.0.3376.13777, Culture=neutral,
PublicKeyToken=null
Contoso.BaseClasses.Log4NetLogger, BaseClasses,
Version=1.0.3376.13777, Culture=neutral,
PublicKeyToken=null
Built by: StructureMap.Pipeline.HybridBuildPolicy

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Contoso.BaseClasses.IValidationService, BaseClasses,
Version=1.0.3376.13777, Culture=neutral,
PublicKeyToken=null
Contoso.BaseClasses.ValidationService, BaseClasses,
Version=1.0.3376.13777, Culture=neutral,
PublicKeyToken=null
Contoso.BaseClasses.ValidationService, BaseClasses,
Version=1.0.3376.13777, Culture=neutral,
PublicKeyToken=null
Built by: StructureMap.Pipeline.BuildPolicy

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Contoso.Catalogue.Services.IMembershipService, CatalogueBusinessLogic,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Contoso.Catalogue.Services.MembershipService, CatalogueBusinessLogic,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null Configured
Contoso.Catalogue.Services.MembershipService, CatalogueBusinessLogic,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
Built by: StructureMap.Pipeline.BuildPolicy

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Contoso.Catalogue.Services.IAuthorisationService,
CatalogueBusinessLogic, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Contoso.Catalogue.Services.AuthorisationService,
CatalogueBusinessLogic, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null Configured
Contoso.Catalogue.Services.AuthorisationService,
CatalogueBusinessLogic, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Built by: StructureMap.Pipeline.BuildPolicy

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Contoso.Catalogue.Services.ISoloAuthenticationService,
CatalogueBusinessLogic, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Contoso.Catalogue.Services.SOLOAuthenticationService,
CatalogueBusinessLogic, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Contoso.Catalogue.Services.SOLOAuthenticationService,
CatalogueBusinessLogic, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Built by: StructureMap.Pipeline.BuildPolicy

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Contoso.Catalogue.Services.IOIDAuthenticationService,
CatalogueBusinessLogic, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Contoso.Catalogue.Services.OIDAuthenticationService,
CatalogueBusinessLogic, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null Configured
Contoso.Catalogue.Services.OIDAuthenticationService,
CatalogueBusinessLogic, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Built by: StructureMap.Pipeline.BuildPolicy

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Contoso.Catalogue.Services.IFTSSearchService, CatalogueBusinessLogic,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Contoso.Catalogue.Services.IronySearchService, CatalogueBusinessLogic,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Contoso.Catalogue.Services.IronySearchService, CatalogueBusinessLogic,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Built by: StructureMap.Pipeline.BuildPolicy

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Contoso.Catalogue.Services.ISearchSuggestService,
CatalogueBusinessLogic, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Contoso.Catalogue.Services.SearchSuggestService,
CatalogueBusinessLogic, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null Configured
Contoso.Catalogue.Services.SearchSuggestService,
CatalogueBusinessLogic, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Built by: StructureMap.Pipeline.BuildPolicy

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Contoso.Catalogue.Services.ISearchSanitiseService,
CatalogueBusinessLogic, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Contoso.Catalogue.Services.SearchSanitiseService,
CatalogueBusinessLogic, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Contoso.Catalogue.Services.SearchSanitiseService,
CatalogueBusinessLogic, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Built by: StructureMap.Pipeline.BuildPolicy

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Contoso.Catalogue.Services.IMailService, CatalogueBusinessLogic,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Contoso.Catalogue.Services.SoloMailService, CatalogueBusinessLogic,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Contoso.Catalogue.Services.SoloMailService, CatalogueBusinessLogic,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Built by: StructureMap.Pipeline.BuildPolicy

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Contoso.Catalogue.Services.ITemplateService, CatalogueBusinessLogic,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Contoso.Catalogue.Services.TemplateService, CatalogueBusinessLogic,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null Configured
Contoso.Catalogue.Services.TemplateService, CatalogueBusinessLogic,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Built by: StructureMap.Pipeline.BuildPolicy

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Contoso.Catalogue.Services.IExportService, CatalogueBusinessLogic,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Contoso.Catalogue.Services.ExportService, CatalogueBusinessLogic,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null Configured
Contoso.Catalogue.Services.ExportService, CatalogueBusinessLogic,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Built by: StructureMap.Pipeline.BuildPolicy

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Contoso.Catalogue.Services.IOfflineExportService,
CatalogueBusinessLogic, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Contoso.Catalogue.Services.OfflineExportService,
CatalogueBusinessLogic, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null Configured
Contoso.Catalogue.Services.OfflineExportService,
CatalogueBusinessLogic, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Built by: StructureMap.Pipeline.BuildPolicy

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Contoso.Catalogue.Services.ICaptchaService, CatalogueBusinessLogic,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Contoso.Catalogue.Services.CaptchaService, CatalogueBusinessLogic,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null Configured
Contoso.Catalogue.Services.CaptchaService, CatalogueBusinessLogic,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Built by: StructureMap.Pipeline.BuildPolicy

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Contoso.Catalogue.Services.ICatalogueService, CatalogueBusinessLogic,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Contoso.Catalogue.Services.CatalogueService, CatalogueBusinessLogic,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null Configured
Contoso.Catalogue.Services.CatalogueService, CatalogueBusinessLogic,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
Built by: StructureMap.Pipeline.BuildPolicy

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Contoso.Catalogue.Services.IProductService, CatalogueBusinessLogic,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Contoso.Catalogue.Services.ProductService, CatalogueBusinessLogic,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null Configured
Contoso.Catalogue.Services.ProductService, CatalogueBusinessLogic,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Built by: StructureMap.Pipeline.BuildPolicy

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Contoso.Catalogue.Services.IContractService, CatalogueBusinessLogic,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Contoso.Catalogue.Services.ContractService, CatalogueBusinessLogic,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null Configured
Contoso.Catalogue.Services.ContractService, CatalogueBusinessLogic,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Built by: StructureMap.Pipeline.BuildPolicy

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Contoso.Catalogue.Services.IAuditService, CatalogueBusinessLogic,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Contoso.Catalogue.Services.AuditService, CatalogueBusinessLogic,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null Configured
Contoso.Catalogue.Services.AuditService, CatalogueBusinessLogic,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Built by: StructureMap.Pipeline.BuildPolicy

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Contoso.Catalogue.Data.ICatalogueRepository, CatalogueBusinessLogic,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Contoso.Catalogue.Data.SqlCatalogueRepository,
CatalogueRepositorySqlServer, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null Configured
Contoso.Catalogue.Data.SqlCatalogueRepository,
CatalogueRepositorySqlServer, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Built by: StructureMap.Pipeline.BuildPolicy

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Contoso.Catalogue.Data.IProductRepository, CatalogueBusinessLogic,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Contoso.Catalogue.Data.SqlProductRepository,
CatalogueRepositorySqlServer, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null Configured
Contoso.Catalogue.Data.SqlProductRepository,
CatalogueRepositorySqlServer, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Built by: StructureMap.Pipeline.BuildPolicy

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Contoso.Catalogue.Data.IUserRepository, CatalogueBusinessLogic,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Contoso.Catalogue.Data.SqlUserRepository,
CatalogueRepositorySqlServer, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null Configured
Contoso.Catalogue.Data.SqlUserRepository,
CatalogueRepositorySqlServer, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Built by: StructureMap.Pipeline.BuildPolicy

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Contoso.Catalogue.Data.INavigationRepository, CatalogueBusinessLogic,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Contoso.Catalogue.Data.SqlNavigationRepository,
CatalogueRepositorySqlServer, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null Configured
Contoso.Catalogue.Data.SqlNavigationRepository,
CatalogueRepositorySqlServer, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Built by: StructureMap.Pipeline.BuildPolicy

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Contoso.Catalogue.Data.IAuditRepository, CatalogueBusinessLogic,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Contoso.Catalogue.Data.SqlAuditRepository,
CatalogueRepositorySqlServer, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null Configured
Contoso.Catalogue.Data.SqlAuditRepository,
CatalogueRepositorySqlServer, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Built by: StructureMap.Pipeline.BuildPolicy

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Contoso.Catalogue.Data.ITemplateRepository, CatalogueBusinessLogic,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Contoso.Catalogue.Data.SqlTemplateRepository,
CatalogueRepositorySqlServer, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null Configured
Contoso.Catalogue.Data.SqlTemplateRepository,
CatalogueRepositorySqlServer, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Built by: StructureMap.Pipeline.BuildPolicy

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Contoso.Catalogue.Data.IFinancialCalendarRepository,
CatalogueBusinessLogic, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Contoso.Catalogue.Data.SqlFinancialCalendarRepository,
CatalogueRepositorySqlServer, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null Configured
Contoso.Catalogue.Data.SqlFinancialCalendarRepository,
CatalogueRepositorySqlServer, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Built by: StructureMap.Pipeline.BuildPolicy

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Contoso.Catalogue.Data.ISearchRepository, CatalogueBusinessLogic,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Contoso.Catalogue.Data.SqlSearchRepository,
CatalogueRepositorySqlServer, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null Configured
Contoso.Catalogue.Data.SqlSearchRepository,
CatalogueRepositorySqlServer, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Built by: StructureMap.Pipeline.BuildPolicy

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Contoso.Catalogue.Data.IConfigRepository, CatalogueBusinessLogic,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Contoso.Catalogue.Data.SqlConfigRepository,
CatalogueRepositorySqlServer, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null Configured
Contoso.Catalogue.Data.SqlConfigRepository,
CatalogueRepositorySqlServer, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Built by: StructureMap.Pipeline.BuildPolicy

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Contoso.Catalogue.Data.IOfflineExportRepository,
CatalogueBusinessLogic, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Contoso.Catalogue.Data.SqlOfflineExportRepository,
CatalogueRepositorySqlServer, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null Configured
Contoso.Catalogue.Data.SqlOfflineExportRepository,
CatalogueRepositorySqlServer, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Built by: StructureMap.Pipeline.BuildPolicy

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Contoso.Catalogue.Data.SqlRepository.DB, CatalogueRepositorySqlServer,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null 299ca5bd-59c0-4b75-a368-
e8dd79d82828
Instance is created by Func<object> function: System.Func`2
[StructureMap.IContext,Contoso.Catalogue.Data.SqlRepository.DB]
Built by: StructureMap.Pipeline.HybridBuildPolicy

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Contoso.Catalogue.Data.SqlRepository.WriteableDB,
CatalogueRepositorySqlServer, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null 0f4d0fc9-7fec-4b8a-
a2bf-3bb17cd9e74d
Instance is created by Func<object> function: System.Func`2
[StructureMap.IContext,Contoso.Catalogue.Data.SqlRepository.WriteableDB]
Built by: StructureMap.Pipeline.HybridBuildPolicy

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Contoso.Catalogue.Web.Controllers.StructureMapControllerFactory,
CatalogueWeb, Version=1.0.3376.13778, Culture=neutral,
PublicKeyToken=null
DEFAULT
Configured
Contoso.Catalogue.Web.Controllers.StructureMapControllerFactory,
CatalogueWeb, Version=1.0.3376.13778, Culture=neutral,
PublicKeyToken=null
Built by: StructureMap.Pipeline.BuildPolicy

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Contoso.Catalogue.Web.Controllers.SearchController, CatalogueWeb,
Version=1.0.3376.13778, Culture=neutral,
PublicKeyToken=null
DEFAULT
Configured Contoso.Catalogue.Web.Controllers.SearchController,
CatalogueWeb, Version=1.0.3376.13778, Culture=neutral,
PublicKeyToken=null
Built by: StructureMap.Pipeline.BuildPolicy

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Contoso.Catalogue.Web.Controllers.HomeController, CatalogueWeb,
Version=1.0.3376.13778, Culture=neutral,
PublicKeyToken=null
DEFAULT
Configured Contoso.Catalogue.Web.Controllers.HomeController,
CatalogueWeb, Version=1.0.3376.13778, Culture=neutral,
PublicKeyToken=null
Built by: StructureMap.Pipeline.BuildPolicy

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Contoso.Catalogue.Web.Controllers.ContractController, CatalogueWeb,
Version=1.0.3376.13778, Culture=neutral,
PublicKeyToken=null
DEFAULT
Configured Contoso.Catalogue.Web.Controllers.ContractController,
CatalogueWeb, Version=1.0.3376.13778, Culture=neutral,
PublicKeyToken=null
Built by: StructureMap.Pipeline.BuildPolicy

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Contoso.Catalogue.Services.OIDUtility, CatalogueBusinessLogic,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
DEFAULT
Configured Contoso.Catalogue.Services.OIDUtility,
CatalogueBusinessLogic, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null
Built by: StructureMap.Pipeline.BuildPolicy

=========================================================================================================================================================================================================================================================================================================================================================================================================================================

User: (null)[(null)]
Trust: (null)/(null)
SessionID: (null)
Client Address: (null)
Server Address: (null)
Current Url: (null)
Referrer: (null)


Hostname: SC-WS03
Logger:
StructureMapControllerFactory:GetControllerInstance
Thread: 12
Date: 2009-03-30 08:46:07,433
-----------------------------------
Stack Trace

StructureMap.StructureMapException: StructureMap Exception Code: 400
Unknown error ---> System.ArgumentException: An item with the same key
has already been added.
at System.ThrowHelper.ThrowArgumentException(ExceptionResource
resource)
at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue
value, Boolean add)
at StructureMap.Util.Cache`2.Retrieve(KEY key)
at StructureMap.Pipeline.ConfiguredInstanceBase`1.build(Type
pluginType, BuildSession session)
at StructureMap.Pipeline.Instance.createRawObject(Type pluginType,
BuildSession session)
--- End of inner exception stack trace ---
at StructureMap.Pipeline.Instance.createRawObject(Type pluginType,
BuildSession session)
at StructureMap.Pipeline.Instance.Build(Type pluginType,
BuildSession session)
at StructureMap.Pipeline.BuildPolicy.Build(BuildSession
buildSession, Type pluginType, Instance instance)
at StructureMap.InstanceFactory.Build(BuildSession session,
Instance instance)
at StructureMap.BuildSession.CreateInstance(Type pluginType,
Instance instance)
at StructureMap.BuildSession.<.ctor>b__0(Type t)
at StructureMap.Util.Cache`2.Retrieve(KEY key)
at StructureMap.BuildSession.CreateInstance(Type pluginType)
at StructureMap.Pipeline.DefaultInstance.build(Type pluginType,
BuildSession session)
at StructureMap.Pipeline.Instance.createRawObject(Type pluginType,
BuildSession session)
at StructureMap.Pipeline.Instance.Build(Type pluginType,
BuildSession session)
at StructureMap.Pipeline.ConfiguredInstanceBase`1.getChild(String
propertyName, Type pluginType, BuildSession buildSession)
at
StructureMap.Pipeline.ConfiguredInstanceBase`1.StructureMap.Pipeline.IConfiguredInstance.GetChild
(String propertyName, Type pluginType, BuildSession buildSession)
at
NHSCatalogueServicesProductServiceInstanceBuilder4de2028062ca4c83b80322b4eefba7c2.BuildInstance
(IConfiguredInstance , BuildSession )
at
StructureMap.Pipeline.ConfiguredInstanceBase`1.StructureMap.Pipeline.IConfiguredInstance.Build
(Type pluginType, BuildSession session, InstanceBuilder builder)
at StructureMap.Pipeline.ConfiguredInstanceBase`1.build(Type
pluginType, BuildSession session)
at StructureMap.Pipeline.Instance.createRawObject(Type pluginType,
BuildSession session)
at StructureMap.Pipeline.Instance.Build(Type pluginType,
BuildSession session)
at StructureMap.Pipeline.BuildPolicy.Build(BuildSession
buildSession, Type pluginType, Instance instance)
at StructureMap.InstanceFactory.Build(BuildSession session,
Instance instance)
at StructureMap.BuildSession.CreateInstance(Type pluginType,
Instance instance)
at StructureMap.BuildSession.<.ctor>b__0(Type t)
at StructureMap.Util.Cache`2.Retrieve(KEY key)
at StructureMap.BuildSession.CreateInstance(Type pluginType)
at StructureMap.Pipeline.DefaultInstance.build(Type pluginType,
BuildSession session)
at StructureMap.Pipeline.Instance.createRawObject(Type pluginType,
BuildSession session)
at StructureMap.Pipeline.Instance.Build(Type pluginType,
BuildSession session)
at StructureMap.Pipeline.ConfiguredInstanceBase`1.getChild(String
propertyName, Type pluginType, BuildSession buildSession)
at
StructureMap.Pipeline.ConfiguredInstanceBase`1.StructureMap.Pipeline.IConfiguredInstance.GetChild
(String propertyName, Type pluginType, BuildSession buildSession)
at
NHSCatalogueWebControllersContractControllerInstanceBuilder371e39c9925b44aaa9231e1743b055bd.BuildInstance
(IConfiguredInstance , BuildSession )
at
StructureMap.Pipeline.ConfiguredInstanceBase`1.StructureMap.Pipeline.IConfiguredInstance.Build
(Type pluginType, BuildSession session, InstanceBuilder builder)
at StructureMap.Pipeline.ConfiguredInstanceBase`1.build(Type
pluginType, BuildSession session)
at StructureMap.Pipeline.Instance.createRawObject(Type pluginType,
BuildSession session)
at StructureMap.Pipeline.Instance.Build(Type pluginType,
BuildSession session)
at StructureMap.Pipeline.BuildPolicy.Build(BuildSession
buildSession, Type pluginType, Instance instance)
at StructureMap.InstanceFactory.Build(BuildSession session,
Instance instance)
at StructureMap.BuildSession.CreateInstance(Type pluginType,
Instance instance)
at StructureMap.BuildSession.<.ctor>b__0(Type t)
at StructureMap.Util.Cache`2.Retrieve(KEY key)
at StructureMap.BuildSession.CreateInstance(Type pluginType)
at StructureMap.Container.GetInstance(Type pluginType)
at StructureMap.ObjectFactory.GetInstance(Type pluginType)
at
Contoso.Catalogue.Web.Controllers.StructureMapControllerFactory.GetControllerInstance
(Type controllerType) in D:\Data\Dev\SS\CatalogueWeb\CatalogueWeb
\Helpers\StructureMapControllerFactory.vb:line 29

Andreas Öhlund

unread,
Mar 30, 2009, 9:58:55 AM3/30/09
to structure...@googlegroups.com
Check if you are contructing any of your types using ContructedBy(()=>some_code_to_contruct_object).
 
If this is the case and for some reason some_code_to_contruct_object returns null you're going to end up with a duplicate key exception.
 
This happens because SM inserts null into the cache and the next time the instance is requested null is returned causing SM to create a new object and when trying to cache it the next time InstanceCache.Set will throw an exception. A fix for this would be to have InstanceCache.Set check for the existence of the key with a null value and overwrite if nessesary. (Or throw a nice error message if this is considered an error)
 
A real life situation when this happens is when you try to register HttpContext.Current.User in a webb app. During application start HttpContext.Current.User is null.
 
ForRequestedType<IPrincipal>().TheDefault.Is.ConstructedBy(() => HttpContext.Current.User);
 
Here is a test to repro. the problem:
 
 [Test]
        public void Returning_null_values_in_contructed_by_generates_a_duplicate_cache_entry()
        {
            ObjectFactory.Initialize(x =>
                                         {
                                             x.ForRequestedType<IPrincipal>().TheDefault.Is.ConstructedBy(() => null);
                                             x.ForRequestedType<IUserContextBuilder>().TheDefault.Is.OfConcreteType<TestClass>();
                                         });
 
            //this throws a duplicate cache entry exception
            ObjectFactory.AssertConfigurationIsValid();
        }
 
        public class TestClass : IUserContextBuilder
        {
            private IPrincipal principal;
            public TestClass(IPrincipal principal)
            {
                this.principal = principal;
            }
            public IUserContext CreateContext()
            {
                return null;
            }
        }

 
Jeremy: What's you take on this?
 
Hope this helps.
 
/Andreas

http://andreasohlund.blogspot.com
http://twitter.com/andreasohlund



 
> Date: Mon, 30 Mar 2009 04:55:50 -0700
> Subject: [sm-users] Re: StructureMap Exception Code: 400
> From: james....@gmail.com
> To: structure...@googlegroups.com
> More than mail–Windows Live™ goes way beyond your inbox. More than messages

JamesSpibey

unread,
Mar 30, 2009, 3:24:19 PM3/30/09
to structuremap-users
Hi Andreas,

Thanks for the suggestion however I don't think it applies in this
case because all my ConstructedBy() calls return New foo() so unless
I'm out of memory that should never return null.

Jeremy: I know you're busy but if you get chance to take a look at my
issue above that would be really appreciated

Thanks

James

On Mar 30, 2:58 pm, Andreas Öhlund <andreas_ohl...@hotmail.com> wrote:
> Check if you are contructing any of your types using ContructedBy(()=>some_code_to_contruct_object).
>
> If this is the case and for some reason some_code_to_contruct_object returns null you're going to end up with a duplicate key exception.
>
> This happens because SM inserts null into the cache and the next time the instance is requested null is returned causing SM to create a new object and when trying to cache it the next time InstanceCache.Set will throw an exception. A fix for this would be to have InstanceCache.Set check for the existence of the key with a null value and overwrite if nessesary. (Or throw a nice error message if this is considered an error)
>
> A real life situation when this happens is when you try to register HttpContext.Current.User in a webb app. During application start HttpContext.Current.User is null.
>
> ForRequestedType<IPrincipal>().TheDefault.Is.ConstructedBy(() => HttpContext.Current.User);
>
> Here is a test to repro. the problem:
>
>  [Test]
>         public void Returning_null_values_in_contructed_by_generates_a_duplicate_cache_entry()
>         {
>             ObjectFactory.Initialize(x =>
>                                          {
>                                              x.ForRequestedType<IPrincipal>().TheDefault.Is.ConstructedBy(() => null);
>
>                                              x.ForRequestedType<IUserContextBuilder>().TheDefault.Is.OfConcreteType<TestClass>();
>
>                                          });
>
>             //this throws a duplicate cache entry exception
>             ObjectFactory.AssertConfigurationIsValid();
>         }
>
>         public class TestClass : IUserContextBuilder
>         {
>             private IPrincipal principal;
>
>             public TestClass(IPrincipal principal)
>             {
>                 this.principal = principal;
>             }
>
>             public IUserContext CreateContext()
>             {
>                 return null;
>             }
>         }
>
> Jeremy: What's you take on this?
>
> Hope this helps.
>
> /Andreas
>
> http://andreasohlund.blogspot.comhttp://twitter.com/andreasohlund
>
> > Date: Mon, 30 Mar 2009 04:55:50 -0700
> > Subject: [sm-users] Re: StructureMap Exception Code: 400
> > From: james.spi...@gmail.com
> ...
>
> read more »

Andreas Öhlund

unread,
Mar 30, 2009, 4:14:39 PM3/30/09
to structure...@googlegroups.com
Ok!
 
Need some more info:
 
Your ICacheService seem to be created using a function, could you post the configuration here?
 
Also if you have the .pdb for structuremap set a break in InstanceCache.Set and figure out which key that throws the "duplicate key" exception.
 
/Andreas  

http://andreasohlund.blogspot.com
http://twitter.com/andreasohlund



 
> Date: Mon, 30 Mar 2009 12:24:19 -0700

> Subject: [sm-users] Re: StructureMap Exception Code: 400

JamesSpibey

unread,
Mar 30, 2009, 4:33:23 PM3/30/09
to structuremap-users
Thanks for your reply again, much appreciated. The function
(CacheInit) is shown below. The only problem with debugging the issue
is that it only happen sporadically. I deployed a new version of our
web app today and I got the exception on all 4 servers in our farm
when the application first started but can't reproduce it on my dev
box

Public Shared Function CacheInit() As ICacheService
Dim cfg = ObjectFactory.GetInstance(Of IConfigService)()

Return New AspNetCache(cfg.EnableCache)
End Function

On Mar 30, 9:14 pm, Andreas Öhlund <andreas_ohl...@hotmail.com> wrote:
> Ok!
>
> Need some more info:
>
> Your ICacheService seem to be created using a function, could you post the configuration here?
>
> Also if you have the .pdb for structuremap set a break in InstanceCache.Set and figure out which key that throws the "duplicate key" exception.
>
> /Andreas  
>
> http://andreasohlund.blogspot.comhttp://twitter.com/andreasohlund
>
> > Date: Mon, 30 Mar 2009 12:24:19 -0700
> > Subject: [sm-users] Re: StructureMap Exception Code: 400
> > From: james.spi...@gmail.com
> ...
>
> read more »

Andreas Öhlund

unread,
Mar 30, 2009, 4:44:42 PM3/30/09
to structure...@googlegroups.com
Ok, more questions:
 
Does the problem occur everytime you recycle the application pool? (ie every time SM is configured)
 
How is IConfigService configured in SM?
 
Can cfg.EnableCache throw an exception?
 
Is there any code that can fail in the constructor for AspNetCache?

/Andreas
http://andreasohlund.blogspot.com
http://twitter.com/andreasohlund



 
> Date: Mon, 30 Mar 2009 13:33:23 -0700

> Subject: [sm-users] Re: StructureMap Exception Code: 400

JamesSpibey

unread,
Mar 31, 2009, 4:18:21 AM3/31/09
to structuremap-users
- Yes, it does seem to occur every time the application pool is
recycled.
- Below is the ObjectFactory.Initialize method.
- Technically, yes it could because it attempts to read a value from
web.config. However, that value does exist and so I can't see why it
would throw an exception. Would it be a good idea to put a try/catch
around it to see if it is throwing an exception?
- No, it just assigns the EnableCache value which is passed in to an
internal field

Thanks again

James

--

Public Shared Sub ObjectFactoryInitialization(ByVal scanner As
IInitializationExpression)
scanner.AddRegistry(New CatalogueRegistry())
scanner.AddRegistry(New DBServiceRegistry())

' Web Specific Stuff
scanner.ForRequestedType(Of IContextService)
().TheDefaultIsConcreteType(Of WebContextService)().AsSingletons() '
Allows us to cache
scanner.ForRequestedType(Of ICacheService)
().TheDefault.Is.ConstructedBy(AddressOf CacheInit)
scanner.ForRequestedType(Of IConfigService)
().TheDefaultIsConcreteType(Of ConfigService)()

scanner.ForRequestedType(Of IExceptionManager)
().TheDefaultIsConcreteType(Of ExceptionManager)()
scanner.ForRequestedType(Of IMVCExceptionManager)
().TheDefaultIsConcreteType(Of ExceptionManager)()
End Sub


On Mar 30, 9:44 pm, Andreas Öhlund <andreas_ohl...@hotmail.com> wrote:
> Ok, more questions:
>
> Does the problem occur everytime you recycle the application pool? (ie every time SM is configured)
>
> How is IConfigService configured in SM?
>
> Can cfg.EnableCache throw an exception?
>
> Is there any code that can fail in the constructor for AspNetCache?
>
> /Andreashttp://andreasohlund.blogspot.comhttp://twitter.com/andreasohlund
>
> > Date: Mon, 30 Mar 2009 13:33:23 -0700
> > Subject: [sm-users] Re: StructureMap Exception Code: 400
> > From: james.spi...@gmail.com
> ...
>
> read more »

Jos Krause

unread,
Mar 31, 2009, 4:18:23 AM3/31/09
to structure...@googlegroups.com
Thanks everyone for your contribution on shedding some much needed light on the problem at hand.
 
A lot of the comments posted have given me some a-ha moments that should allow me to do some more specific digging into the code. Again I tried using WhatDoIHave() but this line is never called since the exception is thrown before this line is ever ran. Or at least that would be my interpretation from it.
 
The error-happens-on-application-pool-recycle remark made me think about some new approaches to take to get to the bottom of this since the error does very much occur in the eventlog I just cannot reproduce it consistently.
 
Will let everyone know what I find out if and when I do, thanks again for all the expertise!
 
~ Josh

Andreas Öhlund

unread,
Mar 31, 2009, 5:16:01 AM3/31/09
to structure...@googlegroups.com
The only time I have experienced the "duplicate key" exception is when creation of an instance fails. "AssertConfigurationIsValid" supresses any execption during creation which causes null to be inserted in the instance cache. So try to put catch logger.Error(ex) on all the spots where things can go wrong and see what turns up! (failing to read web.config could be the problem)
> Date: Tue, 31 Mar 2009 01:18:21 -0700

> Subject: [sm-users] Re: StructureMap Exception Code: 400

JamesSpibey

unread,
Mar 31, 2009, 2:31:31 PM3/31/09
to structuremap-users
Thanks Andreas. I've done as you suggested and put a new version live
and bizarely I haven't seen the error this time. However, I'm sure
this isn't the end of the story so I'll keep an eye on it for the next
few days

Regards

James

On Mar 31, 10:16 am, Andreas Öhlund <andreas_ohl...@hotmail.com>
wrote:
> The only time I have experienced the "duplicate key" exception is when creation of an instance fails. "AssertConfigurationIsValid" supresses any execption during creation which causes null to be inserted in the instance cache. So try to put catch logger.Error(ex) on all the spots where things can go wrong and see what turns up! (failing to read web.config could be the problem)
>
> /Andreas
>
> http://andreasohlund.blogspot.comhttp://twitter.com/andreasohlund
>
> > Date: Tue, 31 Mar 2009 01:18:21 -0700
> > Subject: [sm-users] Re: StructureMap Exception Code: 400
> > From: james.spi...@gmail.com
> ...
>
> read more »

JamesSpibey

unread,
Apr 2, 2009, 3:51:54 PM4/2/09
to structuremap-users
As expected this error has returned again today. Nothing was logged in
my code which checks for Nulls so it must be something else. Any
suggestions?
> ...
>
> read more »

Scott Vickers

unread,
Apr 14, 2009, 12:01:40 PM4/14/09
to structuremap-users
We are seeing this same error as well on our mvc sites. Always
happens following an app pool recycle but then continues on normally
after a handful of exceptions. Anybody have any ideas?

On Apr 2, 12:51 pm, JamesSpibey <james.spi...@gmail.com> wrote:
> As expected this errorhasreturned again today. Nothing was logged in
> my code which checks for Nulls so it must be something else. Any
> suggestions?
>
> On Mar 31, 7:31 pm, JamesSpibey <james.spi...@gmail.com> wrote:
>
> > Thanks Andreas. I've done as you suggested and put a new version live
> > and bizarely I haven't seen the error this time. However, I'm sure
> > this isn't the end of the story so I'll keep an eye on it for the next
> > few days
>
> > Regards
>
> > James
>
> > On Mar 31, 10:16 am, Andreas Öhlund <andreas_ohl...@hotmail.com>
> > wrote:
>
> > > The only time I have experienced the "duplicatekey" exception is when creation of an instance fails. "AssertConfigurationIsValid" supresses any execption during creation which causes null to be inserted in the instance cache. So try to put catch logger.Error(ex) on all the spots where things can go wrong and see what turns up! (failing to read web.config could be the problem)
> > > > > > > Also if you have the .pdb for structuremap set a break in InstanceCache.Set and figure out whichkeythat throws the "duplicatekey" exception.
>
> > > > > > > /Andreas  
>
> > > > > > >http://andreasohlund.blogspot.comhttp://twitter.com/andreasohlund
>
> > > > > > > > Date: Mon, 30 Mar 2009 12:24:19 -0700
> > > > > > > > Subject: [sm-users] Re: StructureMap Exception Code: 400
> > > > > > > > From: james.spi...@gmail.com
> > > > > > > > To: structure...@googlegroups.com
>
> > > > > > > > Hi Andreas,
>
> > > > > > > > Thanks for the suggestion however I don't think it applies in this
> > > > > > > > case because all my ConstructedBy() calls return New foo() so unless
> > > > > > > > I'm out of memory that should never return null.
>
> > > > > > > > Jeremy: I know you're busy but if you get chance to take a look at my
> > > > > > > > issue above that would be really appreciated
>
> > > > > > > > Thanks
>
> > > > > > > > James
>
> > > > > > > > On Mar 30, 2:58 pm, Andreas Öhlund <andreas_ohl...@hotmail.com> wrote:
> > > > > > > > > Check if you are contructing any of your types using ContructedBy(()=>some_code_to_contruct_object).
>
> > > > > > > > > If this is the case and for some reason some_code_to_contruct_object returns null you're going to end up with a duplicatekeyexception.
>
> > > > > > > > > This happens because SM inserts null into the cache and the next time the instance is requested null is returned causing SM to create a new object and when trying to cache it the next time InstanceCache.Set will throw an exception. A fix for this would be to have InstanceCache.Set check for the existence of thekeywith a null value and overwrite if nessesary. (Or throw a nice error message if this is considered an error)
>
> > > > > > > > > A real life situation when this happens is when you try to register HttpContext.Current.User in a webb app. During application start HttpContext.Current.User is null.
>
> > > > > > > > > ForRequestedType<IPrincipal>().TheDefault.Is.ConstructedBy(() => HttpContext.Current.User);
>
> > > > > > > > > Here is a test to repro. the problem:
>
> > > > > > > > >  [Test]
> > > > > > > > >         public void Returning_null_values_in_contructed_by_generates_a_duplicate_cache_entry()
> > > > > > > > >         {
> > > > > > > > >             ObjectFactory.Initialize(x =>
> > > > > > > > >                                          {
> > > > > > > > >                                              x.ForRequestedType<IPrincipal>().TheDefault.Is.ConstructedBy(() => null);
>
> > > > > > > > >                                              x.ForRequestedType<IUserContextBuilder>().TheDefault.Is.OfConcreteType<Test Class>();
>
> > > > > > > > >                                          });
>
> > > > > > > > >             //this throws a duplicate cache entry exception
> > > > > > > > >             ObjectFactory.AssertConfigurationIsValid();
> > > > > > > > >         }
>
> > > > > > > > >         public class TestClass : IUserContextBuilder
> > > > > > > > >         {
> > > > > > > > >             private IPrincipal principal;
>
> > > > > > > > >             public TestClass(IPrincipal principal)
> > > > > > > > >             {
> > > > > > > > >                 this.principal = principal;
> > > > > > > > >             }
>
> > > > > > > > >             public IUserContext CreateContext()
> > > > > > > > >             {
> > > > > > > > >                 return null;
> > > > > > > > >             }
> > > > > > > > >         }
>
> > > > > > > > > Jeremy: What's you take on this?
>
> > > > > > > > > Hope this helps.
>
> > > > > > > > > /Andreas
>
> > > > > > > > >http://andreasohlund.blogspot.comhttp://twitter.com/andreasohlund
>
> > > > > > > > > > Date: Mon, 30 Mar 2009 04:55:50 -0700
> > > > > > > > > > Subject: [sm-users] Re: StructureMap Exception Code: 400
> > > > > > > > > > From: james.spi...@gmail.com
> > > > > > > > > > To: structure...@googlegroups.com
>
> > > > > > > > > > I'm having thesameissue - would you mind taking a look at the output
> > > > > > > > > > from WhatDoIHave() for our system?
>
> > > > > > > > > > Thanks
>
> > > > > > > > > > James
>
> > > > > > > > > > -------------------
>
> > > > > > > > > > Message:
> > > > > > > > > > =========================================================================== ================================
> > > > > > > > > > Configuration Sources:
>
> > > > > > > > > > 0) Registry: StructureMap.InitializationExpression,StructureMap
> > > > > > > > > > 1) Registry:
> > > > > > > > > > Contoso.Catalogue.Services.CatalogueRegistry,CatalogueBusinessLogic
> > > > > > > > > > 2) Registry:
> > > > > > > > > > Contoso.Catalogue.Data.DBServiceRegistry,CatalogueRepositorySqlServer
>
> > > > > > > > > > =========================================================================== =========================================================================== =========================================================================== =========================================================================== =========================================================================== ==================================================
> > > > > > > > > > PluginType
> > > > > > > > > > Name
> > > > > > > > > > Description
>
> ...
>
> read more »

Andreas Öhlund

unread,
Apr 14, 2009, 12:11:37 PM4/14/09
to structuremap-users
Can you trap it in the debugger and find out which key SM chokes on?
( you'll need the structuremap.pdb for that)

Is it failing when you call AssertConfigurationIsValid?

Is it failing every time the app pool starts or only on recycle?

Can you post your SM configuration code?

/Andreas
> ...
>
> read more »- Hide quoted text -

Scott Vickers

unread,
Apr 14, 2009, 1:03:44 PM4/14/09
to structuremap-users
It's difficult because it happens very rarely on our dev boxes and
when it does we are generally not debugging, refreshing the page
causes it to go away. We are configuring SM through a registry in
code so I don't think the failing to read web.config issue mentioned
above would affect us.
> ...
>
> read more »

Andreas Öhlund

unread,
Apr 14, 2009, 1:15:15 PM4/14/09
to structure...@googlegroups.com
It would be interesting to find out which type that's causing the duplicate key exception. If you have the time you could get the SM trunk and put som logging in InstanceCache.Set to check for duplicate keys
> Date: Tue, 14 Apr 2009 09:11:37 -0700

> Subject: [sm-users] Re: StructureMap Exception Code: 400

Jeremy Miller

unread,
Apr 19, 2009, 3:13:47 PM4/19/09
to structuremap-users
Josh,

I think *you* are hitting a threading bug. From the stack trace,
you're on a slightly older version of StructureMap. If you download
2.5.3 there's code that does the double locking trick inside the Cache
class for just this case. Can you try the newer version please?

Thanks,

Jeremy

Jeremy Miller

unread,
Apr 19, 2009, 3:15:02 PM4/19/09
to structuremap-users
Andreas et al,

I'm just about to check in the fix for this bug. Look for the 2.5.4
release in the very near future. Thanks for the repro steps. I used
your unit test to fix the problem.

On Mar 30, 8:58 am, Andreas Öhlund <andreas_ohl...@hotmail.com> wrote:
> Check if you are contructing any of your types using ContructedBy(()=>some_code_to_contruct_object).
>
> If this is the case and for some reason some_code_to_contruct_object returns null you're going to end up with a duplicate key exception.
>
> This happens because SM inserts null into the cache and the next time the instance is requested null is returned causing SM to create a new object and when trying to cache it the next time InstanceCache.Set will throw an exception. A fix for this would be to have InstanceCache.Set check for the existence of the key with a null value and overwrite if nessesary. (Or throw a nice error message if this is considered an error)
>
> A real life situation when this happens is when you try to register HttpContext.Current.User in a webb app. During application start HttpContext.Current.User is null.
>
> ForRequestedType<IPrincipal>().TheDefault.Is.ConstructedBy(() => HttpContext.Current.User);
>
> Here is a test to repro. the problem:
>
>  [Test]
>         public void Returning_null_values_in_contructed_by_generates_a_duplicate_cache_entry()
>         {
>             ObjectFactory.Initialize(x =>
>                                          {
>                                              x.ForRequestedType<IPrincipal>().TheDefault.Is.ConstructedBy(() => null);
>
>                                              x.ForRequestedType<IUserContextBuilder>().TheDefault.Is.OfConcreteType<TestClass>();
>
>                                          });
>
>             //this throws a duplicate cache entry exception
>             ObjectFactory.AssertConfigurationIsValid();
>         }
>
>         public class TestClass : IUserContextBuilder
>         {
>             private IPrincipal principal;
>
>             public TestClass(IPrincipal principal)
>             {
>                 this.principal = principal;
>             }
>
>             public IUserContext CreateContext()
>             {
>                 return null;
>             }
>         }
>
> Jeremy: What's you take on this?
>
> Hope this helps.
>
> /Andreas
>
> http://andreasohlund.blogspot.comhttp://twitter.com/andreasohlund
>
> > Date: Mon, 30 Mar 2009 04:55:50 -0700
> > Subject: [sm-users] Re: StructureMap Exception Code: 400
> > From: james.spi...@gmail.com
> ...
>
> read more »

Jeremy Miller

unread,
Apr 19, 2009, 3:54:15 PM4/19/09
to structuremap-users
I've added some new defensive programming checks for just this case in
the trunk.

On Apr 14, 12:15 pm, Andreas Öhlund <andreas_ohl...@hotmail.com>
wrote:
> It would be interesting to find out which type that's causing the duplicate key exception. If you have the time you could get the SM trunk and put som logging in InstanceCache.Set to check for duplicate keys
>
> /Andreas
>
> http://andreasohlund.blogspot.comhttp://twitter.com/andreasohlund
>
> > Date: Tue, 14 Apr 2009 09:11:37 -0700
> > Subject: [sm-users] Re: StructureMap Exception Code: 400
> > From: andreas_ohl...@hotmail.com
> ...
>
> read more »
Reply all
Reply to author
Forward
0 new messages