The value cannot be null. Parameter name: key

The value cannot be null. Parameter name: key

I started getting this error since I implemented StructureMapControllerFactory as my DefaultControllerFactory.

Actually, I copied the code from the MVC Sample App StoreFront, but I could not understand why this error continues to appear. Even this error indicates that the application is still running.

What could be causing this error?

thank you for your time


System.ArgumentNullException was unhandled by user code Message="Value cannot be null.\r\nParameter name: key" Source="mscorlib" ParamName="key" StackTrace: at System.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument) at System.Collections.Generic.Dictionary`2.FindEntry(TKey key) at System.Collections.Generic.Dictionary`2.ContainsKey(TKey key) at StructureMap.Util.Cache`2.get_Item(KEY key) at StructureMap.BuildSession.CreateInstance(Type pluginType) at StructureMap.Container.GetInstance(Type pluginType) at StructureMap.ObjectFactory.GetInstance(Type pluginType) at Yacht.Web.Controllers.StructureMapControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) in D:\Documents\WebSites\JOBS\Yacht\Yacht.Web\Controllers\StructureMapControllerFactory.cs:line 16 at System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) InnerException: 

and this is the class StructureMapControllerFactory

 public class StructureMapControllerFactory : DefaultControllerFactory { protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType) { return ObjectFactory.GetInstance(controllerType) as Controller; } } 
+4
source share
4 answers

Note the nurse’s response to a StructureMap error with an invalid controller .

+1
source

Also keep in mind that if a model has certain properties that are not mapped to a database field, this error may occur. It was kind of a hassle to keep track of this, so I will leave an answer to SO if others run into the same problem.

My fix was simple, including DataAnnotations:

 using System.ComponentModel.DataAnnotations; 

And then use the NotMapped annotation for specific properties:

 [NotMapped] public string SomeCustomProperty { get; set; } 
+4
source

For those who hunt for why they get

The value cannot be null. Parameter name: key

unexplained exception in the bowels of the EF. Verify that one of the POCOs is NOT using an unsupported type. I check POCOs for this list. Not every type can be mapped to the Entity framework. Starting with EF 5.0

 // MY TESTING WOULD Indicate SEE // NOT SUPPORTED comment public const string Boolean = "System.Boolean"; public const string Byte = "System.Byte"; public const string ByteArray = "System.Byte[]"; public const string SByte = "System.SByte"; // NOT SUPPORTED public const string Char = "System.Char"; // NOT SUPPORTED public const string Decimal = "System.Decimal"; public const string Double = "System.Double"; public const string Single = "System.Single"; public const string Int32 = "System.Int32"; public const string UInt32 = "System.UInt32";// NOT SUPPORTED public const string Int64 = "System.Int64"; public const string UInt64 = "System.UInt64";// NOT SUPPORTED public const string Int16 = "System.Int16"; public const string UInt16 = "System.UInt16"; public const string String = "System.String"; public const string DateTimeOffset = "System.DateTimeOffset"; public const string DateTime = "System.DateTime"; public const string Guid = "System.Guid"; public const string Enum = "System.Enum"; public const string Type = "System.Type";// NOT SUPPORTED 

This list is not exhaustive. But it covers some important commonly used types of scalar elements that can cause problems.

+1
source

Revgum's answer was exactly what I needed to use VS2012 MVC and EF5. One small change in EF5, the definition of decoding [NotMapped] is in this library:

 using System.ComponentModel.DataAnnotations.Schema; 

(I believe that his library works for EF4 and lower, but I do not quote me)

Also, keep in mind that if your model has a foreign key relationship, all child models can cause the same error if their unmarked fields are not properly formatted.

If you use EF to reverse engineer your code and don’t touch models, this should never be a problem.

0
source

All Articles