MVC+MEF = Object reference not set to an instance of an object.

Oct 8, 2009 at 3:55 PM

I am using MEF with ASP.NET MVC (i got sampe from hammett blog). 

I tested stress web server performence and i got this error:

 

Index was outside the bounds of the array. 

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IndexOutOfRangeException: Index was outside the bounds of the array.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

/>
  System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) +10416309
System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value) +12
Microsoft.Internal.Collections.ConditionalWeakTable`2.Add(TKey key, TValue value) in ConditionalWeakTable.cs:34
System.ComponentModel.Composition.Hosting.ImportEngine.GetPartManager(ComposablePart part, Boolean createIfNotpresent) in ImportEngine.cs:659
System.ComponentModel.Composition.Hosting.ImportEngine.SatisfyImports(ComposablePart part) in ImportEngine.cs:151
System.ComponentModel.Composition.Hosting.CompositionServices.GetExportedValueFromComposedPart(ImportEngine engine, ComposablePart part, ExportDefinition definition) in CompositionServices.cs:401
System.ComponentModel.Composition.Hosting.CatalogExportProvider.GetExportedValue(ComposablePart part, ExportDefinition export, Boolean isSharedPart) in CatalogExportProvider.cs:393
System.ComponentModel.Composition.Hosting.CatalogExport.GetExportedValueCore() in CatalogExportProvider.CatalogExport.cs:46
System.ComponentModel.Composition.Primitives.Export.get_Value() in Export.cs:199
System.ComponentModel.Composition.ExportServices.GetExportedValueFromLazy(Export export) in ExportServices.cs:130
System.ComponentModel.Composition.<>c__DisplayClass4`2.&lt;CreateStronglyTypedExportOfTM&gt;b__0() in ExportServices.cs:78
System.Lazy`1.get_Value() in Lazy.cs:61
MvcMefExtensions.MefControllerFactory.CreateController(RequestContext requestContext, String controllerName) in MefControllerFactory.cs:43
System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext) +361
System.Web.Mvc.MvcHandler.ProcessRequest(HttpContext httpContext) +146
System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext httpContext) +58
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +599
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +171

--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.4927; ASP.NET Version:2.0.50727.4927

Oct 8, 2009 at 4:54 PM
Edited Oct 8, 2009 at 11:00 PM

Ok i found and solved it :) 

i should use another contructor -

 new CompositionContainer(catalog, true)

 

 

Oct 8, 2009 at 10:59 PM

I got new error 2 times :(

Version:0.9 StartHTML:-1 EndHTML:-1 StartFragment:0000000111 EndFragment:0000005225

<!--StartFragment-->

Server Error in '/Perf' Application.

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.


Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
   System.Lazy`1.get_Value() in Lazy.cs:61
   System.ComponentModel.Composition.ReflectionModel.<GetExports>d__2.MoveNext() in ReflectionModelServices.cs:277
   System.Linq.Buffer`1..ctor(IEnumerable`1 source) +547
   System.Linq.Enumerable.ToArray(IEnumerable`1 source) +107
   System.ComponentModel.Composition.ReflectionModel.ReflectionComposablePartDefinition.get_ExportDefinitions() in ReflectionComposablePartDefinition.cs:63
   System.ComponentModel.Composition.Primitives.ComposablePartCatalog.GetExports(ImportDefinition definition) in ComposablePartCatalog.cs:88
   System.ComponentModel.Composition.Hosting.AggregateCatalog.GetExports(ImportDefinition definition) in AggregateCatalog.cs:152
   System.ComponentModel.Composition.Hosting.CatalogExportProvider.GetExportsCore(ImportDefinition definition, AtomicComposition atomicComposition) in CatalogExportProvider.cs:247
   System.ComponentModel.Composition.Hosting.ExportProvider.TryGetExportsCore(ImportDefinition definition, AtomicComposition atomicComposition, IEnumerable`1& exports) in ExportProvider.cs:208
   System.ComponentModel.Composition.Hosting.ExportProvider.GetExports(ImportDefinition definition, AtomicComposition atomicComposition) in ExportProvider.cs:102
   System.ComponentModel.Composition.Hosting.AggregateExportProvider.GetExportsCore(ImportDefinition definition, AtomicComposition atomicComposition) in AggregateExportProvider.cs:157
   System.ComponentModel.Composition.Hosting.ExportProvider.TryGetExportsCore(ImportDefinition definition, AtomicComposition atomicComposition, IEnumerable`1& exports) in ExportProvider.cs:208
   System.ComponentModel.Composition.Hosting.ExportProvider.TryGetExports(ImportDefinition definition, AtomicComposition atomicComposition, IEnumerable`1& exports) in ExportProvider.cs:145
   System.ComponentModel.Composition.Hosting.CompositionContainer.GetExportsCore(ImportDefinition definition, AtomicComposition atomicComposition) in CompositionContainer.cs:396
   System.ComponentModel.Composition.Hosting.ExportProvider.TryGetExportsCore(ImportDefinition definition, AtomicComposition atomicComposition, IEnumerable`1& exports) in ExportProvider.cs:208
   System.ComponentModel.Composition.Hosting.ExportProvider.GetExports(ImportDefinition definition, AtomicComposition atomicComposition) in ExportProvider.cs:102
   System.ComponentModel.Composition.Hosting.ExportProvider.GetExportsCore(Type type, Type metadataViewType, String contractName, ImportCardinality cardinality) in ExportProvider.GetExportOverrides.cs:799
   System.ComponentModel.Composition.Hosting.ExportProvider.GetExportsCore(String contractName) in ExportProvider.GetExportOverrides.cs:754
   System.ComponentModel.Composition.Hosting.ExportProvider.GetExports(String contractName) in ExportProvider.GetExportOverrides.cs:438
   System.ComponentModel.Composition.Hosting.ExportProvider.GetExports() in ExportProvider.GetExportOverrides.cs:395
   MvcMefExtensions.MefControllerFactory.CreateController(RequestContext requestContext, String controllerName) in MefControllerFactory.cs:30
   System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext) +360
   System.Web.Mvc.MvcHandler.ProcessRequest(HttpContext httpContext) +145
   System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext httpContext) +57
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +598
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +170

 


Version Information: Microsoft .NET Framework Version:2.0.50727.4927; ASP.NET Version:2.0.50727.4927

Developer
Oct 9, 2009 at 10:47 PM

Just from a quick look at the callstack it looks like list of Exports being passed to ReflectionModelServices.CreatePartDefinition is null (or perhaps something in the list is null not exactly sure). I took a quick look at Hammet's MVC sample and found this code in his MVCCatalog.

ReflectionModelServices.CreatePartDefinition(
                                        new Lazy<Type>(() => fixedType), true,
                                        new Lazy<IEnumerable<ImportDefinition>>(() => fixedPart.ImportDefinitions),
                                        new Lazy<IEnumerable<ExportDefinition>>(() => GetControllerExports(fixedPart.ExportDefinitions, fixedType)),
                                        new Lazy<IDictionary<string, object>>(() => newPartMetadata), this);

It didn't look to me that null would ever come out of GetControllerExports, have you changed this code?