Skip to content

Commit

Permalink
Updated documentation to reflect the change
Browse files Browse the repository at this point in the history
Signed-off-by: Whit Waldo <whit.waldo@innovian.net>
  • Loading branch information
WhitWaldo committed Feb 10, 2025
1 parent bdcc98d commit 2bf2bad
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ the dependency injection registration in `Program.cs`, add the following line:
```cs
var builder = WebApplication.CreateBuilder(args);

//Add anywhere between these two
builder.Services.AddDaprJobsClient(); //That's it
//Add anywhere between these two lines
builder.Services.AddDaprJobsClient();

var app = builder.Build();
```
Expand Down Expand Up @@ -203,7 +203,8 @@ public class MySampleClass
It's easy to set up a jobs endpoint if you're at all familiar with [minimal APIs in ASP.NET Core](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis/overview) 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 you can optionally pass a `JobDetails` to get information about the job that has been triggered (e.g. access its scheduling setup or payload).
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:

Expand All @@ -216,7 +217,7 @@ builder.Services.AddDaprJobsClient();
var app = builder.Build();

//Add our endpoint registration
app.MapDaprScheduledJob("myJob", (IServiceProvider serviceProvider, string? jobName, JobDetails? jobDetails) => {
app.MapDaprScheduledJob("myJob", (IServiceProvider serviceProvider, string jobName, ReadOnlyMemory<byte> jobPayload) => {
var logger = serviceProvider.GetService<ILogger>();
logger?.LogInformation("Received trigger invocation for '{jobName}'", "myJob");

Expand All @@ -237,7 +238,7 @@ builder.Services.AddDaprJobsClient();
var app = builder.Build();

//Add our endpoint registration
app.MapDaprScheduledJob("myJob", (string? jobName, JobDetails? jobDetails) => {
app.MapDaprScheduledJob("myJob", (string jobName, ReadOnlyMemory<byte> jobPayload) => {
//Do something...
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,31 +165,18 @@ var oneWeekFromNow = now.AddDays(7);
await daprJobsClient.ScheduleOneTimeJobWithPayloadAsync("myOtherJob", oneWeekFromNow, "This is a test!");
```

The `JobDetails` type returns the data as a `ReadOnlyMemory<byte>?` so the developer has the freedom to deserialize
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:
```cs
if (jobDetails.Payload is not null)
{
string payloadAsString = jobDetails.Payload.DeserializeToString(); //If successful, returns a string value with the value
}
```

To deserialize JSON-encoded UTF-8 bytes to the corresponding type, the following helper method can be used. An
overload argument is available that permits the developer to pass in their own `JsonSerializerOptions` to be applied
during deserialization.

```cs
public sealed record Doodad (string Name, int Value);

//...
if (jobDetails.Payload is not null)
{
var deserializedDoodad = jobDetails.Payload.DeserializeFromJsonBytes<Doodad>();
}
var payloadAsString = Encoding.UTF8.GetString(jobPayload.Span); //If successful, returns a string with the value
```

## Error handling
Expand Down

0 comments on commit 2bf2bad

Please sign in to comment.