This document provides a detailed guide for AI agents to understand and contribute to the Firebase Admin .NET SDK. Adhering to these guidelines is crucial for maintaining code quality, consistency, and idiomatic C# practices.
The Firebase Admin .NET SDK provides C# developers with access to Firebase services on privileged environments. Its design emphasizes idiomatic C#, thread-safety, and a consistent, predictable API surface.
FirebaseAdmin/FirebaseAdmin.sln: The main solution file for the entire project.FirebaseAdmin/FirebaseAdmin/: The primary source code for the SDK.FirebaseAdmin/FirebaseAdmin/FirebaseApp.cs: The main entry point and initialization class for the SDK.FirebaseAdmin/FirebaseAdmin/Auth/: Source code for the Authentication service.FirebaseAdmin/FirebaseAdmin/Messaging/: Source code for the Firebase Cloud Messaging (FCM) service.FirebaseAdmin/FirebaseAdmin/Util/: Internal helper utilities and classes used across services (e.g. Wrapping low level http exceptions as aFirebaseException).
FirebaseAdmin/FirebaseAdmin.Tests/: Unit tests for the SDK.FirebaseAdmin/FirebaseAdmin.IntegrationTests/: Integration tests.FirebaseAdmin/FirebaseAdmin.Snippets/: Code snippets used in documentation.
- Initialization: The SDK is initialized via the
FirebaseApp.Create()method, which configures a singleton instance. - Service Clients: Services like
FirebaseAuthandFirebaseMessagingare accessed through theFirebaseApp.DefaultInstance. - Error Handling: Exceptions are the primary mechanism for error handling. Custom exceptions inherit from
FirebaseException(defined inFirebaseAdmin/FirebaseAdmin/FirebaseException.cs) and are defined within each service module. - HTTP Communication: All outgoing HTTP requests are managed by a centralized
HttpClientprovided via theAppOptions.HttpClientFactory. - Asynchronous Operations: Asynchronous operations are handled using
Task-based asynchronous programming (async/await).
- Formatting: Code style is enforced using
stylecop. Rundotnet formatto apply the rules. - Naming:
- Constants, classes and public methods use
PascalCase. - Private members are not explicitly prefixed.
- Constants, classes and public methods use
- Unit Tests: Located in
FirebaseAdmin.Tests/, with a file naming pattern of*Test.cs.xunitis the testing framework. - Integration Tests: Located in
FirebaseAdmin.IntegrationTests/. These tests interact with live Firebase services and require a configured service account.
The unit tests can be run using the dotnet test command. Ensure that the required .NET frameworks (net462 and net6.0) are installed in your environment.
To run the tests for a specific framework, use the --framework flag:
# Run tests for .NET 6.0
dotnet test FirebaseAdmin/FirebaseAdmin.Tests --framework net6.0
# Run tests for .NET 4.6.2
dotnet test FirebaseAdmin/FirebaseAdmin.Tests --framework net462Note on Integration Tests: The integration tests require a configured service account and other setup that is not available in all environments. It is recommended to rely on the CI for running integration tests.
- Manager: Dependencies are managed using NuGet.
- Manifest: The
FirebaseAdmin/FirebaseAdmin/FirebaseAdmin.csprojfile lists all dependencies. - Command: To add a dependency, use
dotnet add package <PACKAGE_NAME>.
- Public Method: Define the new public method or changes in the relevant service class (e.g.,
FirebaseAuth.cs). - Internal Logic: Implement the core logic as a private method.
- HTTP Client: Use the existing HTTP Client implementation for that service to make the API call.
- Error Handling: Wrap API calls in
try-catchblocks and throw appropriateFirebaseExceptionsubtypes. - Testing: Add a unit test in the corresponding
*Test.csfile and an integration test inFirebaseAdmin.IntegrationTests/where appropriate. This should be thorough and update any tests where the new changes would affect. - Snippet: Add or update code snippet in
FirebaseAdmin.Snippets/to demonstrate the new feature.
- Add Deprecation Note: Locate where the deprecated object is defined and add a deprecation warning with a note (e.g. [Obsolete("Use X instead")]).
- Suppress Obsolete Warnings: Because
Obsoletewarnings result in build errors, tests and snippets where the object is used should be marked using#pragmadirectives to suppress warnings where the object is used.
- DO: Use the centralized
HttpClientfor all API calls. - DO: Follow the established
async/awaitpatterns for asynchronous code. - DON'T: Expose types from the
FirebaseAdmin/FirebaseAdmin/Utils/directory in any public API. - DON'T: Introduce new third-party dependencies without a compelling reason and discussion with the maintainers.
After implementing and testing a change, you must create a commit and Pull Request following these rules:
1. Commit Message Format: The commit message is critical and is used to generate the Pull Request. It MUST follow this structure:
- Title: Use the Conventional Commits specification:
type(scope): subject.scopeshould be the service package changed (e.g.,auth,rtdb,deps).- Note on Scopes: Some services use specific abbreviations. Use the abbreviation if one exists. Common abbreviations include:
messaging->fcmdataconnect->fdcdatabase->rtdbappcheck->fac
- Example:
fix(auth): Resolve issue with custom token verification - Example:
feat(fcm): Add support for multicast messages
- Body: The body is separated from the title by a blank line and MUST contain the following, in order:
- A brief explanation of the problem and the solution.
- A summary of the testing strategy (e.g., "Added a new unit test to verify the fix.").
- A Context Sources section that lists the
idand repository path of everyAGENTS.mdfile you used to verify context usage.
2. Example Commit Message:
feat(fcm): Add support for multicast messages
This change introduces a new `SendEachForMulticastAsync` method to the messaging client, allowing developers to send a single message to multiple tokens efficiently.
Testing: Added unit tests with a mock server and an integration test in `FirebaseAdmin.IntegrationTests/MessagingTest.cs`.
Context Sources Used:
- id: firebase-admin-dotnet
- id: firebase-admin-dotnet-messaging
3. Pull Request: The Pull Request title and description should be populated directly from the commit message's title and body.
- id: firebase-admin-dotnet