{"record":{"id":"abbb3c1c59a53d7b","repo":"stride3d/stride","slug":"igraphicsdevicefactory-is-not-registered-as-a-service","errorCode":null,"errorMessage":"IGraphicsDeviceFactory is not registered as a service","messagePattern":"IGraphicsDeviceFactory is not registered as a service","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Games/GraphicsDeviceManager.cs","lineNumber":110,"sourceCode":"\n        /// <summary>\n        ///   Initializes a new instance of the <see cref=\"GraphicsDeviceManager\"/> class.\n        /// </summary>\n        /// <param name=\"game\">The Game that needs to manage the Graphics Device.</param>\n        /// <exception cref=\"ArgumentNullException\">The <paramref name=\"game\"/> cannot be <see langword=\"null\"/>.</exception>\n        /// <exception cref=\"InvalidOperationException\">\n        ///   Could not get the required <see cref=\"IGraphicsDeviceFactory\"/> service from the <paramref name=\"game\"/>'s services.\n        /// </exception>\n        internal GraphicsDeviceManager(GameBase game)\n        {\n            ArgumentNullException.ThrowIfNull(game);\n\n            this.game = game;\n\n            lockDeviceCreation = new object();\n\n            graphicsDeviceFactory = game.Services.GetService<IGraphicsDeviceFactory>()\n                ?? throw new InvalidOperationException(\"IGraphicsDeviceFactory is not registered as a service\");\n\n            game.WindowCreated += GameOnWindowCreated;\n        }\n\n\n        /// <summary>\n        ///   Occurs when a new Graphics Device is successfully created.\n        /// </summary>\n        public event EventHandler<EventArgs> DeviceCreated;\n        /// <summary>\n        ///   Occurs when the Graphics Device is being disposed.\n        /// </summary>\n        public event EventHandler<EventArgs> DeviceDisposing;\n\n        /// <summary>\n        ///   Occurs when the Graphics Device is being reset.\n        /// </summary>\n        public event EventHandler<EventArgs> DeviceReset;","sourceCodeStart":92,"sourceCodeEnd":128,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Games/GraphicsDeviceManager.cs#L92-L128","documentation":"GraphicsDeviceManager's constructor resolves the IGraphicsDeviceFactory from the game's service registry and throws when it is absent. The factory is what actually builds the platform GraphicsDevice, so without it the manager cannot function; Stride registers it in its normal platform bootstrap, so this failure indicates the service registry was not populated.","triggerScenarios":"new GraphicsDeviceManager(game) where game.Services does not contain an IGraphicsDeviceFactory registration — typically when GraphicsDeviceManager is constructed before the platform bootstrap registers services, or with a custom Game subclass that bypasses the standard setup.","commonSituations":"Custom Game classes that never call the platform registration code, headless/unit-test harnesses instantiating GraphicsDeviceManager, constructing the manager too early in a custom Main, or a stripped/custom service registry.","solutions":["Use the standard platform Game entry point (e.g. MyGameMain with the platform bootstrap) so IGraphicsDeviceFactory is registered before GraphicsDeviceManager is created.","Register a factory in game.Services before constructing the manager: game.Services.AddService<IGraphicsDeviceFactory>(new DeviceFactorySDL()); (platform-appropriate implementation).","Don't construct GraphicsDeviceManager manually in tests/headless code; mock at a higher level or run the normal initialization path."],"exampleFix":"// before\nvar game = new Game();\nvar gdm = new GraphicsDeviceManager(game); // factory missing\n// after\nvar game = new Game();\ngame.Services.AddService<IGraphicsDeviceFactory>(new DeviceFactorySDL());\nvar gdm = new GraphicsDeviceManager(game);","handlingStrategy":"validation","validationCode":"if (game.Services.GetService<IGraphicsDeviceFactory>() is null)\n    game.Services.AddService<IGraphicsDeviceFactory>(new DeviceFactorySDL());\nvar gdm = new GraphicsDeviceManager(game);","typeGuard":"bool FactoryRegistered(Game g) => g.Services.GetService<IGraphicsDeviceFactory>() != null;","tryCatchPattern":"try { var gdm = new GraphicsDeviceManager(game); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"IGraphicsDeviceFactory\"))\n{\n    game.Services.AddService<IGraphicsDeviceFactory>(new DeviceFactorySDL());\n    gdm = new GraphicsDeviceManager(game);\n}","preventionTips":["Always start from the platform project template so services are registered in order.","Never construct GraphicsDeviceManager before platform bootstrap completes.","In headless code, mock above GraphicsDeviceManager rather than instantiating it."],"tags":["game-engine","dependency-injection","service-locator"],"backgroundTag":"missing-dependency","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}