{"record":{"id":"7f4ce6a94773f232","repo":"dotnet/orleans","slug":"provider-type-providertype-must-implement-idocum","errorCode":null,"errorMessage":"Provider type {providerType} must implement IDocumentIdProvider or IPartitionKeyProvider.","messagePattern":"Provider type (.+?) must implement IDocumentIdProvider or IPartitionKeyProvider\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Azure/Orleans.Persistence.Cosmos/HostingExtensions.cs","lineNumber":295,"sourceCode":"        if (typeof(IDocumentIdProvider).IsAssignableFrom(providerType))\n        {\n            services.AddKeyedSingleton(typeof(IDocumentIdProvider), name, providerType);\n            return;\n        }\n\n#pragma warning disable CS0618 // Type or member is obsolete\n        if (typeof(IPartitionKeyProvider).IsAssignableFrom(providerType))\n        {\n            services.AddKeyedSingleton(typeof(IPartitionKeyProvider), name, providerType);\n            if (registerUnkeyedPartitionProvider)\n            {\n                services.TryAddSingleton(typeof(IPartitionKeyProvider), providerType);\n            }\n\n            return;\n        }\n\n        throw new ArgumentException(\n            $\"Provider type {providerType} must implement {nameof(IDocumentIdProvider)} or {nameof(IPartitionKeyProvider)}.\",\n            nameof(providerType));\n#pragma warning restore CS0618 // Type or member is obsolete\n    }\n}","sourceCodeStart":277,"sourceCodeEnd":300,"githubUrl":"https://github.com/dotnet/orleans/blob/fca799fa70ecb6ad975224271703ca43221f58de/src/Azure/Orleans.Persistence.Cosmos/HostingExtensions.cs#L277-L300","documentation":"Thrown by HostingExtensions when registering a provider type for document ID or partition key resolution, but the type implements neither IDocumentIdProvider nor IPartitionKeyProvider. The code checks IPartitionKeyProvider.IsAssignableFrom(providerType); if false, it throws ArgumentException naming both required interfaces.","triggerScenarios":"Calling a hosting extension (e.g., AddPartitionKeyProvider or AddDocumentIdProvider) with a Type that does not implement either required interface. The extension checks assignability before registering and throws if neither interface is satisfied.","commonSituations":"Developer creates a class intended to be a partition key provider but forgets to implement IPartitionKeyProvider or IDocumentIdProvider. Renaming an interface during a refactor. Passing the wrong type by mistake (e.g., the grain class instead of the provider class). Version mismatch where the interface moved to a different namespace.","solutions":["Ensure the provider class implements IDocumentIdProvider (or the obsolete IPartitionKeyProvider), e.g., class MyProvider : IDocumentIdProvider { ... }.","Verify the correct type is passed to the hosting extension — not the grain type, not the options type.","Check that the Orleans.Persistence.Cosmos package is referenced so the interface is available.","If migrating from IPartitionKeyProvider (obsolete), switch to IDocumentIdProvider."],"exampleFix":"// before: missing interface\npublic class MyPartitionKeyProvider\n{\n    public string GetPartitionKey(GrainId grainId) => grainId.ToString();\n}\n\n// after: implement the interface\npublic class MyPartitionKeyProvider : IDocumentIdProvider\n{\n    public DocumentId GetDocumentId(GrainId grainId) => new(grainId.ToString(), grainId.ToString());\n}","handlingStrategy":"type-guard","validationCode":"// Verify before registration\nvar providerType = typeof(MyProvider);\nif (!typeof(IDocumentIdProvider).IsAssignableFrom(providerType)\n    && !typeof(IPartitionKeyProvider).IsAssignableFrom(providerType))\n    throw new InvalidOperationException($\"{providerType.Name} must implement IDocumentIdProvider or IPartitionKeyProvider.\");","typeGuard":"static bool IsValidProviderType(Type t) =>\n    typeof(IDocumentIdProvider).IsAssignableFrom(t) || typeof(IPartitionKeyProvider).IsAssignableFrom(t);","tryCatchPattern":"try { siloBuilder.AddPartitionKeyProvider(name, providerType); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"must implement\"))\n{\n    logger.LogError(ex, \"Provider type does not implement IDocumentIdProvider or IPartitionKeyProvider.\");\n    throw;\n}","preventionTips":["Always implement IDocumentIdProvider (or the obsolete IPartitionKeyProvider) on provider classes.","Use a compile-time check or unit test to verify provider types implement the required interface.","Migrate from IPartitionKeyProvider to IDocumentIdProvider when upgrading.","Double-check the type passed to the hosting extension — pass the provider, not the grain."],"tags":["cosmos-db","interface","type-check","configuration","document-id-provider","orleans"],"backgroundTag":null,"analyzedSha":"fca799fa70ecb6ad975224271703ca43221f58de","analyzedAt":"2026-08-13T19:55:57.938Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}