https://github.com/shibayan/durable-functions-typed-proxy
Type-safe activity helper for Durable Functions
https://github.com/shibayan/durable-functions-typed-proxy
azure-functions durable-functions
Last synced: 3 months ago
JSON representation
Type-safe activity helper for Durable Functions
- Host: GitHub
- URL: https://github.com/shibayan/durable-functions-typed-proxy
- Owner: shibayan
- License: apache-2.0
- Created: 2019-06-21T07:25:46.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2022-12-15T15:59:10.000Z (over 2 years ago)
- Last Synced: 2025-03-26T16:45:38.659Z (3 months ago)
- Topics: azure-functions, durable-functions
- Language: C#
- Homepage:
- Size: 62.5 KB
- Stars: 6
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# Type-safe activity helper for Durable Functions
[](https://github.com/shibayan/durable-functions-typed-proxy/actions/workflows/build.yml)
[](https://www.nuget.org/packages/DurableTask.TypedProxy/)
[](https://www.nuget.org/packages/DurableTask.TypedProxy/)
[](https://github.com/shibayan/durable-functions-typed-proxy/blob/master/LICENSE)## Basic usage
### 1. Implement the activity
```csharp
// Contract for activity
public interface IHelloActivity
{
// The return type must be Task or Task
Task SayHello(string name);
}// Implementation of activity
public class HelloActivity : IHelloActivity
{
[FunctionName(nameof(SayHello))]
public Task SayHello([ActivityTrigger] string name)
{
return Task.FromResult($"Hello {name}!");
}
}
```### 2. Create type-safe proxy and called methods
```csharp
public class Function1
{
[FunctionName("Function1")]
public async Task> RunOrchestrator(
[OrchestrationTrigger] DurableOrchestrationContext context)
{
var outputs = new List();// Create type-safe activity proxy with interface
var proxy = context.CreateActivityProxy();// Replace "hello" with the name of your Durable Activity Function.
outputs.Add(await proxy.SayHello("Tokyo"));
outputs.Add(await proxy.SayHello("Seattle"));
outputs.Add(await proxy.SayHello("London"));// returns ["Hello Tokyo!", "Hello Seattle!", "Hello London!"]
return outputs;
}
}
```## Advanced usage
### Use ILogger
```csharp
public class HelloActivity : IHelloActivity
{
public HelloActivity(ILoggerFactory loggerFactory)
{
// Create logger instance
_logger = loggerFactory.CreateLogger();
}
private readonly ILogger _logger;[FunctionName(nameof(SayHello))]
public Task SayHello([ActivityTrigger] string name)
{
_logger.LogInformation($"Saying hello to {name}.");
return Task.FromResult($"Hello {name}!");
}
}
```### Retry options
```csharp
public interface IHttpGetActivity
{
// Declarative RetryOptions definition
[RetryOptions("00:00:05", 10)]
Task HttpGet(string path);
}public class HttpGetActivity : IHttpGetActivity
{
public HttpGetActivity(HttpClient httpClient)
{
_httpClient = httpClient;
}private readonly HttpClient _httpClient;
// In case of failure, retry is performed transparently
[FunctionName(nameof(HttpGet))]
public Task HttpGet([ActivityTrigger] string path)
{
return _httpClient.GetStringAsync(path);
}
}
```### Custom retry handler
```csharp
public static class RetryStrategy
{
// Implement custom retry handler
public static bool HttpError(Exception ex)
{
return ex.InnerException is HttpRequestException;
}
}public interface IHttpGetActivity
{
// Must be setting HandlerType and HandlerMethodName
[RetryOptions("00:00:05", 10, HandlerType = typeof(RetryStrategy), HandlerMethodName = nameof(RetryStrategy.HttpError))]
Task HttpGet(string path);
}public class HttpGetActivity : IHttpGetActivity
{
public HttpGetActivity(HttpClient httpClient)
{
_httpClient = httpClient;
}private readonly HttpClient _httpClient;
[FunctionName(nameof(HttpGet))]
public Task HttpGet([ActivityTrigger] string path)
{
return _httpClient.GetStringAsync(path);
}
}
```## Blog
- https://blog.shibayan.jp/entry/20190621/1561114911 (Japanese)
## License
This project is licensed under the [Apache License 2.0](https://github.com/shibayan/durable-functions-typed-proxy/blob/master/LICENSE)