{"record":{"id":"d820843f97b84057","repo":"OrchardCMS/OrchardCore","slug":"shape-creation-failed-for-type-shapetype-the-shape-factory","errorCode":null,"errorMessage":"Shape creation failed for type '{shapeType}'. The shape factory returned null.","messagePattern":"Shape creation failed for type '(.+?)'\\. The shape factory returned null\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/OrchardCore/OrchardCore.DisplayManagement/Implementation/DefaultShapeFactory.cs","lineNumber":97,"sourceCode":"        creating?.Invoke(creatingContext, state);\n\n        // 'Creating' events may add behaviors and alter base type.\n        foreach (var ev in _events)\n        {\n            ev.Creating(creatingContext);\n        }\n\n        if (shapeDescriptor != null)\n        {\n            foreach (var ev in shapeDescriptor.CreatingAsync)\n            {\n                await ev(creatingContext);\n            }\n        }\n\n        // Create the new instance.\n        var shape = await creatingContext.CreateInternalAsync()\n            ?? throw new InvalidOperationException($\"Shape creation failed for type '{shapeType}'. The shape factory returned null.\");\n\n        var createdContext = new ShapeCreatedContext\n        {\n            ServiceProvider = _serviceProvider,\n            New = creatingContext.New,\n            ShapeFactory = creatingContext.ShapeFactory,\n            ShapeType = creatingContext.ShapeType,\n            Shape = shape,\n        };\n\n        var shapeMetadata = shape.Metadata;\n        shapeMetadata.Type = shapeType;\n\n        // Merge wrappers if there are any.\n        if (shapeDescriptor != null && shapeMetadata.Wrappers.Count + shapeDescriptor.Wrappers.Count > 0)\n        {\n            shapeMetadata.Wrappers.AddRange(shapeDescriptor.Wrappers);\n        }","sourceCodeStart":79,"sourceCodeEnd":115,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore/OrchardCore.DisplayManagement/Implementation/DefaultShapeFactory.cs#L79-L115","documentation":"DefaultShapeFactory.CreateAsync fires Creating events, then invokes the registered internal creation logic (CreateInternalAsync). If that factory returns null — meaning no shape creation handler produced an instance for the type — it throws InvalidOperationException(\"Shape creation failed for type '{shapeType}'. The shape factory returned null.\").","triggerScenarios":"Calling IShapeFactory.CreateAsync for a shape type whose internal creation pipeline (Creating handlers / default shape creation) returns null — e.g., a custom IShapeFactoryEvents or overriding CreateInternalAsync returning null, or a shape type with no valid creation path registered.","commonSituations":"Custom shape factory events that replace/intercept creation but forget to set a result; dynamic shape types created from user/config input where a handler bails out; broken DI registrations for shape creation services.","solutions":["Check IShapeFactoryEvents.Creating implementations for the shape type — ensure they do not suppress or return without creating a shape.","Verify the shape type is created via a known mechanism (dynamic proxy shape, typed shape class binding) and that related DI services are registered in Startup.","Log the shapeType value and reproduce with a minimal call to isolate which handler nulls the result."],"exampleFix":"// before (IShapeFactoryEvents)\npublic void Creating(ShapeCreatingContext context) { context.Cancel = true; }\n// after\npublic void Creating(ShapeCreatingContext context) { /* allow default creation */ }","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"catch (InvalidOperationException ex) when (ex.Message.StartsWith(\"Shape creation failed\"))\n{\n    logger.LogError(ex, \"Shape factory returned null for {Type}\", shapeType);\n    return Results.NotFound();\n}","preventionTips":["Never set Cancel/return null in IShapeFactoryEvents.Creating handlers","Test custom shape types immediately after registering their creation services","Avoid deriving shape types from unvalidated user input"],"tags":["shapes","factory","null"],"backgroundTag":"internal-invariant-violation","analyzedSha":"4306c0717fe573f6fca1b4955909ddab6a192807","analyzedAt":"2026-09-13T17:41:05.024Z","contentChangedAt":"2026-09-13T17:41:05.024Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}