Skip to content

Commit a714a19

Browse files
Merge pull request #40635 from rostyslav-diakiv/patch-3
Fixed wrong syntax in Code example
2 parents 4dd1747 + 5b7a764 commit a714a19

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

articles/azure-functions/durable/durable-functions-entities.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,68 @@ For example, we can modify the counter entity example above so it sends a "miles
203203
break;
204204
```
205205

206+
The following snippet demonstrates how to incorporate the injected service into your entity class.
207+
208+
```csharp
209+
public class HttpEntity
210+
{
211+
private readonly HttpClient client;
212+
213+
public HttpEntity(IHttpClientFactory factory)
214+
{
215+
this.client = factory.CreateClient();
216+
}
217+
218+
public async Task<int> GetAsync(string url)
219+
{
220+
using (var response = await this.client.GetAsync(url))
221+
{
222+
return (int)response.StatusCode;
223+
}
224+
}
225+
226+
// The function entry point must be declared static
227+
[FunctionName(nameof(HttpEntity))]
228+
public static Task Run([EntityTrigger] IDurableEntityContext ctx)
229+
=> ctx.DispatchAsync<HttpEntity>();
230+
}
231+
```
232+
233+
> [!NOTE]
234+
> Unlike when using constructor injection in regular .NET Azure Functions, the functions entry point method for class-based entities *must* be declared `static`. Declaring a non-static function entry point may cause conflicts between the normal Azure Functions object initializer and the Durable Entities object initializer.
235+
236+
### Bindings in entity classes (.NET)
237+
238+
Unlike regular functions, entity class methods do not have direct access to input and output bindings. Instead, binding data must be captured in the entry-point function declaration and then passed to the `DispatchAsync<T>` method. Any objects passed to `DispatchAsync<T>` will be automatically passed into the entity class constructor as an argument.
239+
240+
The following example shows how a `CloudBlobContainer` reference from the [blob input binding](../functions-bindings-storage-blob.md#input) can be made available to a class-based entity.
241+
242+
```csharp
243+
public class BlobBackedEntity
244+
{
245+
private readonly CloudBlobContainer container;
246+
247+
public BlobBackedEntity(CloudBlobContainer container)
248+
{
249+
this.container = container;
250+
}
251+
252+
// ... entity methods can use this.container in their implementations ...
253+
254+
[FunctionName(nameof(BlobBackedEntity))]
255+
public static Task Run(
256+
[EntityTrigger] IDurableEntityContext context,
257+
[Blob("my-container", FileAccess.Read)] CloudBlobContainer container)
258+
{
259+
// passing the binding object as a parameter makes it available to the
260+
// entity class constructor
261+
return context.DispatchAsync<BlobBackedEntity>(container);
262+
}
263+
}
264+
```
265+
266+
For more information on bindings in Azure Functions, see the [Azure Functions Triggers and Bindings](../functions-triggers-bindings.md) documentation.
267+
206268
## Entity coordination
207269

208270
There may be times when you need to coordinate operations across multiple entities. For example, in a banking application, you may have entities representing individual bank accounts. When transferring funds from one account to another, you must ensure that the _source_ account has sufficient funds, and that updates to both the _source_ and _destination_ accounts are done in a transactionally consistent way.

0 commit comments

Comments
 (0)