Dapr offers a variety of packages to help with the development of .NET applications. Using them you can create .NET clients, servers, and virtual actors with Dapr.
Note that while .NET 6 is generally supported as the minimum .NET requirement across the Dapr .NET SDK packages
and .NET 7 is the minimally supported version of .NET by Dapr.Workflows in Dapr v1.15, only .NET 8 and .NET 9 will
continue to be supported by Dapr in v1.16 and later.
Installation
To get started with the Client .NET SDK, install the Dapr .NET SDK package:
dotnet add package Dapr.Client
Try it out
Put the Dapr .NET SDK to the test. Walk through the .NET quickstarts and tutorials to see Dapr in action:
usingvarclient=newDaprClientBuilder().UseTimeout(TimeSpan.FromSeconds(2)).// Optionally, set a timeoutBuild();// Invokes a POST method named "deposit" that takes input of type "Transaction"vardata=new{id="17",amount=99m};varaccount=awaitclient.InvokeMethodAsync<object,Account>("routing","deposit",data,cancellationToken);Console.WriteLine("Returned: id:{0} | Balance:{1}",account.Id,account.Balance);
varclient=DaprClient.CreateInvokeHttpClient(appId:"routing");// To set a timeout on the HTTP client:client.Timeout=TimeSpan.FromSeconds(2);vardeposit=newTransaction{Id="17",Amount=99m};varresponse=awaitclient.PostAsJsonAsync("/deposit",deposit,cancellationToken);varaccount=awaitresponse.Content.ReadFromJsonAsync<Account>(cancellationToken:cancellationToken);Console.WriteLine("Returned: id:{0} | Balance:{1}",account.Id,account.Balance);
gRPC
You can use the DaprClient to invoke your services over gRPC.
Visit .NET SDK examples for code samples and instructions to try out pub/sub
Interact with output bindings
usingvarclient=newDaprClientBuilder().Build();// Example payload for the Twilio SendGrid bindingvaremail=new{metadata=new{emailTo="customer@example.com",subject="An email from Dapr SendGrid binding",},data="<h1>Testing Dapr Bindings</h1>This is a test.<br>Bye!",};awaitclient.InvokeBindingAsync("send-email","create",email);
varclient=newDaprClientBuilder().Build();// Retrieve a key-value-pair-based secret - returns a Dictionary<string, string>varsecrets=awaitclient.GetSecretAsync("mysecretstore","key-value-pair-secret");Console.WriteLine($"Got secret keys: {string.Join(",", secrets.Keys)}");
varclient=newDaprClientBuilder().Build();// Retrieve a key-value-pair-based secret - returns a Dictionary<string, string>varsecrets=awaitclient.GetSecretAsync("mysecretstore","key-value-pair-secret");Console.WriteLine($"Got secret keys: {string.Join(",", secrets.Keys)}");// Retrieve a single-valued secret - returns a Dictionary<string, string>// containing a single value with the secret name as the keyvardata=awaitclient.GetSecretAsync("mysecretstore","single-value-secret");varvalue=data["single-value-secret"]Console.WriteLine("Got a secret value, I'm not going to be print it, it's a secret!");
varclient=newDaprClientBuilder().Build();// Retrieve a specific set of keys.varspecificItems=awaitclient.GetConfiguration("configstore",newList<string>(){"key1","key2"});Console.WriteLine($"Here are my values:\n{specificItems[0].Key} -> {specificItems[0].Value}\n{specificItems[1].Key} -> {specificItems[1].Value}");// Retrieve all configuration items by providing an empty list.varspecificItems=awaitclient.GetConfiguration("configstore",newList<string>());Console.WriteLine($"I got {configItems.Count} entires!");foreach(variteminconfigItems){Console.WriteLine($"{item.Key} -> {item.Value}")}
Subscribe to Configuration Keys
varclient=newDaprClientBuilder().Build();// The Subscribe Configuration API returns a wrapper around an IAsyncEnumerable<IEnumerable<ConfigurationItem>>.// Iterate through it by accessing its Source in a foreach loop. The loop will end when the stream is severed// or if the cancellation token is cancelled.varsubscribeConfigurationResponse=awaitdaprClient.SubscribeConfiguration(store,keys,metadata,cts.Token);awaitforeach(varitemsinsubscribeConfigurationResponse.Source.WithCancellation(cts.Token)){foreach(variteminitems){Console.WriteLine($"{item.Key} -> {item.Value}")}}
Distributed lock (Alpha)
Acquire a lock
usingSystem;usingDapr.Client;namespaceLockService{classProgram{ [Obsolete("Distributed Lock API is in Alpha, this can be removed once it is stable.")]staticasyncTaskMain(string[]args){vardaprLockName="lockstore";varfileName="my_file_name";varclient=newDaprClientBuilder().Build();// Locking with this approach will also unlock it automatically, as this is a disposable objectawaitusing(varfileLock=awaitclient.Lock(DAPR_LOCK_NAME,fileName,"random_id_abc123",60)){if(fileLock.Success){Console.WriteLine("Success");}else{Console.WriteLine($"Failed to lock {fileName}.");}}}}}
This health endpoint returns true when Dapr has initialized all its components, but may not have finished setting up a communication channel with your application.
This is best used when you want to utilize a Dapr component in your startup path, for instance, loading secrets from a secretstore.
The DaprClient also provides a helper method to wait for the sidecar to become healthy (components only). When using this method, it is recommended to include a CancellationToken to
allow for the request to timeout. Below is an example of how this is used in the DaprSecretStoreConfigurationProvider.
// Wait for the Dapr sidecar to report healthy before attempting use Dapr components.using(vartokenSource=newCancellationTokenSource(sidecarWaitTimeout)){awaitclient.WaitForSidecarAsync(tokenSource.Token);}// Perform Dapr component operations here i.e. fetching secrets.
A DaprClient holds access to networking resources in the form of TCP sockets used to communicate with the Dapr sidecar. DaprClient implements IDisposable to support eager cleanup of resources.
Dependency Injection
The AddDaprClient() method will register the Dapr client with ASP.NET Core dependency injection. This method accepts an optional
options delegate for configuring the DaprClient and an ServiceLifetime argument, allowing you to specify a different lifetime
for the registered resources instead of the default Singleton value.
The following example assumes all default values are acceptable and is sufficient to register the DaprClient.
services.AddDaprClient();
The optional configuration delegates are used to configure DaprClient by specifying options on the provided DaprClientBuilder
as in the following example:
The another optional configuration delegate overload provides access to both the DaprClientBuilder as well as an IServiceProvider
allowing for more advanced configurations that may require injecting services from the dependency injection container.
Rather than using dependency injection, a DaprClient can also be built using the static client builder.
For best performance, create a single long-lived instance of DaprClient and provide access to that shared instance throughout your application. DaprClient instances are thread-safe and intended to be shared.
Avoid creating a DaprClient per-operation and disposing it when the operation is complete.
Configuring DaprClient
A DaprClient can be configured by invoking methods on DaprClientBuilder class before calling .Build() to create the client. The settings for each DaprClient object are separate and cannot be changed after calling .Build().
By default, the DaprClientBuilder will prioritize the following locations, in the following order, to source the configuration
values:
The value provided to a method on the DaprClientBuilder (e.g. UseTimeout(TimeSpan.FromSeconds(30)))
The value pulled from an optionally injected IConfiguration matching the name expected in the associated environment variable
The value pulled from the associated environment variable
Default values
Configuring on DaprClientBuilder
The DaprClientBuilder contains the following methods to set configuration options:
UseHttpEndpoint(string): The HTTP endpoint of the Dapr sidecar
UseGrpcEndpoint(string): Sets the gRPC endpoint of the Dapr sidecar
UseGrpcChannelOptions(GrpcChannelOptions): Sets the gRPC channel options used to connect to the Dapr sidecar
UseHttpClientFactory(IHttpClientFactory): Configures the DaprClient to use a registered IHttpClientFactory when building HttpClient instances
UseJsonSerializationOptions(JsonSerializerOptions): Used to configure JSON serialization
UseDaprApiToken(string): Adds the provided token to every request to authenticate to the Dapr sidecar
UseTimeout(TimeSpan): Specifies a timeout value used by the HttpClient when communicating with the Dapr sidecar
Configuring From IConfiguration
Rather than rely on sourcing configuration values directly from environment variables or because the values are sourced
from dependency injected services, another options is to make these values available on IConfiguration.
For example, you might be registering your application in a multi-tenant environment and need to prefix the environment
variables used. The following example shows how these values can be sourced from the environment variables to your
IConfiguration when their keys are prefixed with test_;
varbuilder=WebApplication.CreateBuilder(args);builder.Configuration.AddEnvironmentVariables("test_");//Retrieves all environment variables that start with "test_" and removes the prefix when sourced from IConfigurationbuilder.Services.AddDaprClient();
Configuring From Environment Variables
The SDK will read the following environment variables to configure the default values:
DAPR_HTTP_ENDPOINT: used to find the HTTP endpoint of the Dapr sidecar, example: https://dapr-api.mycompany.com
DAPR_GRPC_ENDPOINT: used to find the gRPC endpoint of the Dapr sidecar, example: https://dapr-grpc-api.mycompany.com
DAPR_HTTP_PORT: if DAPR_HTTP_ENDPOINT is not set, this is used to find the HTTP local endpoint of the Dapr sidecar
DAPR_GRPC_PORT: if DAPR_GRPC_ENDPOINT is not set, this is used to find the gRPC local endpoint of the Dapr sidecar
DAPR_API_TOKEN: used to set the API Token
Note
If both DAPR_HTTP_ENDPOINT and DAPR_HTTP_PORT are specified, the port value from DAPR_HTTP_PORT will be ignored in favor of the port
implicitly or explicitly defined on DAPR_HTTP_ENDPOINT. The same is true of both DAPR_GRPC_ENDPOINT and DAPR_GRPC_PORT.
Configuring gRPC channel options
Dapr’s use of CancellationToken for cancellation relies on the configuration of the gRPC channel options and this is enabled by default. If you need to configure these options yourself, make sure to enable the ThrowOperationCanceledOnCancellation setting.
The APIs on DaprClient that perform asynchronous operations accept an optional CancellationToken parameter. This follows a standard .NET idiom for cancellable operations. Note that when cancellation occurs, there is no guarantee that the remote endpoint stops processing the request, only that the client has stopped waiting for completion.
When an operation is cancelled, it will throw an OperationCancelledException.
Understanding DaprClient JSON serialization
Many methods on DaprClient perform JSON serialization using the System.Text.Json serializer. Methods that accept an application data type as an argument will JSON serialize it, unless the documentation clearly states otherwise.
It is worth reading the System.Text.Json documentation if you have advanced requirements. The Dapr .NET SDK provides no unique serialization behavior or customizations - it relies on the underlying serializer to convert data to and from the application’s .NET types.
DaprClient is configured to use a serializer options object configured from JsonSerializerDefaults.Web. This means that DaprClient will use camelCase for property names, allow reading quoted numbers ("10.99"), and will bind properties case-insensitively. These are the same settings used with ASP.NET Core and the System.Text.Json.Http APIs, and are designed to follow interoperable web conventions.
System.Text.Json as of .NET 5.0 does not have good support for all of F# language features built-in. If you are using F# you may want to use one of the converter packages that add support for F#’s features such as FSharp.SystemTextJson.
Simple guidance for JSON serialization
Your experience using JSON serialization and DaprClient will be smooth if you use a feature set that maps to JSON’s type system. These are general guidelines that will simplify your code where they can be applied.
Avoid inheritance and polymorphism
Do not attempt to serialize data with cyclic references
Do not put complex or expensive logic in constructors or property accessors
Use .NET types that map cleanly to JSON types (numeric types, strings, DateTime)
Create your own classes for top-level messages, events, or state values so you can add properties in the future
Design types with get/set properties OR use the supported pattern for immutable types with JSON
Polymorphism and serialization
The System.Text.Json serializer used by DaprClient uses the declared type of values when performing serialization.
This section will use DaprClient.SaveStateAsync<TValue>(...) in examples, but the advice is applicable to any Dapr building block exposed by the SDK.
publicclassWidget{publicstringColor{get;set;}}...// Storing a Widget value as JSON in the state storewidgetwidget=newWidget(){Color="Green",};awaitclient.SaveStateAsync("mystatestore","mykey",widget);
In the example above, the type parameter TValue has its type argument inferred from the type of the widget variable. This is important because the System.Text.Json serializer will perform serialization based on the declared type of the value. The result is that the JSON value { "color": "Green" } will be stored.
Consider what happens when you try to use derived type of Widget:
publicclassWidget{publicstringColor{get;set;}}publicclassSuperWidget:Widget{publicboolHasSelfCleaningFeature{get;set;}}...// Storing a SuperWidget value as JSON in the state storeWidgetwidget=newSuperWidget(){Color="Green",HasSelfCleaningFeature=true,};awaitclient.SaveStateAsync("mystatestore","mykey",widget);
In this example we’re using a SuperWidget but the variable’s declared type is Widget. Since the JSON serializer’s behavior is determined by the declared type, it only sees a simple Widget and will save the value { "color": "Green" } instead of { "color": "Green", "hasSelfCleaningFeature": true }.
If you want the properties of SuperWidget to be serialized, then the best option is to override the type argument with object. This will cause the serializer to include all data as it knows nothing about the type.
In this guide, you will learn how to use IActorProxyFactory.
Tip
For a non-dependency-injected application, you can use the static methods on ActorProxy. Since the ActorProxy methods are error prone, try to avoid using them when configuring custom settings.
Identifying an actor
All of the APIs on IActorProxyFactory will require an actor type and actor id to communicate with an actor. For strongly-typed clients, you also need one of its interfaces.
Actor type uniquely identifies the actor implementation across the whole application.
Actor id uniquely identifies an instance of that type.
If you don’t have an actor id and want to communicate with a new instance, create a random id with ActorId.CreateRandom(). Since the random id is a cryptographically strong identifier, the runtime will create a new actor instance when you interact with it.
You can use the type ActorReference to exchange an actor type and actor id with other actors as part of messages.
Two styles of actor client
The actor client supports two different styles of invocation:
Actor client style
Description
Strongly-typed
Strongly-typed clients are based on .NET interfaces and provide the typical benefits of strong-typing. They don’t work with non-.NET actors.
Weakly-typed
Weakly-typed clients use the ActorProxy class. It is recommended to use these only when required for interop or other advanced reasons.
Using a strongly-typed client
The following example uses the CreateActorProxy<> method to create a strongly-typed client. CreateActorProxy<> requires an actor interface type, and will return an instance of that interface.
// Create a proxy for IOtherActor to type OtherActor with a random idvarproxy=this.ProxyFactory.CreateActorProxy<IOtherActor>(ActorId.CreateRandom(),"OtherActor");// Invoke a method defined by the interface to invoke the actor//// proxy is an implementation of IOtherActor so we can invoke its methods directlyawaitproxy.DoSomethingGreat();
Using a weakly-typed client
The following example uses the Create method to create a weakly-typed client. Create returns an instance of ActorProxy.
// Create a proxy for type OtherActor with a random idvarproxy=this.ProxyFactory.Create(ActorId.CreateRandom(),"OtherActor");// Invoke a method by name to invoke the actor//// proxy is an instance of ActorProxy.awaitproxy.InvokeMethodAsync("DoSomethingGreat");
Since ActorProxy is a weakly-typed proxy, you need to pass in the actor method name as a string.
You can also use ActorProxy to invoke methods with both a request and a response message. Request and response messages will be serialized using the System.Text.Json serializer.
// Create a proxy for type OtherActor with a random idvarproxy=this.ProxyFactory.Create(ActorId.CreateRandom(),"OtherActor");// Invoke a method on the proxy to invoke the actor//// proxy is an instance of ActorProxy.varrequest=newMyRequest(){Message="Hi, it's me.",};varresponse=awaitproxy.InvokeMethodAsync<MyRequest,MyResponse>("DoSomethingGreat",request);
When using a weakly-typed proxy, you must proactively define the correct actor method names and message types. When using a strongly-typed proxy, these names and types are defined for you as part of the interface definition.
Actor method invocation exception details
The actor method invocation exception details are surfaced to the caller and the callee, providing an entry point to track down the issue. Exception details include:
Method name
Line number
Exception type
UUID
You use the UUID to match the exception on the caller and callee side. Below is an example of exception details:
Dapr.Actors.ActorMethodInvocationException: Remote Actor Method Exception, DETAILS: Exception: NotImplementedException, Method Name: ExceptionExample, Line Number: 14, Exception uuid: d291a006-84d5-42c4-b39e-d6300e9ac38b
Learn all about authoring and running actors with the .NET SDK
Author actors
ActorHost
The ActorHost:
Is a required constructor parameter of all actors
Is provided by the runtime
Must be passed to the base class constructor
Contains all of the state that allows that actor instance to communicate with the runtime
internalclassMyActor:Actor,IMyActor,IRemindable{publicMyActor(ActorHosthost)// Accept ActorHost in the constructor:base(host)// Pass ActorHost to the base class constructor{}}
Since the ActorHost contains state unique to the actor, you don’t need to pass the instance into other parts of your code. It’s recommended only create your own instances of ActorHost in tests.
Dependency injection
Actors support dependency injection of additional parameters into the constructor. Any other parameters you define will have their values satisfied from the dependency injection container.
internalclassMyActor:Actor,IMyActor,IRemindable{publicMyActor(ActorHosthost,BankServicebank)// Accept BankService in the constructor:base(host){...}}
An actor type should have a single public constructor. The actor infrastructure uses the ActivatorUtilities pattern for constructing actor instances.
// In Startup.cspublicvoidConfigureServices(IServiceCollectionservices){...// Register additional types with dependency injection.services.AddSingleton<BankService>();}
Each actor instance has its own dependency injection scope and remains in memory for some time after performing an operation. During that time, the dependency injection scope associated with the actor is also considered live. The scope will be released when the actor is deactivated.
If an actor injects an IServiceProvider in the constructor, the actor will receive a reference to the IServiceProvider associated with its scope. The IServiceProvider can be used to resolve services dynamically in the future.
internalclassMyActor:Actor,IMyActor,IRemindable{publicMyActor(ActorHosthost,IServiceProviderservices)// Accept IServiceProvider in the constructor:base(host){...}}
When using this pattern, avoid creating many instances of transient services which implement IDisposable. Since the scope associated with an actor could be considered valid for a long time, you can accumulate many services in memory. See the dependency injection guidelines for more information.
IDisposable and actors
Actors can implement IDisposable or IAsyncDisposable. It’s recommended that you rely on dependency injection for resource management rather than implementing dispose functionality in application code. Dispose support is provided in the rare case where it is truly necessary.
Logging
Inside an actor class, you have access to an ILogger instance through a property on the base Actor class. This instance is connected to the ASP.NET Core logging system and should be used for all logging inside an actor. Read more about logging. You can configure a variety of different logging formats and output sinks.
Use structured logging with named placeholders like the example below:
publicTask<MyData>GetDataAsync(){this.Logger.LogInformation("Getting state at {CurrentTime}",DateTime.UtcNow);returnthis.StateManager.GetStateAsync<MyData>("my_data");}
When logging, avoid using format strings like: $"Getting state at {DateTime.UtcNow}"
Logging should use the named placeholder syntax which offers better performance and integration with logging systems.
Using an explicit actor type name
By default, the type of the actor, as seen by clients, is derived from the name of the actor implementation class. The default name will be the class name (without namespace).
If desired, you can specify an explicit type name by attaching an ActorAttribute attribute to the actor implementation class.
In the example above, the name will be MyCustomActorTypeName.
No change is needed to the code that registers the actor type with the runtime, providing the value via the attribute is all that is required.
Host actors on the server
Registering actors
Actor registration is part of ConfigureServices in Startup.cs. You can register services with dependency injection via the ConfigureServices method. Registering the set of actor types is part of the registration of actor services.
Inside ConfigureServices you can:
Register the actor runtime (AddActors)
Register actor types (options.Actors.RegisterActor<>)
Configure actor runtime settings options
Register additional service types for dependency injection into actors (services)
// In Startup.cspublicvoidConfigureServices(IServiceCollectionservices){// Register actor runtime with DIservices.AddActors(options=>{// Register actor types and configure actor settingsoptions.Actors.RegisterActor<MyActor>();// Configure default settingsoptions.ActorIdleTimeout=TimeSpan.FromMinutes(10);options.ActorScanInterval=TimeSpan.FromSeconds(35);options.DrainOngoingCallTimeout=TimeSpan.FromSeconds(35);options.DrainRebalancedActors=true;});// Register additional services for use with actorsservices.AddSingleton<BankService>();}
You can configure the JsonSerializerOptions as part of ConfigureServices:
// In Startup.cspublicvoidConfigureServices(IServiceCollectionservices){services.AddActors(options=>{...// Customize JSON optionsoptions.JsonSerializerOptions=...});}
Actors and routing
The ASP.NET Core hosting support for actors uses the endpoint routing system. The .NET SDK provides no support hosting actors with the legacy routing system from early ASP.NET Core releases.
Since actors uses endpoint routing, the actors HTTP handler is part of the middleware pipeline. The following is a minimal example of a Configure method setting up the middleware pipeline with actors.
// in Startup.cspublicvoidConfigure(IApplicationBuilderapp,IWebHostEnvironmentenv){if(env.IsDevelopment()){app.UseDeveloperExceptionPage();}app.UseRouting();app.UseEndpoints(endpoints=>{// Register actors handlers that interface with the Dapr runtime.endpoints.MapActorsHandlers();});}
The UseRouting and UseEndpoints calls are necessary to configure routing. Configure actors as part of the pipeline by adding MapActorsHandlers inside the endpoint middleware.
This is a minimal example, it’s valid for Actors functionality to existing alongside:
Controllers
Razor Pages
Blazor
gRPC Services
Dapr pub/sub handler
other endpoints such as health checks
Problematic middleware
Certain middleware may interfere with the routing of Dapr requests to the actors handlers. In particular, the UseHttpsRedirection is problematic for Dapr’s default configuration. Dapr sends requests over unencrypted HTTP by default, which the UseHttpsRedirection middleware will block. This middleware cannot be used with Dapr at this time.
// in Startup.cspublicvoidConfigure(IApplicationBuilderapp,IWebHostEnvironmentenv){if(env.IsDevelopment()){app.UseDeveloperExceptionPage();}// INVALID - this will block non-HTTPS requestsapp.UseHttpsRedirection();// INVALID - this will block non-HTTPS requestsapp.UseRouting();app.UseEndpoints(endpoints=>{// Register actors handlers that interface with the Dapr runtime.endpoints.MapActorsHandlers();});}
Necessary steps to serialize your types using remoted Actors in .NET
Actor Serialization
The Dapr actor package enables you to use Dapr virtual actors within a .NET application with either a weakly- or strongly-typed client. Each utilizes a different serialization approach. This document will review the differences and convey a few key ground rules to understand in either scenario.
Please be advised that it is not a supported scenario to use the weakly- or strongly typed actor clients interchangeably because of these different serialization approaches. The data persisted using one Actor client will not be accessible using the other Actor client, so it is important to pick one and use it consistently throughout your application.
Weakly-typed Dapr Actor client
In this section, you will learn how to configure your C# types so they are properly serialized and deserialized at runtime when using a weakly-typed actor client. These clients use string-based names of methods with request and response payloads that are serialized using the System.Text.Json serializer. Please note that this serialization framework is not specific to Dapr and is separately maintained by the .NET team within the .NET GitHub repository.
When using the weakly-typed Dapr Actor client to invoke methods from your various actors, it’s not necessary to independently serialize or deserialize the method payloads as this will happen transparently on your behalf by the SDK.
The client will use the latest version of System.Text.Json available for the version of .NET you’re building against and serialization is subject to all the inherent capabilities provided in the associated .NET documentation.
The serializer will be configured to use the JsonSerializerOptions.Webdefault options unless overridden with a custom options configuration which means the following are applied:
Deserialization of the property name is performed in a case-insensitive manner
Serialization of the property name is performed using camel casing unless the property is overridden with a [JsonPropertyName] attribute
Deserialization will read numeric values from number and/or string values
Basic Serialization
In the following example, we present a simple class named Doodad though it could just as well be a record as well.
The default property names can be overridden by applying the [JsonPropertyName] attribute to desired properties.
Generally, this isn’t going to be necessary for types you’re persisting to the actor state as you’re not intended to read or write them independent of Dapr-associated functionality, but
the following is provided just to clearly illustrate that it’s possible.
Override Property Names on Classes
Here’s an example demonstrating the use of JsonPropertyName to change the name for the first property following serialization. Note that the last usage of JsonPropertyName on the Count property
matches what it would be expected to serialize to. This is largely just to demonstrate that applying this attribute won’t negatively impact anything - in fact, it might be preferable if you later
decide to change the default serialization options but still need to consistently access the properties previously serialized before that change as JsonPropertyName will override those options.
Because the argument passed in a primary constructor (introduced in C# 12) can be applied to either a property or field within a record, using the [JsonPropertyName] attribute may
require specifying that you intend the attribute to apply to a property and not a field in some ambiguous cases. Should this be necessary, you’d indicate as much in the primary constructor with:
If [property: ] is applied to the [JsonPropertyName] attribute where it’s not necessary, it will not negatively impact serialization or deserialization as the operation will
proceed normally as though it were a property (as it typically would if not marked as such).
Enumeration types
Enumerations, including flat enumerations are serializable to JSON, but the value persisted may surprise you. Again, it’s not expected that the developer should ever engage
with the serialized data independently of Dapr, but the following information may at least help in diagnosing why a seemingly mild version migration isn’t working as expected.
Take the following enum type providing the various seasons in the year:
publicenumSeason{Spring,Summer,Fall,Winter}
We’ll go ahead and use a separate demonstration type that references our Season and simultaneously illustrate how this works with records:
That might be unexpected that our Season.Winter value was represented as a 3, but this is because the serializer is going to automatically use numeric representations
of the enum values starting with zero for the first value and incrementing the numeric value for each additional value available. Again, if a migration were taking place and
a developer had flipped the order of the enums, this would affect a breaking change in your solution as the serialized numeric values would point to different values when deserialized.
Rather, there is a JsonConverter available with System.Text.Json that will instead opt to use a string-based value instead of the numeric value. The [JsonConverter] attribute needs
to be applied to be enum type itself to enable this, but will then be realized in any downstream serialization or deserialization operation that references the enum.
Using the same values from our myEngagement instance above, this would produce the following JSON instead:
{"name":"Ski Trip","season":"Winter"}
As a result, the enum members can be shifted around without fear of introducing errors during deserialization.
Custom Enumeration Values
The System.Text.Json serialization platform doesn’t, out of the box, support the use of [EnumMember] to allow you to change the value of enum that’s used during serialization or deserialization, but
there are scenarios where this could be useful. Again, assume that you’re tasking with refactoring the solution to apply some better names to your various
enums. You’re using the JsonStringEnumConverter<TType> detailed above so you’re saving the name of the enum to value instead of a numeric value, but if you change
the enum name, that will introduce a breaking change as the name will no longer match what’s in state.
Do note that if you opt into using this approach, you should decorate all your enum members with the [EnumMeber] attribute so that the values are consistently applied for each enum value instead
of haphazardly. Nothing will validate this at build or runtime, but it is considered a best practice operation.
How can you specify the precise value persisted while still changing the name of the enum member in this scenario? Use a custom JsonConverter with an extension method that can pull the value
out of the attached [EnumMember] attributes where provided. Add the following to your solution:
publicsealedclassEnumMemberJsonConverter<T>:JsonConverter<T>whereT:struct,Enum{/// <summary>Reads and converts the JSON to type <typeparamref name="T" />.</summary>/// <param name="reader">The reader.</param>/// <param name="typeToConvert">The type to convert.</param>/// <param name="options">An object that specifies serialization options to use.</param>/// <returns>The converted value.</returns>publicoverrideTRead(refUtf8JsonReaderreader,TypetypeToConvert,JsonSerializerOptionsoptions){// Get the string value from the JSON readervarvalue=reader.GetString();// Loop through all the enum valuesforeach(varenumValueinEnum.GetValues<T>()){// Get the value from the EnumMember attribute, if anyvarenumMemberValue=GetValueFromEnumMember(enumValue);// If the values match, return the enum valueif(value==enumMemberValue){returnenumValue;}}// If no match found, throw an exceptionthrownewJsonException($"Invalid value for {typeToConvert.Name}: {value}");}/// <summary>Writes a specified value as JSON.</summary>/// <param name="writer">The writer to write to.</param>/// <param name="value">The value to convert to JSON.</param>/// <param name="options">An object that specifies serialization options to use.</param>publicoverridevoidWrite(Utf8JsonWriterwriter,Tvalue,JsonSerializerOptionsoptions){// Get the value from the EnumMember attribute, if anyvarenumMemberValue=GetValueFromEnumMember(value);// Write the value to the JSON writerwriter.WriteStringValue(enumMemberValue);}privatestaticstringGetValueFromEnumMember(Tvalue){MemberInfo[]member=typeof(T).GetMember(value.ToString(),BindingFlags.DeclaredOnly|BindingFlags.Static|BindingFlags.Public);if(member.Length==0)returnvalue.ToString();object[]customAttributes=member.GetCustomAttributes(typeof(EnumMemberAttribute),false);if(customAttributes.Length!=0){EnumMemberAttributeenumMemberAttribute=(EnumMemberAttribute)customAttributes;if(enumMemberAttribute!=null&&enumMemberAttribute.Value!=null)returnenumMemberAttribute.Value;}returnvalue.ToString();}}
Now let’s add a sample enumerator. We’ll set a value that uses the lower-case version of each enum member to demonstrate this. Don’t forget to decorate the enum with the JsonConverter
attribute and reference our custom converter in place of the numeral-to-string converter used in the last section.
This time, serialization will take into account the values from the attached [EnumMember] attribute providing us a mechanism to refactor our application without necessitating
a complex versioning scheme for our existing enum values in the state.
{"event":"Conference","season":"fall"}
Strongly-typed Dapr Actor client
In this section, you will learn how to configure your classes and records so they are properly serialized and deserialized at runtime when using a strongly-typed actor client. These clients are implemented using .NET interfaces and are not compatible with Dapr Actors written using other languages.
This actor client serializes data using an engine called the Data Contract Serializer which converts your C# types to and from XML documents. This serialization framework is not specific to Dapr and is separately maintained by the .NET team within the .NET GitHub repository.
When sending or receiving primitives (like strings or ints), this serialization happens transparently and there’s no requisite preparation needed on your part. However, when working with complex types such as those you create, there are some important rules to take into consideration so this process works smoothly.
Serializable Types
There are several important considerations to keep in mind when using the Data Contract Serializer:
By default, all types, read/write properties (after construction) and fields marked as publicly visible are serialized
All types must either expose a public parameterless constructor or be decorated with the DataContractAttribute attribute
Init-only setters are only supported with the use of the DataContractAttribute attribute
Read-only fields, properties without a Get and Set method and internal or properties with private Get and Set methods are ignored during serialization
Serialization is supported for types that use other complex types that are not themselves marked with the DataContractAttribute attribute through the use of the KnownTypesAttribute attribute
If a type is marked with the DataContractAttribute attribute, all members you wish to serialize and deserialize must be decorated with the DataMemberAttribute attribute as well or they’ll be set to their default values
How does deserialization work?
The approach used for deserialization depends on whether or not the type is decorated with the DataContractAttribute attribute. If this attribute isn’t present, an instance of the type is created using the parameterless constructor. Each of the properties and fields are then mapped into the type using their respective setters and the instance is returned to the caller.
If the type is marked with [DataContract], the serializer instead uses reflection to read the metadata of the type and determine which properties or fields should be included based on whether or not they’re marked with the DataMemberAttribute attribute as it’s performed on an opt-in basis. It then allocates an uninitialized object in memory (avoiding the use of any constructors, parameterless or not) and then sets the value directly on each mapped property or field, even if private or uses init-only setters. Serialization callbacks are invoked as applicable throughout this process and then the object is returned to the caller.
Use of the serialization attributes is highly recommended as they grant more flexibility to override names and namespaces and generally use more of the modern C# functionality. While the default serializer can be relied on for primitive types, it’s not recommended for any of your own types, whether they be classes, structs or records. It’s recommended that if you decorate a type with the DataContractAttribute attribute, you also explicitly decorate each of the members you want to serialize or deserialize with the DataMemberAttribute attribute as well.
.NET Classes
Classes are fully supported in the Data Contract Serializer provided that that other rules detailed on this page and the Data Contract Serializer documentation are also followed.
The most important thing to remember here is that you must either have a public parameterless constructor or you must decorate it with the appropriate attributes. Let’s review some examples to really clarify what will and won’t work.
In the following example, we present a simple class named Doodad. We don’t provide an explicit constructor here, so the compiler will provide an default parameterless constructor. Because we’re using supported primitive types (Guid, string and int32) and all our members have a public getter and setter, no attributes are required and we’ll be able to use this class without issue when sending and receiving it from a Dapr actor method.
So let’s tweak it - let’s add our own constructor and only use init-only setters on the members. This will fail to serialize and deserialize not because of the use of the init-only setters, but because there’s no parameterless constructors.
// WILL NOT SERIALIZE PROPERLY!publicclassDoodad{publicDoodad(stringname,intcount){Id=Guid.NewGuid();Name=name;Count=count;}publicGuidId{get;set;}publicstringName{get;init;}publicintCount{get;init;}}
If we add a public parameterless constructor to the type, we’re good to go and this will work without further annotations.
But what if we don’t want to add this constructor? Perhaps you don’t want your developers to accidentally create an instance of this Doodad using an unintended constructor. That’s where the more flexible attributes are useful. If you decorate your type with a DataContractAttribute attribute, you can drop your parameterless constructor and it will work once again.
In the above example, we don’t need to also use the DataMemberAttribute attributes because again, we’re using built-in primitives that the serializer supports. But, we do get more flexibility if we use the attributes. From the DataContractAttribute attribute, we can specify our own XML namespace with the Namespace argument and, via the Name argument, change the name of the type as used when serialized into the XML document.
It’s a recommended practice to append the DataContractAttribute attribute to the type and the DataMemberAttribute attributes to all the members you want to serialize anyway - if they’re not necessary and you’re not changing the default values, they’ll just be ignored, but they give you a mechanism to opt into serializing members that wouldn’t otherwise have been included such as those marked as private or that are themselves complex types or collections.
Note that if you do opt into serializing your private members, their values will be serialized into plain text - they can very well be viewed, intercepted and potentially manipulated based on how you’re handing the data once serialized, so it’s an important consideration whether you want to mark these members or not in your use case.
In the following example, we’ll look at using the attributes to change the serialized names of some of the members as well as introduce the IgnoreDataMemberAttribute attribute. As the name indicates, this tells the serializer to skip this property even though it’d be otherwise eligible to serialize. Further, because I’m decorating the type with the DataContractAttribute attribute, it means that I can use init-only setters on the properties.
When this is serialized, because we’re changing the names of the serialized members, we can expect a new instance of Doodad using the default values this to be serialized as:
C# 12 brought us primary constructors on classes. Use of a primary constructor means the compiler will be prevented from creating the default implicit parameterless constructor. While a primary constructor on a class doesn’t generate any public properties, it does mean that if you pass this primary constructor any arguments or have non-primitive types in your class, you’ll either need to specify your own parameterless constructor or use the serialization attributes.
Here’s an example where we’re using the primary constructor to inject an ILogger to a field and add our own parameterless constructor without the need for any attributes.
Structs are supported by the Data Contract serializer provided that they are marked with the DataContractAttribute attribute and the members you wish to serialize are marked with the DataMemberAttribute attribute. Further, to support deserialization, the struct will also need to have a parameterless constructor. This works even if you define your own parameterless constructor as enabled in C# 10.
Records were introduced in C# 9 and follow precisely the same rules as classes when it comes to serialization. We recommend that you should decorate all your records with the DataContractAttribute attribute and members you wish to serialize with DataMemberAttribute attributes so you don’t experience any deserialization issues using this or other newer C# functionalities. Because record classes use init-only setters for properties by default and encourage the use of the primary constructor, applying these attributes to your types ensures that the serializer can properly otherwise accommodate your types as-is.
Typically records are presented as a simple one-line statement using the new primary constructor concept:
publicrecordDoodad(GuidId,stringName,intCount);
This will throw an error encouraging the use of the serialization attributes as soon as you use it in a Dapr actor method invocation because there’s no parameterless constructor available nor is it decorated with the aforementioned attributes.
Here we add an explicit parameterless constructor and it won’t throw an error, but none of the values will be set during deserialization since they’re created with init-only setters. Because this doesn’t use the DataContractAttribute attribute or the DataMemberAttribute attribute on any members, the serializer will be unable to map the target members correctly during deserialization.
This approach does without the additional constructor and instead relies on the serialization attributes. Because we mark the type with the DataContractAttribute attribute and decorate each member with its own DataMemberAttribute attribute, the serialization engine will be able to map from the XML document to our type without issue.
There are several types built into .NET that are considered primitive and eligible for serialization without additional effort on the part of the developer:
Again, if you want to pass these types around via your actor methods, no additional consideration is necessary as they’ll be serialized and deserialized without issue. Further, types that are themselves marked with the (SerializeableAttribute)[https://learn.microsoft.com/en-us/dotnet/api/system.serializableattribute] attribute will be serialized.
Enumeration Types
Enumerations, including flag enumerations are serializable if appropriately marked. The enum members you wish to be serialized must be marked with the EnumMemberAttribute attribute in order to be serialized. Passing a custom value into the optional Value argument on this attribute will allow you to specify the value used for the member in the serialized document instead of having the serializer derive it from the name of the member.
The enum type does not require that the type be decorated with the DataContractAttribute attribute - only that the members you wish to serialize be decorated with the EnumMemberAttribute attributes.
publicenumColors{ [EnumMember]Red, [EnumMember(Value="g")]Green,Blue,//Even if used by a type, this value will not be serialized as it's not decorated with the EnumMember attribute}
Collection Types
With regards to the data contact serializer, all collection types that implement the IEnumerable interface including arays and generic collections are considered collections. Those types that implement IDictionary or the generic IDictionary<TKey, TValue> are considered dictionary collections; all others are list collections.
Not unlike other complex types, collection types must have a parameterless constructor available. Further, they must also have a method called Add so they can be properly serialized and deserialized. The types used by these collection types must themselves be marked with the DataContractAttribute attribute or otherwise be serializable as described throughout this document.
Data Contract Versioning
As the data contract serializer is only used in Dapr with respect to serializing the values in the .NET SDK to and from the Dapr actor instances via the proxy methods, there’s little need to consider versioning of data contracts as the data isn’t being persisted between application versions using the same serializer. For those interested in learning more about data contract versioning visit here.
Known Types
Nesting your own complex types is easily accommodated by marking each of the types with the DataContractAttribute attribute. This informs the serializer as to how deserialization should be performed.
But what if you’re working with polymorphic types and one of your members is a base class or interface with derived classes or other implementations? Here, you’ll use the KnownTypeAttribute attribute to give a hint to the serializer about how to proceed.
When you apply the KnownTypeAttribute attribute to a type, you are informing the data contract serializer about what subtypes it might encounter allowing it to properly handle the serialization and deserialization of these types, even when the actual type at runtime is different from the declared type.
[DataContract]
[KnownType(typeof(DerivedClass))]
public class BaseClass
{
//Members of the base class
}
[DataContract]
public class DerivedClass : BaseClass
{
//Additional members of the derived class
}
In this example, the BaseClass is marked with [KnownType(typeof(DerivedClass))] which tells the data contract serializer that DerivedClass is a possible implementation of BaseClass that it may need to serialize or deserialize. Without this attribute, the serialize would not be aware of the DerivedClass when it encounters an instance of BaseClass that is actually of type DerivedClass and this could lead to a serialization exception because the serializer would not know how to handle the derived type. By specifying all possible derived types as known types, you ensure that the serializer can process the type and its members correctly.
For more information and examples about using [KnownType], please refer to the official documentation.
2.4 - How to: Run and use virtual actors in the .NET SDK
Try out .NET Dapr virtual actors with this example
The Dapr actor package allows you to interact with Dapr virtual actors from a .NET application. In this guide, you learn how to:
The interface project (\MyActor\MyActor.Interfaces)
This project contains the interface definition for the actor. Actor interfaces can be defined in any project with any name. The interface defines the actor contract shared by:
The actor implementation
The clients calling the actor
Because client projects may depend on it, it’s better to define it in an assembly separate from the actor implementation.
The actor service project (\MyActor\MyActorService)
This project implements the ASP.Net Core web service that hosts the actor. It contains the implementation of the actor, MyActor.cs. An actor implementation is a class that:
Derives from the base type Actor
Implements the interfaces defined in the MyActor.Interfaces project.
An actor class must also implement a constructor that accepts an ActorService instance and an ActorId, and passes them to the base Actor class.
The actor client project (\MyActor\MyActorClient)
This project contains the implementation of the actor client which calls MyActor’s method defined in Actor Interfaces.
Note that while .NET 6 is generally supported as the minimum .NET requirement across the Dapr .NET SDK packages
and .NET 7 is the minimally supported version of .NET by Dapr.Workflows in Dapr v1.15, only .NET 8 and .NET 9 will
continue to be supported by Dapr in v1.16 and later.
Step 0: Prepare
Since we’ll be creating 3 projects, choose an empty directory to start from, and open it in your terminal of choice.
Step 1: Create actor interfaces
Actor interface defines the actor contract that is shared by the actor implementation and the clients calling the actor.
Actor interface is defined with the below requirements:
Actor interface must inherit Dapr.Actors.IActor interface
The return type of Actor method must be Task or Task<object>
Actor method can have one argument at a maximum
Create interface project and add dependencies
# Create Actor Interfacesdotnet new classlib -o MyActor.Interfaces
cd MyActor.Interfaces
# Add Dapr.Actors nuget package. Please use the latest package version from nuget.orgdotnet add package Dapr.Actors
cd ..
Implement IMyActor interface
Define IMyActor interface and MyData data object. Paste the following code into MyActor.cs in the MyActor.Interfaces project.
Dapr uses ASP.NET web service to host Actor service. This section will implement IMyActor actor interface and register Actor to Dapr Runtime.
Create actor service project and add dependencies
# Create ASP.Net Web service to host Dapr actordotnet new web -o MyActorService
cd MyActorService
# Add Dapr.Actors.AspNetCore nuget package. Please use the latest package version from nuget.orgdotnet add package Dapr.Actors.AspNetCore
# Add Actor Interface referencedotnet add reference ../MyActor.Interfaces/MyActor.Interfaces.csproj
cd ..
Add actor implementation
Implement IMyActor interface and derive from Dapr.Actors.Actor class. Following example shows how to use Actor Reminders as well. For Actors to use Reminders, it must derive from IRemindable. If you don’t intend to use Reminder feature, you can skip implementing IRemindable and reminder specific methods which are shown in the code below.
Paste the following code into MyActor.cs in the MyActorService project:
usingDapr.Actors;usingDapr.Actors.Runtime;usingMyActor.Interfaces;usingSystem;usingSystem.Threading.Tasks;namespaceMyActorService{internalclassMyActor:Actor,IMyActor,IRemindable{// The constructor must accept ActorHost as a parameter, and can also accept additional// parameters that will be retrieved from the dependency injection container///// <summary>/// Initializes a new instance of MyActor/// </summary>/// <param name="host">The Dapr.Actors.Runtime.ActorHost that will host this actor instance.</param>publicMyActor(ActorHosthost):base(host){}/// <summary>/// This method is called whenever an actor is activated./// An actor is activated the first time any of its methods are invoked./// </summary>protectedoverrideTaskOnActivateAsync(){// Provides opportunity to perform some optional setup.Console.WriteLine($"Activating actor id: {this.Id}");returnTask.CompletedTask;}/// <summary>/// This method is called whenever an actor is deactivated after a period of inactivity./// </summary>protectedoverrideTaskOnDeactivateAsync(){// Provides Opporunity to perform optional cleanup.Console.WriteLine($"Deactivating actor id: {this.Id}");returnTask.CompletedTask;}/// <summary>/// Set MyData into actor's private state store/// </summary>/// <param name="data">the user-defined MyData which will be stored into state store as "my_data" state</param>publicasyncTask<string>SetDataAsync(MyDatadata){// Data is saved to configured state store implicitly after each method execution by Actor's runtime.// Data can also be saved explicitly by calling this.StateManager.SaveStateAsync();// State to be saved must be DataContract serializable.awaitthis.StateManager.SetStateAsync<MyData>("my_data",// state namedata);// data saved for the named state "my_data"return"Success";}/// <summary>/// Get MyData from actor's private state store/// </summary>/// <return>the user-defined MyData which is stored into state store as "my_data" state</return>publicTask<MyData>GetDataAsync(){// Gets state from the state store.returnthis.StateManager.GetStateAsync<MyData>("my_data");}/// <summary>/// Register MyReminder reminder with the actor/// </summary>publicasyncTaskRegisterReminder(){awaitthis.RegisterReminderAsync("MyReminder",// The name of the remindernull,// User state passed to IRemindable.ReceiveReminderAsync()TimeSpan.FromSeconds(5),// Time to delay before invoking the reminder for the first timeTimeSpan.FromSeconds(5));// Time interval between reminder invocations after the first invocation}/// <summary>/// Get MyReminder reminder details with the actor/// </summary>publicasyncTask<IActorReminder>GetReminder(){awaitthis.GetReminderAsync("MyReminder");}/// <summary>/// Unregister MyReminder reminder with the actor/// </summary>publicTaskUnregisterReminder(){Console.WriteLine("Unregistering MyReminder...");returnthis.UnregisterReminderAsync("MyReminder");}// <summary>// Implement IRemindeable.ReceiveReminderAsync() which is call back invoked when an actor reminder is triggered.// </summary>publicTaskReceiveReminderAsync(stringreminderName,byte[]state,TimeSpandueTime,TimeSpanperiod){Console.WriteLine("ReceiveReminderAsync is called!");returnTask.CompletedTask;}/// <summary>/// Register MyTimer timer with the actor/// </summary>publicTaskRegisterTimer(){returnthis.RegisterTimerAsync("MyTimer",// The name of the timernameof(this.OnTimerCallBack),// Timer callbacknull,// User state passed to OnTimerCallback()TimeSpan.FromSeconds(5),// Time to delay before the async callback is first invokedTimeSpan.FromSeconds(5));// Time interval between invocations of the async callback}/// <summary>/// Unregister MyTimer timer with the actor/// </summary>publicTaskUnregisterTimer(){Console.WriteLine("Unregistering MyTimer...");returnthis.UnregisterTimerAsync("MyTimer");}/// <summary>/// Timer callback once timer is expired/// </summary>privateTaskOnTimerCallBack(byte[]data){Console.WriteLine("OnTimerCallBack is called!");returnTask.CompletedTask;}}}
Register actor runtime with ASP.NET Core startup
The Actor runtime is configured through ASP.NET Core Startup.cs.
The runtime uses the ASP.NET Core dependency injection system to register actor types and essential services. This integration is provided through the AddActors(...) method call in ConfigureServices(...). Use the delegate passed to AddActors(...) to register actor types and configure actor runtime settings. You can register additional types for dependency injection inside ConfigureServices(...). These will be available to be injected into the constructors of your Actor types.
Actors are implemented via HTTP calls with the Dapr runtime. This functionality is part of the application’s HTTP processing pipeline and is registered inside UseEndpoints(...) inside Configure(...).
Paste the following code into Startup.cs in the MyActorService project:
usingMicrosoft.AspNetCore.Builder;usingMicrosoft.AspNetCore.Hosting;usingMicrosoft.Extensions.DependencyInjection;usingMicrosoft.Extensions.Hosting;namespaceMyActorService{publicclassStartup{publicvoidConfigureServices(IServiceCollectionservices){services.AddActors(options=>{// Register actor types and configure actor settingsoptions.Actors.RegisterActor<MyActor>();});}publicvoidConfigure(IApplicationBuilderapp,IWebHostEnvironmentenv){if(env.IsDevelopment()){app.UseDeveloperExceptionPage();}app.UseRouting();// Register actors handlers that interface with the Dapr runtime.app.MapActorsHandlers();}}}
Step 3: Add a client
Create a simple console app to call the actor service. Dapr SDK provides Actor Proxy client to invoke actor methods defined in Actor Interface.
Create actor client project and add dependencies
# Create Actor's Clientdotnet new console -o MyActorClient
cd MyActorClient
# Add Dapr.Actors nuget package. Please use the latest package version from nuget.orgdotnet add package Dapr.Actors
# Add Actor Interface referencedotnet add reference ../MyActor.Interfaces/MyActor.Interfaces.csproj
cd ..
Invoke actor methods with strongly-typed client
You can use ActorProxy.Create<IMyActor>(..) to create a strongly-typed client and invoke methods on the actor.
Paste the following code into Program.cs in the MyActorClient project:
usingSystem;usingSystem.Threading.Tasks;usingDapr.Actors;usingDapr.Actors.Client;usingMyActor.Interfaces;namespaceMyActorClient{classProgram{staticasyncTaskMainAsync(string[]args){Console.WriteLine("Startup up...");// Registered Actor Type in Actor ServicevaractorType="MyActor";// An ActorId uniquely identifies an actor instance// If the actor matching this id does not exist, it will be createdvaractorId=newActorId("1");// Create the local proxy by using the same interface that the service implements.//// You need to provide the type and id so the actor can be located. varproxy=ActorProxy.Create<IMyActor>(actorId,actorType);// Now you can use the actor interface to call the actor's methods.Console.WriteLine($"Calling SetDataAsync on {actorType}:{actorId}...");varresponse=awaitproxy.SetDataAsync(newMyData(){PropertyA="ValueA",PropertyB="ValueB",});Console.WriteLine($"Got response: {response}");Console.WriteLine($"Calling GetDataAsync on {actorType}:{actorId}...");varsavedData=awaitproxy.GetDataAsync();Console.WriteLine($"Got response: {savedData}");}}}
Running the code
The projects that you’ve created can now to test the sample.
Run MyActorService
Since MyActorService is hosting actors, it needs to be run with the Dapr CLI.
cd MyActorService
dapr run --app-id myapp --app-port 5000 --dapr-http-port 3500 -- dotnet run
You will see commandline output from both daprd and MyActorService in this terminal. You should see something like the following, which indicates that the application started successfully.
...
ℹ️ Updating metadata for app command: dotnet run
✅ You're up and running! Both Dapr and your app logs will appear here.
== APP == info: Microsoft.Hosting.Lifetime[0]
== APP == Now listening on: https://localhost:5001
== APP == info: Microsoft.Hosting.Lifetime[0]
== APP == Now listening on: http://localhost:5000
== APP == info: Microsoft.Hosting.Lifetime[0]
== APP == Application started. Press Ctrl+C to shut down.
== APP == info: Microsoft.Hosting.Lifetime[0]
== APP == Hosting environment: Development
== APP == info: Microsoft.Hosting.Lifetime[0]
== APP == Content root path: /Users/ryan/actortest/MyActorService
Run MyActorClient
MyActorClient is acting as the client, and it can be run normally with dotnet run.
Open a new terminal an navigate to the MyActorClient directory. Then run the project with:
dotnet run
You should see commandline output like:
Startup up...
Calling SetDataAsync on MyActor:1...
Got response: Success
Calling GetDataAsync on MyActor:1...
Got response: PropertyA: ValueA, PropertyB: ValueB
💡 This sample relies on a few assumptions. The default listening port for an ASP.NET Core web project is 5000, which is being passed to dapr run as --app-port 5000. The default HTTP port for the Dapr sidecar is 3500. We’re telling the sidecar for MyActorService to use 3500 so that MyActorClient can rely on the default value.
Now you have successfully created an actor service and client. See the related links section to learn more.
Get up and running with Dapr Workflow and the Dapr .NET SDK
3.1 - DaprWorkflowClient usage
Essential tips and advice for using DaprWorkflowClient
Lifetime management
A DaprWorkflowClient holds access to networking resources in the form of TCP sockets used to communicate with the Dapr sidecar as well
as other types used in the management and operation of Workflows. DaprWorkflowClient implements IAsyncDisposable to support eager
cleanup of resources.
Dependency Injection
The AddDaprWorkflow() method will register the Dapr workflow services with ASP.NET Core dependency injection. This method
requires an options delegate that defines each of the workflows and activities you wish to register and use in your application.
Note
This method will attempt to register a DaprClient instance, but this will only work if it hasn’t already been registered with another
lifetime. For example, an earlier call to AddDaprClient() with a singleton lifetime will always use a singleton regardless of the
lifetime chose for the workflow client. The DaprClient instance will be used to communicate with the Dapr sidecar and if it’s not
yet registered, the lifetime provided during the AddDaprWorkflow() registration will be used to register the DaprWorkflowClient
as well as its own dependencies.
Singleton Registration
By default, the AddDaprWorkflow method will register the DaprWorkflowClient and associated services using a singleton lifetime. This means
that the services will be instantiated only a single time.
The following is an example of how registration of the DaprWorkflowClient as it would appear in a typical Program.cs file:
While this may generally be acceptable in your use case, you may instead wish to override the lifetime specified. This is done by passing a ServiceLifetime
argument in AddDaprWorkflow. For example, you may wish to inject another scoped service into your ASP.NET Core processing pipeline
that needs context used by the DaprClient that wouldn’t be available if the former service were registered as a singleton.
Finally, Dapr services can also be registered using a transient lifetime meaning that they will be initialized every time they’re injected. This
is demonstrated in the following example:
Workflow activities support the same dependency injection that developers have come to expect of modern C# applications. Assuming a proper
registration at startup, any such type can be injected into the constructor of the workflow activity and available to utilize during
the execution of the workflow. This makes it simple to add logging via an injected ILogger or access to other Dapr
building blocks by injecting DaprClient or DaprJobsClient, for example.
internalsealedclassSquareNumberActivity:WorkflowActivity<int,int>{privatereadonlyILogger_logger;publicMyActivity(ILoggerlogger){this._logger=logger;}publicoverrideTask<int>RunAsync(WorkflowActivityContextcontext,intinput){this._logger.LogInformation("Squaring the value {number}",input);varresult=input*input;this._logger.LogInformation("Got a result of {squareResult}",result);returnTask.FromResult(result);}}
Using ILogger in Workflow
Because workflows must be deterministic, it is not possible to inject arbitrary services into them. For example,
if you were able to inject a standard ILogger into a workflow and it needed to be replayed because of an error,
subsequent replay from the event source log would result in the log recording additional operations that didn’t actually
take place a second or third time because their results were sourced from the log. This has the potential to introduce
a significant amount of confusion. Rather, a replay-safe logger is made available for use within workflows. It will only
log events the first time the workflow runs and will not log anything whenever the workflow is being replaced.
This logger can be retrieved from a method present on the WorkflowContext available on your workflow instance and
otherwise used precisely as you might otherwise use an ILogger instance.
An end-to-end sample demonstrating this can be seen in the
.NET SDK repository
but a brief extraction of this sample is available below.
publicclassOrderProcessingWorkflow:Workflow<OrderPayload,OrderResult>{publicoverrideasyncTask<OrderResult>RunAsync(WorkflowContextcontext,OrderPayloadorder){stringorderId=context.InstanceId;varlogger=context.CreateReplaySafeLogger<OrderProcessingWorkflow>();//Use this method to access the logger instancelogger.LogInformation("Received order {orderId} for {quantity} {name} at ${totalCost}",orderId,order.Quantity,order.Name,order.TotalCost);//...}}
3.2 - How to: Author and manage Dapr Workflow in the .NET SDK
Learn how to author and manage Dapr Workflow using the .NET SDK
Let’s create a Dapr workflow and invoke it using the console. In the provided order processing workflow example, the console prompts provide directions on how to both purchase and restock items. In this guide, you will:
If successful, you should see a response like the following:
{"instanceID":"12345678"}
Send an HTTP request to get the status of the workflow that was started:
curl -i -X GET http://localhost:3500/v1.0/workflows/dapr/12345678
The workflow is designed to take several seconds to complete. If the workflow hasn’t completed when you issue the HTTP request, you’ll see the following JSON response (formatted for readability) with workflow status as RUNNING:
When the workflow has completed, the stdout of the workflow app should look like:
info: WorkflowConsoleApp.Activities.NotifyActivity[0]
Received order 12345678 for Paperclips at $99.95
info: WorkflowConsoleApp.Activities.ReserveInventoryActivity[0]
Reserving inventory: 12345678, Paperclips, 1
info: WorkflowConsoleApp.Activities.ProcessPaymentActivity[0]
Processing payment: 12345678, 99.95, USD
info: WorkflowConsoleApp.Activities.NotifyActivity[0]
Order 12345678 processed successfully!
If you have Zipkin configured for Dapr locally on your machine, then you can view the workflow trace spans in the Zipkin web UI (typically at http://localhost:9411/zipkin/).
With the Dapr AI package, you can interact with the Dapr AI workloads from a .NET application.
Today, Dapr provides the Conversational API to engage with large language models. To get started with this workload,
walk through the Dapr Conversational AI how-to guide.
4.1 - Dapr AI Client
Learn how to create Dapr AI clients
The Dapr AI client package allows you to interact with the AI capabilities provided by the Dapr sidecar.
Lifetime management
A DaprConversationClient is a version of the Dapr client that is dedicated to interacting with the Dapr Conversation
API. It can be registered alongside a DaprClient and other Dapr clients without issue.
It maintains access to networking resources in the form of TCP sockets used to communicate with the Dapr sidecar.
For best performance, create a single long-lived instance of DaprConversationClient and provide access to that shared
instance throughout your application. DaprConversationClient instances are thread-safe and intended to be shared.
This can be aided by utilizing the dependency injection functionality. The registration method supports registration using
as a singleton, a scoped instance or as transient (meaning it’s recreated every time it’s injected), but also enables
registration to utilize values from an IConfiguration or other injected service in a way that’s impractical when
creating the client from scratch in each of your classes.
Avoid creating a DaprConversationClient for each operation.
Configuring DaprConversationClient via DaprConversationClientBuilder
A DaprConversationClient can be configured by invoking methods on the DaprConversationClientBuilder class before
calling .Build() to create the client itself. The settings for each DaprConversationClient are separate
and cannot be changed after calling .Build().
vardaprConversationClient=newDaprConversationClientBuilder().UseDaprApiToken("abc123")// Specify the API token used to authenticate to other Dapr sidecars.Build();
The DaprConversationClientBuilder contains settings for:
The HTTP endpoint of the Dapr sidecar
The gRPC endpoint of the Dapr sidecar
The JsonSerializerOptions object used to configure JSON serialization
The GrpcChannelOptions object used to configure gRPC
The API token used to authenticate requests to the sidecar
The factory method used to create the HttpClient instance used by the SDK
The timeout used for the HttpClient instance when making requests to the sidecar
The SDK will read the following environment variables to configure the default values:
DAPR_HTTP_ENDPOINT: used to find the HTTP endpoint of the Dapr sidecar, example: https://dapr-api.mycompany.com
DAPR_GRPC_ENDPOINT: used to find the gRPC endpoint of the Dapr sidecar, example: https://dapr-grpc-api.mycompany.com
DAPR_HTTP_PORT: if DAPR_HTTP_ENDPOINT is not set, this is used to find the HTTP local endpoint of the Dapr sidecar
DAPR_GRPC_PORT: if DAPR_GRPC_ENDPOINT is not set, this is used to find the gRPC local endpoint of the Dapr sidecar
DAPR_API_TOKEN: used to set the API token
Configuring gRPC channel options
Dapr’s use of CancellationToken for cancellation relies on the configuration of the gRPC channel options. If you need
to configure these options yourself, make sure to enable the ThrowOperationCanceledOnCancellation setting.
The APIs on DaprConversationClient perform asynchronous operations and accept an optional CancellationToken parameter. This
follows a standard .NET practice for cancellable operations. Note that when cancellation occurs, there is no guarantee that
the remote endpoint stops processing the request, only that the client has stopped waiting for completion.
When an operation is cancelled, it will throw an OperationCancelledException.
Configuring DaprConversationClient via dependency injection
Using the built-in extension methods for registering the DaprConversationClient in a dependency injection container can
provide the benefit of registering the long-lived service a single time, centralize complex configuration and improve
performance by ensuring similarly long-lived resources are re-purposed when possible (e.g. HttpClient instances).
There are three overloads available to give the developer the greatest flexibility in configuring the client for their
scenario. Each of these will register the IHttpClientFactory on your behalf if not already registered, and configure
the DaprConversationClientBuilder to use it when creating the HttpClient instance in order to re-use the same instance as
much as possible and avoid socket exhaustion and other issues.
In the first approach, there’s no configuration done by the developer and the DaprConversationClient is configured with the
default settings.
varbuilder=WebApplication.CreateBuilder(args);builder.Services.AddDaprConversationClient();//Registers the `DaprConversationClient` to be injected as neededvarapp=builder.Build();
Sometimes the developer will need to configure the created client using the various configuration options detailed
above. This is done through an overload that passes in the DaprConversationClientBuiler and exposes methods for configuring
the necessary options.
varbuilder=WebApplication.CreateBuilder(args);builder.Services.AddDaprConversationClient((_,daprConversationClientBuilder)=>{//Set the API tokendaprConversationClientBuilder.UseDaprApiToken("abc123");//Specify a non-standard HTTP endpointdaprConversationClientBuilder.UseHttpEndpoint("http://dapr.my-company.com");});varapp=builder.Build();
Finally, it’s possible that the developer may need to retrieve information from another service in order to populate
these configuration values. That value may be provided from a DaprClient instance, a vendor-specific SDK or some
local service, but as long as it’s also registered in DI, it can be injected into this configuration operation via the
last overload:
varbuilder=WebApplication.CreateBuilder(args);//Register a fictional service that retrieves secrets from somewherebuilder.Services.AddSingleton<SecretService>();builder.Services.AddDaprConversationClient((serviceProvider,daprConversationClientBuilder)=>{//Retrieve an instance of the `SecretService` from the service providervarsecretService=serviceProvider.GetRequiredService<SecretService>();vardaprApiToken=secretService.GetSecret("DaprApiToken").Value;//Configure the `DaprConversationClientBuilder`daprConversationClientBuilder.UseDaprApiToken(daprApiToken);});varapp=builder.Build();
4.2 - How to: Create and use Dapr AI Conversations in the .NET SDK
Learn how to create and use the Dapr Conversational AI client using the .NET SDK
.NET 6 is supported as the minimum required for the Dapr .NET SDK packages in this release. Only .NET 8 and .NET 9
will be supported in Dapr v1.16 and later releases.
Installation
To get started with the Dapr AI .NET SDK client, install the Dapr.AI package from NuGet:
dotnet add package Dapr.AI
A DaprConversationClient maintains access to networking resources in the form of TCP sockets used to communicate with the Dapr sidecar.
Dependency Injection
The AddDaprAiConversation() method will register the Dapr client ASP.NET Core dependency injection and is the recommended approach
for using this package. This method accepts an optional options delegate for configuring the DaprConversationClient and a
ServiceLifetime argument, allowing you to specify a different lifetime for the registered services instead of the default Singleton
value.
The following example assumes all default values are acceptable and is sufficient to register the DaprConversationClient:
services.AddDaprAiConversation();
The optional configuration delegate is used to configure the DaprConversationClient by specifying options on the
DaprConversationClientBuilder as in the following example:
services.AddSingleton<DefaultOptionsProvider>();services.AddDaprAiConversation((serviceProvider,clientBuilder)=>{//Inject a service to source a value fromvaroptionsProvider=serviceProvider.GetRequiredService<DefaultOptionsProvider>();varstandardTimeout=optionsProvider.GetStandardTimeout();//Configure the value on the client builderclientBuilder.UseTimeout(standardTimeout);});
Manual Instantiation
Rather than using dependency injection, a DaprConversationClient can also be built using the static client builder.
For best performance, create a single long-lived instance of DaprConversationClient and provide access to that shared instance throughout
your application. DaprConversationClient instances are thread-safe and intended to be shared.
Avoid creating a DaprConversationClient per-operation.
A DaprConversationClient can be configured by invoking methods on the DaprConversationClientBuilder class before calling .Build()
to create the client. The settings for each DaprConversationClient are separate and cannot be changed after calling .Build().
Clone the SDK repo to try out some examples and get started.
Building Blocks
This part of the .NET SDK allows you to interface with the Conversations API to send and receive messages from
large language models.
Send messages
5 - Dapr Jobs .NET SDK
Get up and running with Dapr Jobs and the Dapr .NET SDK
With the Dapr Job package, you can interact with the Dapr Job APIs from a .NET application to trigger future operations
to run according to a predefined schedule with an optional payload.
5.1 - How to: Author and manage Dapr Jobs in the .NET SDK
Learn how to author and manage Dapr Jobs using the .NET SDK
Let’s create an endpoint that will be invoked by Dapr Jobs when it triggers, then schedule the job in the same app. We’ll use the simple example provided here, for the following demonstration and walk through it as an explainer of how you can schedule one-time or recurring jobs using either an interval or Cron expression yourself. In this guide,
you will:
Note that while .NET 6 is the minimum support version of .NET in Dapr v1.15, only .NET 8 and .NET 9 will continue to be supported by Dapr in v1.16 and later.
From the .NET SDK root directory, navigate to the Dapr Jobs example.
cd examples/Jobs
Run the application locally
To run the Dapr application, you need to start the .NET program and a Dapr sidecar. Navigate to the JobsSample directory.
cd JobsSample
We’ll run a command that starts both the Dapr sidecar and the .NET program at the same time.
dapr run --app-id jobsapp --dapr-grpc-port 4001 --dapr-http-port 3500 -- dotnet run
Dapr listens for HTTP requests at http://localhost:3500 and internal Jobs gRPC requests at http://localhost:4001.
Register the Dapr Jobs client with dependency injection
The Dapr Jobs SDK provides an extension method to simplify the registration of the Dapr Jobs client. Before completing
the dependency injection registration in Program.cs, add the following line:
varbuilder=WebApplication.CreateBuilder(args);//Add anywhere between these two linesbuilder.Services.AddDaprJobsClient();varapp=builder.Build();
Note that in today’s implementation of the Jobs API, the app that schedules the job will also be the app that receives the trigger notification. In other words, you cannot schedule a trigger to run in another application. As a result, while you don’t explicitly need the Dapr Jobs client to be registered in your application to schedule a trigger invocation endpoint, your endpoint will never be invoked without the same app also scheduling the job somehow (whether via this Dapr Jobs .NET SDK or an HTTP call to the sidecar).
It’s possible that you may want to provide some configuration options to the Dapr Jobs client that
should be present with each call to the sidecar such as a Dapr API token, or you want to use a non-standard
HTTP or gRPC endpoint. This is possible through use of an overload of the registration method that allows configuration of a
DaprJobsClientBuilder instance:
Still, it’s possible that whatever values you wish to inject need to be retrieved from some other source, itself registered as a dependency. There’s one more overload you can use to inject an IServiceProvider into the configuration action method. In the following example, we register a fictional singleton that can retrieve secrets from somewhere and pass it into the configuration method for AddDaprJobClient so
we can retrieve our Dapr API token from somewhere else for registration here:
It’s possible to configure the Dapr Jobs client using the values in your registered IConfiguration as well without
explicitly specifying each of the value overrides using the DaprJobsClientBuilder as demonstrated in the previous
section. Rather, by populating an IConfiguration made available through dependency injection the AddDaprJobsClient()
registration will automatically use these values over their respective defaults.
Start by populating the values in your configuration. This can be done in several different ways as demonstrated below.
Configuration via ConfigurationBuilder
Application settings can be configured without using a configuration source and by instead populating the value in-memory
using a ConfigurationBuilder instance:
varbuilder=WebApplication.CreateBuilder();//Create the configurationvarconfiguration=newConfigurationBuilder().AddInMemoryCollection(newDictionary<string,string>{{"DAPR_HTTP_ENDPOINT","http://localhost:54321"},{"DAPR_API_TOKEN","abc123"}}).Build();builder.Configuration.AddConfiguration(configuration);builder.Services.AddDaprJobsClient();//This will automatically populate the HTTP endpoint and API token values from the IConfiguration
Configuration via Environment Variables
Application settings can be accessed from environment variables available to your application.
The following environment variables will be used to populate both the HTTP endpoint and API token used to register the
Dapr Jobs client.
The Dapr Jobs client will be configured to use both the HTTP endpoint http://localhost:54321 and populate all outbound
requests with the API token header abc123.
Configuration via prefixed Environment Variables
However, in shared-host scenarios where there are multiple applications all running on the same machine without using
containers or in development environments, it’s not uncommon to prefix environment variables. The following example
assumes that both the HTTP endpoint and the API token will be pulled from environment variables prefixed with the
value “myapp_”. The two environment variables used in this scenario are as follows:
Key
Value
myapp_DAPR_HTTP_ENDPOINT
http://localhost:54321
myapp_DAPR_API_TOKEN
abc123
These environment variables will be loaded into the registered configuration in the following example and made available
without the prefix attached.
The Dapr Jobs client will be configured to use both the HTTP endpoint http://localhost:54321 and populate all outbound
requests with the API token header abc123.
Use the Dapr Jobs client without relying on dependency injection
While the use of dependency injection simplifies the use of complex types in .NET and makes it easier to
deal with complicated configurations, you’re not required to register the DaprJobsClient in this way. Rather, you can also elect to create an instance of it from a DaprJobsClientBuilder instance as demonstrated below:
publicclassMySampleClass{publicvoidDoSomething(){vardaprJobsClientBuilder=newDaprJobsClientBuilder();vardaprJobsClient=daprJobsClientBuilder.Build();//Do something with the `daprJobsClient`}}
Set up a endpoint to be invoked when the job is triggered
It’s easy to set up a jobs endpoint if you’re at all familiar with minimal APIs in ASP.NET Core as the syntax is the same between the two.
Once dependency injection registration has been completed, configure the application the same way you would to handle mapping an HTTP request via the minimal API functionality in ASP.NET Core. Implemented as an extension method,
pass the name of the job it should be responsive to and a delegate. Services can be injected into the delegate’s arguments as you wish and the job payload can be accessed from the ReadOnlyMemory<byte> originally provided to the
job registration.
There are two delegates you can use here. One provides an IServiceProvider in case you need to inject other services into the handler:
//We have this from the example abovevarbuilder=WebApplication.CreateBuilder(args);builder.Services.AddDaprJobsClient();varapp=builder.Build();//Add our endpoint registrationapp.MapDaprScheduledJob("myJob",(IServiceProviderserviceProvider,stringjobName,ReadOnlyMemory<byte>jobPayload)=>{varlogger=serviceProvider.GetService<ILogger>();logger?.LogInformation("Received trigger invocation for '{jobName}'","myJob");//Do something...});app.Run();
The other overload of the delegate doesn’t require an IServiceProvider if not necessary:
//We have this from the example abovevarbuilder=WebApplication.CreateBuilder(args);builder.Services.AddDaprJobsClient();varapp=builder.Build();//Add our endpoint registrationapp.MapDaprScheduledJob("myJob",(stringjobName,ReadOnlyMemory<byte>jobPayload)=>{//Do something...});app.Run();
Support cancellation tokens when processing mapped invocations
You may want to ensure that timeouts are handled on job invocations so that they don’t indefinitely hang and use system resources. When setting up the job mapping, there’s an optional TimeSpan parameter that can be
provided as the last argument to specify a timeout for the request. Every time the job mapping invocation is triggered, a new CancellationTokenSource will be created using this timeout parameter and a CancellationToken
will be created from it to put an upper bound on the processing of the request. If a timeout isn’t provided, this defaults to CancellationToken.None and a timeout will not be automatically applied to the mapping.
//We have this from the example abovevarbuilder=WebApplication.CreateBuilder(args);builder.Services.AddDaprJobsClient();varapp=builder.Build();//Add our endpoint registrationapp.MapDaprScheduledJob("myJob",(stringjobName,ReadOnlyMemory<byte>jobPayload)=>{//Do something...},TimeSpan.FromSeconds(15));//Assigns a maximum timeout of 15 seconds for handling the invocation requestapp.Run();
Register the job
Finally, we have to register the job we want scheduled. Note that from here, all SDK methods have cancellation token support and use a default token if not otherwise set.
There are three different ways to set up a job that vary based on how you want to configure the schedule:
One-time job
A one-time job is exactly that; it will run at a single point in time and will not repeat. This approach requires that you select a job name and specify a time it should be triggered.
Argument Name
Type
Description
Required
jobName
string
The name of the job being scheduled.
Yes
scheduledTime
DateTime
The point in time when the job should be run.
Yes
payload
ReadOnlyMemory
Job data provided to the invocation endpoint when triggered.
No
cancellationToken
CancellationToken
Used to cancel out of the operation early, e.g. because of an operation timeout.
No
One-time jobs can be scheduled from the Dapr Jobs client as in the following example:
An interval-based job is one that runs on a recurring loop configured as a fixed amount of time, not unlike how reminders work in the Actors building block today. These jobs can be scheduled with a number of optional arguments as well:
Argument Name
Type
Description
Required
jobName
string
The name of the job being scheduled.
Yes
interval
TimeSpan
The interval at which the job should be triggered.
Yes
startingFrom
DateTime
The point in time from which the job schedule should start.
No
repeats
int
The maximum number of times the job should be triggered.
No
ttl
When the job should expires and no longer trigger.
No
payload
ReadOnlyMemory
Job data provided to the invocation endpoint when triggered.
No
cancellationToken
CancellationToken
Used to cancel out of the operation early, e.g. because of an operation timeout.
No
Interval-based jobs can be scheduled from the Dapr Jobs client as in the following example:
publicclassMyOperation(DaprJobsClientdaprJobsClient){publicasyncTaskScheduleIntervalJobAsync(CancellationTokencancellationToken){varhourlyInterval=TimeSpan.FromHours(1);//Trigger the job hourly, but a maximum of 5 timesawaitdaprJobsClient.ScheduleIntervalJobAsync("myJobName",hourlyInterval,repeats:5),cancellationToken:cancellationToken;}}
Cron-based job
A Cron-based job is scheduled using a Cron expression. This gives more calendar-based control over when the job is triggered as it can used calendar-based values in the expression. Like the other options, these jobs can be scheduled with a number of optional arguments as well:
Argument Name
Type
Description
Required
jobName
string
The name of the job being scheduled.
Yes
cronExpression
string
The systemd Cron-like expression indicating when the job should be triggered.
Yes
startingFrom
DateTime
The point in time from which the job schedule should start.
No
repeats
int
The maximum number of times the job should be triggered.
No
ttl
When the job should expires and no longer trigger.
No
payload
ReadOnlyMemory
Job data provided to the invocation endpoint when triggered.
No
cancellationToken
CancellationToken
Used to cancel out of the operation early, e.g. because of an operation timeout.
No
A Cron-based job can be scheduled from the Dapr Jobs client as follows:
publicclassMyOperation(DaprJobsClientdaprJobsClient){publicasyncTaskScheduleCronJobAsync(CancellationTokencancellationToken){//At the top of every other hour on the fifth day of the monthconststringcronSchedule="0 */2 5 * *";//Don't start this until next monthvarnow=DateTime.UtcNow;varoneMonthFromNow=now.AddMonths(1);varfirstOfNextMonth=newDateTime(oneMonthFromNow.Year,oneMonthFromNow.Month,1,0,0,0);//Trigger the job hourly, but a maximum of 5 timesawaitdaprJobsClient.ScheduleCronJobAsync("myJobName",cronSchedule,dueTime:firstOfNextMonth,cancellationToken:cancellationToken);}}
Get details of already-scheduled job
If you know the name of an already-scheduled job, you can retrieve its metadata without waiting for it to
be triggered. The returned JobDetails exposes a few helpful properties for consuming the information from the Dapr Jobs API:
If the Schedule property contains a Cron expression, the IsCronExpression property will be true and the expression will also be available in the CronExpression property.
If the Schedule property contains a duration value, the IsIntervalExpression property will instead be true and the value will be converted to a TimeSpan value accessible from the Interval property.
Essential tips and advice for using DaprJobsClient
Lifetime management
A DaprJobsClient is a version of the Dapr client that is dedicated to interacting with the Dapr Jobs API. It can be
registered alongside a DaprClient and other Dapr clients without issue.
It maintains access to networking resources in the form of TCP sockets used to communicate with the Dapr sidecar and
implements IDisposable to support the eager cleanup of resources.
For best performance, create a single long-lived instance of DaprJobsClient and provide access to that shared instance
throughout your application. DaprJobsClient instances are thread-safe and intended to be shared.
This can be aided by utilizing the dependency injection functionality. The registration method supports registration using
as a singleton, a scoped instance or as transient (meaning it’s recreated every time it’s injected), but also enables
registration to utilize values from an IConfiguration or other injected service in a way that’s impractical when
creating the client from scratch in each of your classes.
Avoid creating a DaprJobsClient for each operation and disposing it when the operation is complete.
Configuring DaprJobsClient via the DaprJobsClientBuilder
A DaprJobsClient can be configured by invoking methods on the DaprJobsClientBuilder class before calling .Build()
to create the client itself. The settings for each DaprJobsClient are separate
and cannot be changed after calling .Build().
vardaprJobsClient=newDaprJobsClientBuilder().UseDaprApiToken("abc123")// Specify the API token used to authenticate to other Dapr sidecars.Build();
The DaprJobsClientBuilder contains settings for:
The HTTP endpoint of the Dapr sidecar
The gRPC endpoint of the Dapr sidecar
The JsonSerializerOptions object used to configure JSON serialization
The GrpcChannelOptions object used to configure gRPC
The API token used to authenticate requests to the sidecar
The factory method used to create the HttpClient instance used by the SDK
The timeout used for the HttpClient instance when making requests to the sidecar
The SDK will read the following environment variables to configure the default values:
DAPR_HTTP_ENDPOINT: used to find the HTTP endpoint of the Dapr sidecar, example: https://dapr-api.mycompany.com
DAPR_GRPC_ENDPOINT: used to find the gRPC endpoint of the Dapr sidecar, example: https://dapr-grpc-api.mycompany.com
DAPR_HTTP_PORT: if DAPR_HTTP_ENDPOINT is not set, this is used to find the HTTP local endpoint of the Dapr sidecar
DAPR_GRPC_PORT: if DAPR_GRPC_ENDPOINT is not set, this is used to find the gRPC local endpoint of the Dapr sidecar
DAPR_API_TOKEN: used to set the API token
Configuring gRPC channel options
Dapr’s use of CancellationToken for cancellation relies on the configuration of the gRPC channel options. If you need
to configure these options yourself, make sure to enable the ThrowOperationCanceledOnCancellation setting.
The APIs on DaprJobsClient perform asynchronous operations and accept an optional CancellationToken parameter. This
follows a standard .NET practice for cancellable operations. Note that when cancellation occurs, there is no guarantee that
the remote endpoint stops processing the request, only that the client has stopped waiting for completion.
When an operation is cancelled, it will throw an OperationCancelledException.
Configuring DaprJobsClient via dependency injection
Using the built-in extension methods for registering the DaprJobsClient in a dependency injection container can
provide the benefit of registering the long-lived service a single time, centralize complex configuration and improve
performance by ensuring similarly long-lived resources are re-purposed when possible (e.g. HttpClient instances).
There are three overloads available to give the developer the greatest flexibility in configuring the client for their
scenario. Each of these will register the IHttpClientFactory on your behalf if not already registered, and configure
the DaprJobsClientBuilder to use it when creating the HttpClient instance in order to re-use the same instance as
much as possible and avoid socket exhaustion and other issues.
In the first approach, there’s no configuration done by the developer and the DaprJobsClient is configured with the
default settings.
varbuilder=WebApplication.CreateBuilder(args);builder.Services.AddDaprJobsClient();//Registers the `DaprJobsClient` to be injected as neededvarapp=builder.Build();
Sometimes the developer will need to configure the created client using the various configuration options detailed
above. This is done through an overload that passes in the DaprJobsClientBuiler and exposes methods for configuring
the necessary options.
varbuilder=WebApplication.CreateBuilder(args);builder.Services.AddDaprJobsClient((_,daprJobsClientBuilder)=>{//Set the API tokendaprJobsClientBuilder.UseDaprApiToken("abc123");//Specify a non-standard HTTP endpointdaprJobsClientBuilder.UseHttpEndpoint("http://dapr.my-company.com");});varapp=builder.Build();
Finally, it’s possible that the developer may need to retrieve information from another service in order to populate
these configuration values. That value may be provided from a DaprClient instance, a vendor-specific SDK or some
local service, but as long as it’s also registered in DI, it can be injected into this configuration operation via the
last overload:
varbuilder=WebApplication.CreateBuilder(args);//Register a fictional service that retrieves secrets from somewherebuilder.Services.AddSingleton<SecretService>();builder.Services.AddDaprJobsClient((serviceProvider,daprJobsClientBuilder)=>{//Retrieve an instance of the `SecretService` from the service providervarsecretService=serviceProvider.GetRequiredService<SecretService>();vardaprApiToken=secretService.GetSecret("DaprApiToken").Value;//Configure the `DaprJobsClientBuilder`daprJobsClientBuilder.UseDaprApiToken(daprApiToken);});varapp=builder.Build();
Understanding payload serialization on DaprJobsClient
While there are many methods on the DaprClient that automatically serialize and deserialize data using the
System.Text.Json serializer, this SDK takes a different philosophy. Instead, the relevant methods accept an optional
payload of ReadOnlyMemory<byte> meaning that serialization is an exercise left to the developer and is not
generally handled by the SDK.
That said, there are some helper extension methods available for each of the scheduling methods. If you know that you
want to use a type that’s JSON-serializable, you can use the Schedule*WithPayloadAsync method for each scheduling
type that accepts an object as a payload and an optional JsonSerializerOptions to use when serializing the value.
This will convert the value to UTF-8 encoded bytes for you as a convenience. Here’s an example of what this might
look like when scheduling a Cron expression:
In the same vein, if you have a plain string value, you can use an overload of the same method to serialize a
string-typed payload and the JSON serialization step will be skipped and it’ll only be encoded to an array of
UTF-8 encoded bytes. Here’s an example of what this might look like when scheduling a one-time job:
varnow=DateTime.UtcNow;varoneWeekFromNow=now.AddDays(7);awaitdaprJobsClient.ScheduleOneTimeJobWithPayloadAsync("myOtherJob",oneWeekFromNow,"This is a test!");
The delegate handling the job invocation expects at least two arguments to be present:
A string that is populated with the jobName, providing the name of the invoked job
A ReadOnlyMemory<byte> that is populated with the bytes originally provided during the job registration.
Because the payload is stored as a ReadOnlyMemory<byte>, the developer has the freedom to serialize and deserialize
as they wish, but there are again two helper extensions included that can deserialize this to either a JSON-compatible
type or a string. Both methods assume that the developer encoded the originally scheduled job (perhaps using the
helper serialization methods) as these methods will not force the bytes to represent something they’re not.
To deserialize the bytes to a string, the following helper method can be used:
varpayloadAsString=Encoding.UTF8.GetString(jobPayload.Span);//If successful, returns a string with the value
Error handling
Methods on DaprJobsClient will throw a DaprJobsServiceException if an issue is encountered between the SDK
and the Jobs API service running on the Dapr sidecar. If a failure is encountered because of a poorly formatted
request made to the Jobs API service through this SDK, a DaprMalformedJobException will be thrown. In case of
illegal argument values, the appropriate standard exception will be thrown (e.g. ArgumentOutOfRangeException
or ArgumentNullException) with the name of the offending argument. And for anything else, a DaprException
will be thrown.
The most common cases of failure will be related to:
Incorrect argument formatting while engaging with the Jobs API
Transient failures such as a networking problem
Invalid data, such as a failure to deserialize a value into a type it wasn’t originally serialized from
In any of these cases, you can examine more exception details through the .InnerException property.
6 - Dapr Messaging .NET SDK
Get up and running with the Dapr Messaging .NET SDK
With the Dapr Messaging package, you can interact with the Dapr messaging APIs from a .NET application. In the
v1.15 release, this package only contains the functionality corresponding to the
streaming PubSub capability.
Future Dapr .NET SDK releases will migrate existing messaging capabilities out from Dapr.Client to this
Dapr.Messaging package. This will be documented in the release notes, documentation and obsolete attributes in advance.
6.1 - How to: Author and manage Dapr streaming subscriptions in the .NET SDK
Learn how to author and manage Dapr streaming subscriptions using the .NET SDK
Let’s create a subscription to a pub/sub topic or queue at using the streaming capability. We’ll use the
simple example provided here,
for the following demonstration and walk through it as an explainer of how you can configure message handlers at
runtime and which do not require an endpoint to be pre-configured. In this guide, you will:
Note that while .NET 6 is the minimum support version of .NET in Dapr v1.15, only .NET 8 and .NET 9 will continue to be supported by Dapr in v1.16 and later.
From the .NET SDK root directory, navigate to the Dapr streaming PubSub example.
cd examples/Client/PublishSubscribe
Run the application locally
To run the Dapr application, you need to start the .NET program and a Dapr sidecar. Navigate to the StreamingSubscriptionExample directory.
cd StreamingSubscriptionExample
We’ll run a command that starts both the Dapr sidecar and the .NET program at the same time.
dapr run --app-id pubsubapp --dapr-grpc-port 4001 --dapr-http-port 3500 -- dotnet run
Dapr listens for HTTP requests at http://localhost:3500 and internal Jobs gRPC requests at http://localhost:4001.
Register the Dapr PubSub client with dependency injection
The Dapr Messaging SDK provides an extension method to simplify the registration of the Dapr PubSub client. Before
completing the dependency injection registration in Program.cs, add the following line:
varbuilder=WebApplication.CreateBuilder(args);//Add anywhere between these twobuilder.Services.AddDaprPubSubClient();//That's itvarapp=builder.Build();
It’s possible that you may want to provide some configuration options to the Dapr PubSub client that
should be present with each call to the sidecar such as a Dapr API token, or you want to use a non-standard
HTTP or gRPC endpoint. This be possible through use of an overload of the registration method that allows configuration
of a DaprPublishSubscribeClientBuilder instance:
Still, it’s possible that whatever values you wish to inject need to be retrieved from some other source, itself registered as a dependency. There’s one more overload you can use to inject an IServiceProvider into the configuration action method. In the following example, we register a fictional singleton that can retrieve secrets from somewhere and pass it into the configuration method for AddDaprJobClient so
we can retrieve our Dapr API token from somewhere else for registration here:
It’s possible to configure the Dapr PubSub client using the values in your registered IConfiguration as well without
explicitly specifying each of the value overrides using the DaprPublishSubscribeClientBuilder as demonstrated in the previous
section. Rather, by populating an IConfiguration made available through dependency injection the AddDaprPubSubClient()
registration will automatically use these values over their respective defaults.
Start by populating the values in your configuration. This can be done in several different ways as demonstrated below.
Configuration via ConfigurationBuilder
Application settings can be configured without using a configuration source and by instead populating the value in-memory
using a ConfigurationBuilder instance:
varbuilder=WebApplication.CreateBuilder();//Create the configurationvarconfiguration=newConfigurationBuilder().AddInMemoryCollection(newDictionary<string,string>{{"DAPR_HTTP_ENDPOINT","http://localhost:54321"},{"DAPR_API_TOKEN","abc123"}}).Build();builder.Configuration.AddConfiguration(configuration);builder.Services.AddDaprPubSubClient();//This will automatically populate the HTTP endpoint and API token values from the IConfiguration
Configuration via Environment Variables
Application settings can be accessed from environment variables available to your application.
The following environment variables will be used to populate both the HTTP endpoint and API token used to register the
Dapr PubSub client.
The Dapr PubSub client will be configured to use both the HTTP endpoint http://localhost:54321 and populate all outbound
requests with the API token header abc123.
Configuration via prefixed Environment Variables
However, in shared-host scenarios where there are multiple applications all running on the same machine without using
containers or in development environments, it’s not uncommon to prefix environment variables. The following example
assumes that both the HTTP endpoint and the API token will be pulled from environment variables prefixed with the
value “myapp_”. The two environment variables used in this scenario are as follows:
Key
Value
myapp_DAPR_HTTP_ENDPOINT
http://localhost:54321
myapp_DAPR_API_TOKEN
abc123
These environment variables will be loaded into the registered configuration in the following example and made available
without the prefix attached.
The Dapr PubSub client will be configured to use both the HTTP endpoint http://localhost:54321 and populate all outbound
requests with the API token header abc123.
Use the Dapr PubSub client without relying on dependency injection
While the use of dependency injection simplifies the use of complex types in .NET and makes it easier to
deal with complicated configurations, you’re not required to register the DaprPublishSubscribeClient in this way.
Rather, you can also elect to create an instance of it from a DaprPublishSubscribeClientBuilder instance as
demonstrated below:
publicclassMySampleClass{publicvoidDoSomething(){vardaprPubSubClientBuilder=newDaprPublishSubscribeClientBuilder();vardaprPubSubClient=daprPubSubClientBuilder.Build();//Do something with the `daprPubSubClient`}}
Set up message handler
The streaming subscription implementation in Dapr gives you greater control over handling backpressure from events by
leaving the messages in the Dapr runtime until your application is ready to accept them. The .NET SDK supports a
high-performance queue for maintaining a local cache of these messages in your application while processing is pending.
These messages will persist in the queue until processing either times out for each one or a response action is taken
for each (typically after processing succeeds or fails). Until this response action is received by the Dapr runtime,
the messages will be persisted by Dapr and made available in case of a service failure.
The various response actions available are as follows:
Response Action
Description
Retry
The event should be delivered again in the future.
Drop
The event should be deleted (or forwarded to a dead letter queue, if configured) and not attempted again.
Success
The event should be deleted as it was successfully processed.
The handler will receive only one message at a time and if a cancellation token is provided to the subscription,
this token will be provided during the handler invocation.
The handler must be configured to return a Task<TopicResponseAction> indicating one of these operations, even if from
a try/catch block. If an exception is not caught by your handler, the subscription will use the response action configured
in the options during subscription registration.
The following demonstrates the sample message handler provided in the example:
Task<TopicResponseAction>HandleMessageAsync(TopicMessagemessage,CancellationTokencancellationToken=default){try{//Do something with the messageConsole.WriteLine(Encoding.UTF8.GetString(message.Data.Span));returnTask.FromResult(TopicResponseAction.Success);}catch{returnTask.FromResult(TopicResponseAction.Retry);}}
Configure and subscribe to the PubSub topic
Configuration of the streaming subscription requires the name of the PubSub component registered with Dapr, the name
of the topic or queue being subscribed to, the DaprSubscriptionOptions providing the configuration for the subscription,
the message handler and an optional cancellation token. The only required argument to the DaprSubscriptionOptions is
the default MessageHandlingPolicy which consists of a per-event timeout and the TopicResponseAction to take when
that timeout occurs.
Other options are as follows:
Property Name
Description
Metadata
Additional subscription metadata
DeadLetterTopic
The optional name of the dead-letter topic to send dropped messages to.
MaximumQueuedMessages
By default, there is no maximum boundary enforced for the internal queue, but setting this
property would impose an upper limit.
MaximumCleanupTimeout
When the subscription is disposed of or the token flags a cancellation request, this specifies
the maximum amount of time available to process the remaining messages in the internal queue.
Subscription is then configured as in the following example:
varmessagingClient=app.Services.GetRequiredService<DaprPublishSubscribeClient>();varcancellationTokenSource=newCancellationTokenSource(TimeSpan.FromSeconds(60));//Override the default of 30 secondsvaroptions=newDaprSubscriptionOptions(newMessageHandlingPolicy(TimeSpan.FromSeconds(10),TopicResponseAction.Retry));varsubscription=awaitmessagingClient.SubscribeAsync("pubsub","mytopic",options,HandleMessageAsync,cancellationTokenSource.Token);
Terminate and clean up subscription
When you’ve finished with your subscription and wish to stop receiving new events, simply await a call to
DisposeAsync() on your subscription instance. This will cause the client to unregister from additional events and
proceed to finish processing all the events still leftover in the backpressure queue, if any, before disposing of any
internal resources. This cleanup will be limited to the timeout interval provided in the DaprSubscriptionOptions when
the subscription was registered and by default, this is set to 30 seconds.
6.2 - DaprPublishSubscribeClient usage
Essential tips and advice for using DaprPublishSubscribeClient
Lifetime management
A DaprPublishSubscribeClient is a version of the Dapr client that is dedicated to interacting with the Dapr Messaging API.
It can be registered alongside a DaprClient and other Dapr clients without issue.
It maintains access to networking resources in the form of TCP sockets used to communicate with the Dapr sidecar and implements
IAsyncDisposable to support the eager cleanup of resources.
For best performance, create a single long-lived instance of DaprPublishSubscribeClient and provide access to that shared
instance throughout your application. DaprPublishSubscribeClient instances are thread-safe and intended to be shared.
This can be aided by utilizing the dependency injection functionality. The registration method supports registration using
as a singleton, a scoped instance or as transient (meaning it’s recreated every time it’s injected), but also enables
registration to utilize values from an IConfiguration or other injected service in a way that’s impractical when
creating the client from scratch in each of your classes.
Avoid creating a DaprPublishSubscribeClient for each operation and disposing it when the operation is complete. It’s
intended that the DaprPublishSubscribeClient should only be disposed when you no longer wish to receive events on the
subscription as disposing it will cancel the ongoing receipt of new events.
Configuring DaprPublishSubscribeClient via the DaprPublishSubscribeClientBuilder
A DaprPublishSubscribeClient can be configured by invoking methods on the DaprPublishSubscribeClientBuilder class
before calling .Build() to create the client itself. The settings for each DaprPublishSubscribeClient are separate
and cannot be changed after calling .Build().
vardaprPubsubClient=newDaprPublishSubscribeClientBuilder().UseDaprApiToken("abc123")// Specify the API token used to authenticate to other Dapr sidecars.Build();
The DaprPublishSubscribeClientBuilder contains settings for:
The HTTP endpoint of the Dapr sidecar
The gRPC endpoint of the Dapr sidecar
The JsonSerializerOptions object used to configure JSON serialization
The GrpcChannelOptions object used to configure gRPC
The API token used to authenticate requests to the sidecar
The factory method used to create the HttpClient instance used by the SDK
The timeout used for the HttpClient instance when making requests to the sidecar
The SDK will read the following environment variables to configure the default values:
DAPR_HTTP_ENDPOINT: used to find the HTTP endpoint of the Dapr sidecar, example: https://dapr-api.mycompany.com
DAPR_GRPC_ENDPOINT: used to find the gRPC endpoint of the Dapr sidecar, example: https://dapr-grpc-api.mycompany.com
DAPR_HTTP_PORT: if DAPR_HTTP_ENDPOINT is not set, this is used to find the HTTP local endpoint of the Dapr sidecar
DAPR_GRPC_PORT: if DAPR_GRPC_ENDPOINT is not set, this is used to find the gRPC local endpoint of the Dapr sidecar
DAPR_API_TOKEN: used to set the API token
Configuring gRPC channel options
Dapr’s use of CancellationToken for cancellation relies on the configuration of the gRPC channel options. If you
need to configure these options yourself, make sure to enable the ThrowOperationCanceledOnCancellation setting.
Using cancellation with DaprPublishSubscribeClient
The APIs on DaprPublishSubscribeClient perform asynchronous operations and accept an optional CancellationToken
parameter. This follows a standard .NET practice for cancellable operations. Note that when cancellation occurs, there is
no guarantee that the remote endpoint stops processing the request, only that the client has stopped waiting for completion.
When an operation is cancelled, it will throw an OperationCancelledException.
Configuring DaprPublishSubscribeClient via dependency injection
Using the built-in extension methods for registering the DaprPublishSubscribeClient in a dependency injection container
can provide the benefit of registering the long-lived service a single time, centralize complex configuration and improve
performance by ensuring similarly long-lived resources are re-purposed when possible (e.g. HttpClient instances).
There are three overloads available to give the developer the greatest flexibility in configuring the client for their
scenario. Each of these will register the IHttpClientFactory on your behalf if not already registered, and configure
the DaprPublishSubscribeClientBuilder to use it when creating the HttpClient instance in order to re-use the same
instance as much as possible and avoid socket exhaustion and other issues.
In the first approach, there’s no configuration done by the developer and the DaprPublishSubscribeClient is configured with
the default settings.
varbuilder=WebApplication.CreateBuilder(args);builder.Services.DaprPublishSubscribeClient();//Registers the `DaprPublishSubscribeClient` to be injected as neededvarapp=builder.Build();
Sometimes the developer will need to configure the created client using the various configuration options detailed above. This is done through an overload that passes in the DaprJobsClientBuiler and exposes methods for configuring the necessary options.
varbuilder=WebApplication.CreateBuilder(args);builder.Services.AddDaprJobsClient((_,daprPubSubClientBuilder)=>{//Set the API tokendaprPubSubClientBuilder.UseDaprApiToken("abc123");//Specify a non-standard HTTP endpointdaprPubSubClientBuilder.UseHttpEndpoint("http://dapr.my-company.com");});varapp=builder.Build();
Finally, it’s possible that the developer may need to retrieve information from another service in order to populate these configuration values. That value may be provided from a DaprClient instance, a vendor-specific SDK or some local service, but as long as it’s also registered in DI, it can be injected into this configuration operation via the last overload:
varbuilder=WebApplication.CreateBuilder(args);//Register a fictional service that retrieves secrets from somewherebuilder.Services.AddSingleton<SecretService>();builder.Services.AddDaprPublishSubscribeClient((serviceProvider,daprPubSubClientBuilder)=>{//Retrieve an instance of the `SecretService` from the service providervarsecretService=serviceProvider.GetRequiredService<SecretService>();vardaprApiToken=secretService.GetSecret("DaprApiToken").Value;//Configure the `DaprPublishSubscribeClientBuilder`daprPubSubClientBuilder.UseDaprApiToken(daprApiToken);});varapp=builder.Build();
7 - Error Handling in the Dapr .NET SDK
Learn about error handling in the Dapr.NET SDK.
7.1 - Richer Error Model in the Dapr .NET SDK
Learn how to use the richer error model in the .NET SDK.
The Dapr .NET SDK supports the richer error model, implemented by the Dapr runtime. This model provides a way for applications to enrich their errors with added context,
allowing consumers of the application to better understand the issue and resolve faster. You can read more about the richer error model here, and you
can find the Dapr proto file implementing these errors here.
The Dapr .NET SDK implements all details supported by the Dapr runtime, implemented in the Dapr.Common.Exceptions namespace, and is accessible through
the DaprException extension method TryGetExtendedErrorInfo. Currently this detail extraction is only supported for
RpcException’s where the details are present.
// Example usage of ExtendedErrorInfotry{// Perform some action with the Dapr client that throws a DaprException.}catch(DaprExceptiondaprEx){if(daprEx.TryGetExtendedErrorInfo(outDaprExtendedErrorInfoerrorInfo){Console.WriteLine(errorInfo.Code);Console.WriteLine(errorInfo.Message);foreach(DaprExtendedErrorDetaildetailinerrorInfo.Details){Console.WriteLine(detail.ErrorType);switch(detail.ErrorType)caseExtendedErrorType.ErrorInfo:Console.WriteLine(detail.Reason);Console.WriteLine(detail.Domain);default:Console.WriteLine(detail.TypeUrl);}}}
DaprExtendedErrorInfo
Contains Code (the status code) and Message (the error message) associated with the error, parsed from an inner RpcException.
Also contains a collection of DaprExtendedErrorDetails parsed from the details in the exception.
DaprExtendedErrorDetail
All details implement the abstract DaprExtendedErrorDetail and have an associated DaprExtendedErrorType.
Information telling the client how long to wait before they should retry. Provides a DaprRetryDelay with the properties
Second (offset in seconds) and Nano (offset in nanoseconds).
DebugInfo
Debugging information offered by the server. Contains StackEntries (a collection of strings containing the stack trace), and
Detail (further debugging information).
QuotaFailure
Information relating to some quota that may have been reached, such as a daily usage limit on an API. It has one property Violations,
a collection of DaprQuotaFailureViolation, which each contain a Subject (the subject of the request) and Description (further information regarding the failure).
PreconditionFailure
Information informing the client that some required precondition was not met. Has one property Violations, a collection of
DaprPreconditionFailureViolation, which each has Subject (subject where the precondition failure occured e.g. “Azure”), Type (representation of the precondition type e.g. “TermsOfService”), and Description (further description e.g. “ToS must be accepted.”).
RequestInfo
Information returned by the server that can be used by the server to identify the clients request. Contains
RequestId and ServingData properties, RequestId being some string (such as a UID) the server can interpret,
and ServingData being some arbitrary data that made up part of the request.
LocalizedMessage
Contains a localized message, along with the locale of the message. Contains Locale (the locale e.g. “en-US”) and Message (the localized message).
BadRequest
Describes a bad request field. Contains collection of DaprBadRequestDetailFieldViolation, which each has Field (the offending field in request e.g. ‘first_name’) and
Description (further information detailing the reason e.g. “first_name cannot contain special characters”).
ErrorInfo
Details the cause of an error. Contains three properties, Reason (the reason for the error, which should take the form of UPPER_SNAKE_CASE e.g. DAPR_INVALID_KEY),
Domain (domain the error belongs to e.g. ‘dapr.io’), and Metadata, a key value based collection of futher information.
Help
Provides resources for the client to perform further research into the issue. Contains a collection of DaprHelpDetailLink,
which provides Url (a url to help or documentation), and Description (a description of what the link provides).
ResourceInfo
Provides information relating to an accessed resource. Provides three properties ResourceType (type of the resource being access e.g. “Azure service bus”),
ResourceName (The name of the resource e.g. “my-configured-service-bus”), Owner (the owner of the resource e.g. “subscriptionowner@dapr.io”),
and Description (further information on the resource relating to the error e.g. “missing permissions to use this resource”).
Unknown
Returned when the detail type url cannot be mapped to the correct DaprExtendedErrorDetail implementation.
Provides one property TypeUrl (the type url that could not be parsed e.g. “type.googleapis.com/Google.rpc.UnrecognizedType”).
8 - Developing applications with the Dapr .NET SDK
Learn about local development integration options for .NET Dapr applications
Thinking more than one at a time
Using your favorite IDE or editor to launch an application typically assumes that you only need to run one thing: the application you’re debugging. However, developing microservices challenges you to think about your local development process for more than one at a time. A microservices application has multiple services that you might need running simultaneously, and dependencies (like state stores) to manage.
Adding Dapr to your development process means you need to manage the following concerns:
Each service you want to run
A Dapr sidecar for each service
Dapr component and configuration manifests
Additional dependencies such as state stores
optional: the Dapr placement service for actors
This document assumes that you’re building a production application, and want to create a repeatable and robust set of development practices. The guidance here is general, and applies to any .NET server application using Dapr (including actors).
Managing components
You have two primary methods of storing component definitions for local development with Dapr:
Use the default location (~/.dapr/components)
Use your own location
Creating a folder within your source code repository to store components and configuration will give you a way to version and share these definitions. The guidance provided here will assume you created a folder next to the application source code to store these files.
Development options
Choose one of these links to learn about tools you can use in local development scenarios. These articles are ordered from lowest investment to highest investment. You may want to read them all to get an overview of your options.
The Dapr CLI provides you with a good base to work from by initializing a local redis container, zipkin container, the placement service, and component manifests for redis. This will enable you to work with the following building blocks on a fresh install with no additional setup:
You can run .NET services with dapr run as your strategy for developing locally. Plan on running one of these commands per-service in order to launch your application.
Pro: this is easy to set up since its part of the default Dapr installation
Con: this uses long-running docker containers on your machine, which might not be desirable
Con: the scalability of this approach is poor since it requires running a separate command per-service
Using the Dapr CLI
For each service you need to choose:
A unique app-id for addressing (app-id)
A unique listening port for HTTP (port)
You also should have decided on where you are storing components (components-path).
The following command can be run from multiple terminals to launch each service, with the respective values substituted.
dapr run --app-id <app-id> --app-port <port> --components-path <components-path> -- dotnet run -p <project> --urls http://localhost:<port>
Explanation: this command will use dapr run to launch each service and its sidecar. The first half of the command (before --) passes required configuration to the Dapr CLI. The second half of the command (after --) passes required configuration to the dotnet run command.
💡 Ports
Since you need to configure a unique port for each service, you can use this command to pass that port value to both Dapr and the service. --urls http://localhost:<port> will configure ASP.NET Core to listen for traffic on the provided port. Using configuration at the commandline is a more flexible approach than hardcoding a listening port elsewhere.
If any of your services do not accept HTTP traffic, then modify the command above by removing the --app-port and --urls arguments.
Next steps
If you need to debug, then use the attach feature of your debugger to attach to one of the running processes.
If you want to scale up this approach, then consider building a script which automates this process for your whole application.
8.2 - Dapr .NET SDK Development with .NET Aspire
Learn about local development with .NET Aspire
.NET Aspire
.NET Aspire is a development tool
designed to make it easier to include external software into .NET applications by providing a framework that allows
third-party services to be readily integrated, observed and provisioned alongside your own software.
Aspire simplifies local development by providing rich integration with popular IDEs including
Microsoft Visual Studio,
Visual Studio Code,
JetBrains Rider and others
to launch your application with the debugger while automatically launching and provisioning access to other
integrations as well, including Dapr.
While Aspire also assists with deployment of your application to various cloud hosts like Microsoft Azure and
Amazon AWS, deployment is currently outside the scope of this guide. More information can be found in Aspire’s
documentation here.
Prerequisites
While the Dapr .NET SDK is compatible with .NET 6,
.NET 8 or .NET 9,
.NET Aspire is only compatible with .NET 8 or
.NET 9.
We’ll start by creating a brand new .NET application. Open your preferred CLI and navigate to the directory you wish
to create your new .NET solution within. Start by using the following command to install a template that will create
an empty Aspire application:
dotnet new install Aspire.ProjectTemplates
Once that’s installed, proceed to create an empty .NET Aspire application in your current directory. The -n argument
allows you to specify the name of the output solution. If it’s excluded, the .NET CLI will instead use the name
of the output directory, e.g. C:\source\aspiredemo will result in the solution being named aspiredemo. The rest
of this tutorial will assume a solution named aspiredemo.
dotnet new aspire -n aspiredemo
This will create two Aspire-specific directories and one file in your directory:
aspiredemo.AppHost/ contains the Aspire orchestration project that is used to configure each of the integrations
used in your application(s).
aspiredemo.ServiceDefaults/ contains a collection of extensions meant to be shared across your solution to aid in
resilience, service discovery and telemetry capabilities offered by Aspire (these are distinct from the capabilities
offered in Dapr itself).
aspiredemo.sln is the file that maintains the layout of your current solution
We’ll next create a project that’ll serve as our Dapr application. From the same directory, use the following
to create an empty ASP.NET Core project called MyApp. This will be created relative to your current directory in
MyApp\MyApp.csproj.
dotnet new web MyApp
Next we’ll configure the AppHost project to add the necessary package to support local Dapr development. Navigate
into the AppHost directory with the following and install the CommunityToolkit.Aspire.Hosting.Dapr package from NuGet into the project.
We’ll also add a reference to our MyApp project so we can reference it during the registration process.
This package was previously called Aspire.Hosting.Dapr, which has been marked as deprecated.
cd aspiredemo.AppHost
dotnet add package CommunityToolkit.Aspire.Hosting.Dapr
dotnet add reference ../MyApp/
Next, we need to configure Dapr as a resource to be loaded alongside your project. Open the Program.cs file in that
project within your preferred IDE. It should look similar to the following:
If you’re familiar with the dependency injection approach used in ASP.NET Core projects or others utilizing the
Microsoft.Extensions.DependencyInjection functionality, you’ll find that this will be a familiar experience.
Because we’ve already added a project reference to MyApp, we need to start by adding a reference in this configuration
as well. Add the following before the builder.Build().Run() line:
Because the project reference has been added to this solution, your project shows up as a type within the Projects.
namespace for our purposes here. The name of the variable you assign the project to doesn’t much matter in this tutorial
but would be used if you wanted to create a reference between this project and another using Aspire’s service discovery
functionality.
Adding .WithDaprSidecar() configures Dapr as a .NET Aspire resource so that when the project runs, the sidecar will be
deployed alongside your application. This accepts a number of different options and could optionally be configured as in
the following example:
DaprSidecarOptionssidecarOptions=new(){AppId="my-other-app",AppPort=8080,//Note that this argument is required if you intend to configure pubsub, actors or workflows as of Aspire v9.0 DaprGrpcPort=50001,DaprHttpPort=3500,MetricsPort=9090};builder.AddProject<Projects.MyOtherApp>("myotherapp").WithReference(myApp).WithDaprSidecar(sidecarOptions);
As indicated in the example above, as of .NET Aspire 9.0, if you intend to use any functionality in which Dapr needs to
call into your application such as pubsub, actors or workflows, you will need to specify your AppPort as
a configured option as Aspire will not automatically pass it to Dapr at runtime. It’s expected that this behavior will
change in a future release as a fix has been merged and can be tracked here.
When you open the solution in your IDE, ensure that the aspiredemo.AppHost is configured as your startup project, but
when you launch it in a debug configuration, you’ll note that your integrated console should reflect your expected Dapr
logs and it will be available to your application.
8.3 - Dapr .NET SDK Development with Project Tye
Learn about local development with Project Tye
Project Tye
.NET Project Tye is a microservices development tool designed to make running many .NET services easy. Tye enables you to store a configuration of multiple .NET services, processes, and container images as a runnable application.
Tye is advantageous for a .NET Dapr developer because:
Tye has the ability to automate the dapr CLI built-in
Tye understands .NET’s conventions and requires almost no configuration for .NET services
Tye can manage the lifetime of your dependencies in containers
Pros/cons:
Pro: Tye can automate all of the steps described above. You no longer need to think about concepts like ports or app-ids.
Pro: Since Tye can also manage containers for you, you can make those part of the application definition and stop the long-running containers on your machine.
Using Tye
Follow the Tye Getting Started to install the tye CLI and create a tye.yaml for your application.
Next follow the steps in the Tye Dapr recipe to add Dapr. Make sure to specify the relative path to your components folder with components-path in tye.yaml.
Next add any additional container dependencies and add component definitions to the folder you created earlier.
You should end up with something like this:
name:store-applicationextensions:# Configuration for dapr goes here.- name:daprcomponents-path:<components-path> # Services to run go here.services:# The name will be used as the app-id. For a .NET project, Tye only needs the path to the project file.- name:ordersproject:orders/orders.csproj- name:productsproject:products/products.csproj- name:storeproject:store/store.csproj# Containers you want to run need an image name and set of ports to expose.- name:redisimage:redisbindings:- port:6973
Checkin tye.yaml in source control with the application code.
You can now use tye run to launch the whole application from one terminal. When running, Tye has a dashboard at http://localhost:8000 to view application status and logs.
Next steps
Tye runs your services locally as normal .NET process. If you need to debug, then use the attach feature of your debugger to attach to one of the running processes. Since Tye is .NET aware, it has the ability to start a process suspended for startup debugging.
Tye also has an option to run your services in containers if you wish to test locally in containers.
8.4 - Dapr .NET SDK Development with Docker-Compose
docker-compose is a CLI tool included with Docker Desktop that you can use to run multiple containers at a time. It is a way to automate the lifecycle of multiple containers together, and offers a development experience similar to a production environment for applications targeting Kubernetes.
Pro: Since docker-compose manages containers for you, you can make dependencies part of the application definition and stop the long-running containers on your machine.
Con: most investment required, services need to be containerized to get started.
Con: can be difficult to debug and troubleshoot if you are unfamilar with Docker.
Using docker-compose
From the .NET perspective, there is no specialized guidance needed for docker-compose with Dapr. docker-compose runs containers, and once your service is in a container, configuring it similar to any other programming technology.
💡 App Port
In a container, an ASP.NET Core app will listen on port 80 by default. Remember this for when you need to configure the --app-port later.
To summarize the approach:
Create a Dockerfile for each service
Create a docker-compose.yaml and place check it in to the source code repository
Similar to running locally with dapr run for each service you need to choose a unique app-id. Choosing the container name as the app-id will make this simple to remember.
The compose file will contain at a minimum:
A network that the containers use to communicate
Each service’s container
A <service>-daprd sidecar container with the service’s port and app-id specified
Additional dependencies that run in containers (redis for example)
optional: Dapr placement container (for actors)
You can also view a larger example from the eShopOnContainers sample application.
9 - How to troubleshoot and debug with the Dapr .NET SDK
Tips, tricks, and guides for troubleshooting and debugging with the Dapr .NET SDKs
9.1 - Troubleshoot Pub/Sub with the .NET SDK
Troubleshoot Pub/Sub with the .NET SDK
Troubleshooting Pub/Sub
The most common problem with pub/sub is that the pub/sub endpoint in your application is not being called.
There are a few layers to this problem with different solutions:
The application is not receiving any traffic from Dapr
The application is not registering pub/sub endpoints with Dapr
The pub/sub endpoints are registered with Dapr, but the request is not reaching the desired endpoint
Step 1: Turn up the logs
This is important. Future steps will depend on your ability to see logging output. ASP.NET Core logs almost nothing with the default log settings, so you will need to change it.
Adjust the logging verbosity to include Information logging for ASP.NET Core as described here. Set the Microsoft key to Information.
Step 2: Verify you can receive traffic from Dapr
Start the application as you would normally (dapr run ...). Make sure that you’re including an --app-port argument in the commandline. Dapr needs to know that your application is listening for traffic. By default an ASP.NET Core application will listen for HTTP on port 5000 in local development.
Wait for Dapr to finish starting
Examine the logs
You should see a log entry like:
info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
Request starting HTTP/1.1 GET http://localhost:5000/.....
During initialization Dapr will make some requests to your application for configuration. If you can’t find these then it means that something has gone wrong. Please ask for help either via an issue or in Discord (include the logs). If you see requests made to your application, then continue to step 3.
Step 3: Verify endpoint registration
Start the application as you would normally (dapr run ...).
Use curl at the command line (or another HTTP testing tool) to access the /dapr/subscribe endpoint.
Here’s an example command assuming your application’s listening port is 5000:
curl http://localhost:5000/dapr/subscribe -v
For a correctly configured application the output should look like the following:
* Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 5000 (#0)
> GET /dapr/subscribe HTTP/1.1
> Host: localhost:5000
> User-Agent: curl/7.64.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Fri, 15 Jan 2021 22:31:40 GMT
< Content-Type: application/json
< Server: Kestrel
< Transfer-Encoding: chunked
<
* Connection #0 to host localhost left intact
[{"topic":"deposit","route":"deposit","pubsubName":"pubsub"},{"topic":"withdraw","route":"withdraw","pubsubName":"pubsub"}]* Closing connection 0
Pay particular attention to the HTTP status code, and the JSON output.
< HTTP/1.1 200 OK
A 200 status code indicates success.
The JSON blob that’s included near the end is the output of /dapr/subscribe that’s processed by the Dapr runtime. In this case it’s using the ControllerSample in this repo - so this is an example of correct output.
With the output of this command in hand, you are ready to diagnose a problem or move on to the next step.
Option 0: The response was a 200 included some pub/sub entries
If you have entries in the JSON output from this test then the problem lies elsewhere, move on to step 2.
Option 1: The response was not a 200, or didn’t contain JSON
If the response was not a 200 or did not contain JSON, then the MapSubscribeHandler() endpoint was not reached.
Make sure you have some code like the following in Startup.cs and repeat the test.
app.UseRouting();app.UseCloudEvents();app.UseEndpoints(endpoints=>{endpoints.MapSubscribeHandler();// This is the Dapr subscribe handlerendpoints.MapControllers();});
If adding the subscribe handler did not resolve the problem, please open an issue on this repo and include the contents of your Startup.cs file.
Option 2: The response contained JSON but it was empty (like [])
If the JSON output was an empty array (like []) then the subscribe handler is registered, but no topic endpoints were registered.
If you’re using a controller for pub/sub you should have a method like:
[Topic("pubsub", "deposit")][HttpPost("deposit")]publicasyncTask<ActionResult>Deposit(...)// Using Pub/Sub routing[Topic("pubsub", "transactions", "event.type == \"withdraw.v2\"", 1)][HttpPost("withdraw")]publicasyncTask<ActionResult>Withdraw(...)
In this example the Topic and HttpPost attributes are required, but other details might be different.
If you’re using routing for pub/sub you should have an endpoint like:
In this example the call to WithTopic(...) is required but other details might be different.
After correcting this code and re-testing if the JSON output is still the empty array (like []) then please open an issue on this repository and include the contents of Startup.cs and your pub/sub endpoint.
Step 4: Verify endpoint reachability
In this step we’ll verify that the entries registered with pub/sub are reachable. The last step should have left you with some JSON output like the following:
If after doing this you still don’t understand the problem please open an issue on this repo and include the contents of your Startup.cs.
Option 1: Routing did not execute
If you don’t see an entry for Microsoft.AspNetCore.Routing.EndpointMiddleware in the logs, then it means that the request was handled by something other than routing. Usually the problem in this case is a misbehaving middleware. Other logs from the request might give you a clue to what’s happening.
If you need help understanding the problem please open an issue on this repo and include the contents of your Startup.cs.
Option 2: Routing chose the wrong endpoint
If you see an entry for Microsoft.AspNetCore.Routing.EndpointMiddleware in the logs, but it contains the wrong endpoint then it means that you’ve got a routing conflict. The endpoint that was chosen will appear in the logs so that should give you an idea of what’s causing the conflict.
If you need help understanding the problem please open an issue on this repo and include the contents of your Startup.cs.