{"record":{"id":"4e73404ad58e8738","repo":"dotnet/orleans","slug":"serviceprovider","errorCode":null,"errorMessage":"serviceProvider","messagePattern":"serviceProvider","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Azure/Orleans.Streaming.EventHubs/Providers/Streams/EventHub/EventHubAdapterFactory.cs","lineNumber":126,"sourceCode":"            string name,\n            EventHubOptions ehOptions,\n            EventHubReceiverOptions receiverOptions,\n            EventHubStreamCachePressureOptions cacheOptions, \n            StreamCacheEvictionOptions cacheEvictionOptions,\n            StreamStatisticOptions statisticOptions,\n            IEventHubDataAdapter dataAdapter,\n            IServiceProvider serviceProvider,\n            ILoggerFactory loggerFactory,\n            IEnvironmentStatisticsProvider environmentStatisticsProvider)\n        {\n            this.Name = name;\n            this.cacheEvictionOptions = cacheEvictionOptions ?? throw new ArgumentNullException(nameof(cacheEvictionOptions));\n            this.statisticOptions = statisticOptions ?? throw new ArgumentNullException(nameof(statisticOptions));\n            this.ehOptions = ehOptions ?? throw new ArgumentNullException(nameof(ehOptions));\n            this.cacheOptions = cacheOptions?? throw new ArgumentNullException(nameof(cacheOptions));\n            this.dataAdapter = dataAdapter ?? throw new ArgumentNullException(nameof(dataAdapter));\n            this.receiverOptions = receiverOptions?? throw new ArgumentNullException(nameof(receiverOptions));\n            this.serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));\n            this.loggerFactory = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory));\n            this.environmentStatisticsProvider = environmentStatisticsProvider;\n            this.orleansInstruments = serviceProvider.GetRequiredService<OrleansInstruments>();\n        }\n\n        public virtual void Init()\n        {\n            this.receivers = new ConcurrentDictionary<QueueId, EventHubAdapterReceiver>();\n\n            InitEventHubClient();\n\n            if (this.CacheFactory == null)\n            {\n                this.CacheFactory = CreateCacheFactory(this.cacheOptions).CreateCache;\n            }\n\n            if (this.StreamFailureHandlerFactory == null)\n            {","sourceCodeStart":108,"sourceCodeEnd":144,"githubUrl":"https://github.com/dotnet/orleans/blob/fca799fa70ecb6ad975224271703ca43221f58de/src/Azure/Orleans.Streaming.EventHubs/Providers/Streams/EventHub/EventHubAdapterFactory.cs#L108-L144","documentation":"EventHubAdapterFactory requires a non-null IServiceProvider because it is used immediately at construction time to resolve OrleansInstruments (via GetRequiredService) and later during Init/MakeReceiver to resolve keyed checkpointer factories, LoadSheddingOptions, and other framework services. A null service provider would cause a NullReferenceException at the very next line (GetRequiredService<OrleansInstruments>), so the null check fails fast with a clearer message.","triggerScenarios":"Constructing EventHubAdapterFactory directly with serviceProvider set to null. The default Create factory always passes the IServiceProvider from the factory's own services parameter, so this fires only when a caller manually constructs the adapter or a custom factory method passes null.","commonSituations":"Manual construction outside of DI (e.g., in a unit test or a non-standard hosting path) where no IServiceProvider is available; a custom factory or ActivatorUtilities.CreateInstance call where the services argument is accidentally omitted or null.","solutions":["Always pass the IServiceProvider obtained from the silo/client's service collection.","In tests, build a minimal ServiceCollection with the required services (OrleansInstruments, ILoggerFactory, IEnvironmentStatisticsProvider, etc.) and pass its BuildServiceProvider().","Use EventHubAdapterFactory.Create(services, name) which correctly threads the provider through."],"exampleFix":"// before\nvar factory = new EventHubAdapterFactory(\n    name, ehOptions, receiverOptions, cacheOptions, evictionOpts, statOpts, dataAdapter, null /* serviceProvider */, loggerFactory, envStats);\n\n// after\nvar services = new ServiceCollection()\n    .AddSingleton<ILoggerFactory>(loggerFactory)\n    .AddSingleton<OrleansInstruments>()\n    .BuildServiceProvider();\nvar factory = new EventHubAdapterFactory(\n    name, ehOptions, receiverOptions, cacheOptions, evictionOpts, statOpts, dataAdapter, services, loggerFactory, envStats);","handlingStrategy":"validation","validationCode":"// Ensure IServiceProvider is available:\nif (serviceProvider is null)\n    throw new InvalidOperationException(\n        \"An IServiceProvider is required. Build one from a ServiceCollection \" +\n        \"with the necessary Orleans services registered.\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always thread the IServiceProvider from the silo/client builder into the factory.","In tests, build a minimal ServiceCollection with OrleansInstruments, ILoggerFactory, and IEnvironmentStatisticsProvider.","Prefer EventHubAdapterFactory.Create(services, name) which passes the provider correctly."],"tags":["configuration","dependency-injection","eventhub","constructor","null-argument","service-provider"],"backgroundTag":null,"analyzedSha":"fca799fa70ecb6ad975224271703ca43221f58de","analyzedAt":"2026-08-13T19:55:57.938Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}