|
| 1 | +--- |
| 2 | +type: docs |
| 3 | +title: "Dapr .NET SDK Development with Dapr CLI" |
| 4 | +linkTitle: "Experimental Attributes" |
| 5 | +weight: 61000 |
| 6 | +description: Learn about local development with the Dapr CLI |
| 7 | +--- |
| 8 | + |
| 9 | +## Experimental Attributes |
| 10 | + |
| 11 | +### Introduction to Experimental Attributes |
| 12 | + |
| 13 | +With the release of .NET 8, C# 12 introduced the `[Experimental]` attribute, which provides a standardized way to mark |
| 14 | +APIs that are still in development or experimental. This attribute is defined in the `System.Diagnostics.CodeAnalysis` |
| 15 | +namespace and requires a diagnostic ID parameter used to generate compiler warnings when the experimental API |
| 16 | +is used. |
| 17 | + |
| 18 | +In the Dapr .NET SDK, we now use the `[Experimental]` attribute instead of `[Obsolete]` to mark building blocks and |
| 19 | +components that have not yet passed the stable lifecycle certification. This approach provides a clearer distinction |
| 20 | +between: |
| 21 | + |
| 22 | +1. **Experimental APIs** - Features that are available but still evolving and have not yet been certified as stable |
| 23 | +according to the [Dapr Component Certification Lifecycle](https://docs.dapr.io/operations/components/certification-lifecycle/). |
| 24 | + |
| 25 | +2. **Obsolete APIs** - Features that are truly deprecated and will be removed in a future release. |
| 26 | + |
| 27 | +### Usage in the Dapr .NET SDK |
| 28 | + |
| 29 | +In the Dapr .NET SDK, we apply the `[Experimental]` attribute at the class level for building blocks that are still in |
| 30 | +the Alpha or Beta stages of the [Component Certification Lifecycle](https://docs.dapr.io/operations/components/certification-lifecycle/). |
| 31 | +The attribute includes: |
| 32 | + |
| 33 | +- A diagnostic ID that identifies the experimental building block |
| 34 | +- A URL that points to the relevant documentation for that block |
| 35 | + |
| 36 | +For example: |
| 37 | + |
| 38 | +```csharp |
| 39 | +using System.Diagnostics.CodeAnalysis; |
| 40 | +namespace Dapr.Cryptography.Encryption |
| 41 | +{ |
| 42 | + [Experimental("DAPR_CRYPTOGRAPHY", UrlFormat = "https://docs.dapr.io/developing-applications/building-blocks/cryptography/cryptography-overview/")] |
| 43 | + public class DaprEncryptionClient |
| 44 | + { |
| 45 | + // Implementation |
| 46 | + } |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +The diagnostic IDs follow a naming convention of `DAPR_[BUILDING_BLOCK_NAME]`, such as: |
| 51 | + |
| 52 | +- `DAPR_CONVERSATION` - For the Conversation building block |
| 53 | +- `DAPR_CRYPTOGRAPHY` - For the Cryptography building block |
| 54 | +- `DAPR_JOBS` - For the Jobs building block |
| 55 | +- `DAPR_DISTRIBUTEDLOCK` - For the Distributed Lock building block |
| 56 | + |
| 57 | +### Suppressing Experimental Warnings |
| 58 | + |
| 59 | +When you use APIs marked with the `[Experimental]` attribute, the compiler will generate errors. |
| 60 | +To build your solution without marking your own code as experimental, you will need to suppress these errors. Here are |
| 61 | +several approaches to do this: |
| 62 | + |
| 63 | +#### Option 1: Using #pragma directive |
| 64 | + |
| 65 | +You can use the `#pragma warning` directive to suppress the warning for specific sections of code: |
| 66 | + |
| 67 | +```csharp |
| 68 | +// Disable experimental warning |
| 69 | +#pragma warning disable DAPR_CRYPTOGRAPHY |
| 70 | +// Your code using the experimental API |
| 71 | +var client = new DaprEncryptionClient(); |
| 72 | +// Re-enable the warning |
| 73 | +#pragma warning restore DAPR_CRYPTOGRAPHY |
| 74 | +``` |
| 75 | + |
| 76 | +This approach is useful when you want to suppress warnings only for specific sections of your code. |
| 77 | + |
| 78 | +#### Option 2: Project-level suppression |
| 79 | + |
| 80 | +To suppress warnings for an entire project, add the following to your `.csproj` file. |
| 81 | +file. |
| 82 | + |
| 83 | +```xml |
| 84 | +<PropertyGroup> |
| 85 | + <NoWarn>$(NoWarn);DAPR_CRYPTOGRAPHY</NoWarn> |
| 86 | +</PropertyGroup> |
| 87 | +``` |
| 88 | + |
| 89 | +You can include multiple diagnostic IDs separated by semicolons: |
| 90 | + |
| 91 | +```xml |
| 92 | +<PropertyGroup> |
| 93 | + <NoWarn>$(NoWarn);DAPR_CONVERSATION;DAPR_JOBS;DAPR_DISTRIBUTEDLOCK;DAPR_CRYPTOGRAPHY</NoWarn> |
| 94 | +</PropertyGroup> |
| 95 | +``` |
| 96 | + |
| 97 | +This approach is particularly useful for test projects that need to use experimental APIs. |
| 98 | + |
| 99 | +#### Option 3: Directory-level suppression |
| 100 | + |
| 101 | +For suppressing warnings across multiple projects in a directory, add a `Directory.Build.props` file: |
| 102 | + |
| 103 | +```xml |
| 104 | +<PropertyGroup> |
| 105 | + <NoWarn>$(NoWarn);DAPR_CONVERSATION;DAPR_JOBS;DAPR_DISTRIBUTEDLOCK;DAPR_CRYPTOGRAPHY</NoWarn> |
| 106 | +</PropertyGroup> |
| 107 | +``` |
| 108 | + |
| 109 | +This file should be placed in the root directory of your test projects. You can learn more about using |
| 110 | +`Directory.Build.props` files in the |
| 111 | +[MSBuild documentation](https://learn.microsoft.com/en-us/visualstudio/msbuild/customize-by-directory). |
| 112 | + |
| 113 | +### Lifecycle of Experimental APIs |
| 114 | + |
| 115 | +As building blocks move through the certification lifecycle and reach the "Stable" stage, the `[Experimental]` attribute will be removed. No migration or code changes will be required from users when this happens, except for the removal of any warning suppressions if they were added. |
| 116 | + |
| 117 | +Conversely, the `[Obsolete]` attribute will now be reserved exclusively for APIs that are truly deprecated and scheduled for removal. When you see a method or class marked with `[Obsolete]`, you should plan to migrate away from it according to the migration guidance provided in the attribute message. |
| 118 | + |
| 119 | +### Best Practices |
| 120 | + |
| 121 | +1. **In application code:** |
| 122 | + - Be cautious when using experimental APIs, as they may change in future releases |
| 123 | + - Consider isolating usage of experimental APIs to make future updates easier |
| 124 | + - Document your use of experimental APIs for team awareness |
| 125 | + |
| 126 | +2. **In test code:** |
| 127 | + - Use project-level suppression to avoid cluttering test code with warning suppressions |
| 128 | + - Regularly review which experimental APIs you're using and check if they've been stabilized |
| 129 | + |
| 130 | +3. **When contributing to the SDK:** |
| 131 | + - Use `[Experimental]` for new building blocks that haven't completed certification |
| 132 | + - Use `[Obsolete]` only for truly deprecated APIs |
| 133 | + - Provide clear documentation links in the `UrlFormat` parameter |
| 134 | + |
| 135 | +### Additional Resources |
| 136 | + |
| 137 | +- [Dapr Component Certification Lifecycle](https://docs.dapr.io/operations/components/certification-lifecycle/) |
| 138 | +- [C# Experimental Attribute Documentation](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-12.0/experimental-attribute) |
0 commit comments