Reputation: 3736
I am migrating some functions from netcore 3.1 to net5, to use isolated model. However, I have come across this incompatibility that I have not resolve; the documentation has not led me to find a solution. It's about using an Azure function to put a row in a storage table, this code snippet works perfect:
// netcoreapp3.1, works OK
[FunctionName("PlaceOrder")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "post")]
OrderPlacement orderPlacement,
[Table("Orders")] IAsyncCollector<Order> orders)
{
await orders.AddAsync(new Order {
PartitionKey = "US",
RowKey = Guid.NewGuid().ToString(),
CustomerName = orderPlacement.CustomerName,
Product = orderPlacement.Product
});
return new OkResult();
}
The attempt to do it in net5, would be very similar to the following:
// net5.0 approach
[Function("PlaceOrder")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "post")]
HttpRequestData req,
[Table("Orders")] IAsyncCollector<Order> orders)
{
var orderPlacement = await req.ReadFromJsonAsync<OrderPlacement>();
await orders.AddAsync(new Order {
PartitionKey = "US",
RowKey = Guid.NewGuid().ToString(),
CustomerName = orderPlacement.CustomerName,
Product = orderPlacement.Product
});
return new OkResult();
}
But the line of Table bind indicates the error: The attribute 'TableAttribute' is a WebJobs attribute and not supported in the .NET Worker (Isolated Process). - What is the correct equivalent?
Upvotes: 12
Views: 9861
Reputation: 811
I had the same Issue and added the following references to my *.csproj file:
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.5.1" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.0.4" OutputItemType="Analyzer" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.0.13" />
</ItemGroup>
Upvotes: 2
Reputation: 3736
The solution is a single line. The equivalent is the following:
public static class PlaceOrder
{
[Function("PlaceOrder")]
[TableOutput("Orders")]
public static async Task<Order> TableOutput(
[HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequestData req)
{
var orderPlacement = await req.ReadFromJsonAsync<OrderPlacement>();
return new Order {
PartitionKey = "US",
RowKey = Guid.NewGuid().ToString(),
CustomerName = orderPlacement.CustomerName,
Product = orderPlacement.Product
};
}
}
Upvotes: 2
Reputation: 10849
You should be using Microsoft.Azure.Functions.Worker.Extensions.*
package for Azure function .net 5.
You can read this official documentation for further information.
So for table binding use Microsoft.Azure.Functions.Worker.Extensions.Storage
package and also use TableOutput
attribute for binding.
Like shown below:
[Function("TableFunction")]
[TableOutput("OutputTable", Connection = "AzureWebJobsStorage")]
public static MyTableData Run([QueueTrigger("table-items")] string input,
[TableInput("MyTable", "MyPartition", "{queueTrigger}")] MyTableData tableInput,
FunctionContext context)
{
var logger = context.GetLogger("TableFunction");
logger.LogInformation($"PK={tableInput.PartitionKey}, RK={tableInput.RowKey}, Text={tableInput.Text}");
return new MyTableData()
{
PartitionKey = "queue",
RowKey = Guid.NewGuid().ToString(),
Text = $"Output record with rowkey {input} created at {DateTime.Now}"
};
}
You can check the sample for various supported bindings at here.
Upvotes: 27