Reputation: 423
I'm working on an existing Azure Functions project that contains Microsoft.Azure.Functions.Worker.FunctionAttribute functions.
For example:
using Microsoft.Azure.Functions.Worker;
[Function("TestFunction")]
public async Task Run([HttpTrigger(AuthorizationLevel.Function,
FunctionSettings.HttpGet, Route = FunctionSettings.CycleCountEndpoint)]
FunctionContext context)
{
await _repo.RunSomething();
}
}
After encountering a long running Azure function I would like to create an Azure Durable Function in this same project: This function looks like this:
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.DurableTask;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using System.Net.Http;
public static class TestDurableFunction
{
[FunctionName("TestDurableFunction")]
public static async Task<HttpResponseMessage>
HttpStart([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")]
HttpRequestMessage req,
[DurableClient] IDurableOrchestrationClient starter,
ILogger log)
{
string instanceId = await starter.StartNewAsync("TestOrchestrator", null);
log.LogInformation($"Started orchestration with ID = '{instanceId}'.");
return starter.CreateCheckStatusResponse(req, instanceId);
}
}
When I run the project, the TestFunction decorated with [Function("TestFunction")] appears in the console. The Azure.WebJobs.FunctionNameAttribute function does not appear in the console ([FunctionName("TestDurableFunction")])
Is it possible to make these both work in a .net 6 Azure functions application?
Upvotes: 0
Views: 621
Reputation: 8694
It is not possible to implement both in the same project.
Because Microsoft.Azure.Functions.Worker
supports Isolated Azure Function whereas Microsoft.Azure.WebJobs
supports In-process Azure Function.
[Function("Function1")]
supports Isolated Function and [FunctionName("Function1_HttpStart")]
supports In-Process function.I tried to implement the same.
Code:
using System.Net;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
[Function("Function1")]
public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req)
{
_logger.LogInformation("C# HTTP trigger function processed a request.");
var response = req.CreateResponse(HttpStatusCode.OK);
response.Headers.Add("Content-Type", "text/plain; charset=utf-8");
response.WriteString("Welcome to Azure Functions!");
return response;
}
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.DurableTask;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
[FunctionName("Function1_HttpStart")]
public static async Task<HttpResponseMessage> HttpStart(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestMessage req,
[DurableClient] IDurableOrchestrationClient starter,
ILogger log)
{
string instanceId = await starter.StartNewAsync("Function1", null);
log.LogInformation("Started orchestration with ID = '{instanceId}'.", instanceId);
return starter.CreateCheckStatusResponse(req, instanceId);
}
Response:
Differences between in-process and isolate worker process .NET Azure Functions
Upvotes: 1