Value cannot be null.Parameter name: key [duplicate]
Value cannot be null.Parameter name: key
I have started to get this error since i ve implemented the StructureMapControllerFactory as my DefaultControllerFactory.
Actually i copied the code from MVC Sample App StoreFront, but i couldn't figure out why this error keeps popping up. Even this error show up the application still runs.
What might be the reason of this error ?
Thanks 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(Reques开发者_运维百科tContext 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 StructureMapControllerFactory class
public class StructureMapControllerFactory : DefaultControllerFactory
{
protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
{
return ObjectFactory.GetInstance(controllerType) as Controller;
}
}
Also be aware that if a model has properties defined which do not map to a database field then this error can occur. It was kind of a hassle to track this down, so I'll leave an answer on SO in case others run into the same problem.
My fix was simple, include DataAnnotations:
using System.ComponentModel.DataAnnotations;
And then use the NotMapped annotation on the specific properties:
[NotMapped]
public string SomeCustomProperty { get; set; }
See anurse's answer to StructureMap error when invalid controller.
For those Hunting for Why they get
Value cannot be null.Parameter name: key
unexplained exception in the bowels of EF. Check one of POCOs is NOT using an Unsupported type. I check for POCOs against this list Not every type can be mapped in Entity framework. As of 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 does cover some important scalar types often used that May cause issues.
Revgum's answer was exactly what I needed using VS2012 MVC and EF5. One minor change in EF5, the [NotMapped] decoration definition is in this library:
using System.ComponentModel.DataAnnotations.Schema;
(I believe his library works for EF4 and below but don't quote me on that)
Also, keep in mind that if your model has a foreign key relationship, all child models can cause the same error to be thrown if their unmapped fields are not properly decorated.
If you are using EF to reverse engineer code first and not touching the models, then this should never be an issue.
精彩评论