user2370664
user2370664

Reputation: 423

.Net 6 Azure Functions project - include Azure.Worker.FunctionAttribute and Azure.WebJobs.FunctionNameAttribute in same project

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")])

enter image description here

Is it possible to make these both work in a .net 6 Azure functions application?

Upvotes: 0

Views: 621

Answers (1)

Pravallika KV
Pravallika KV

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.

  • Also Function tag in [Function("Function1")] supports Isolated Function and [FunctionName("Function1_HttpStart")] supports In-Process function.

I tried to implement the same.

  • Created an Isolated Http Trigger Azure Function.

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;
}
  • Created Durable Azure Function with below code.
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:

  • Could see only the default Isolated Http Trigger Function getting executed.

enter image description here

Differences between in-process and isolate worker process .NET Azure Functions

Upvotes: 1

Related Questions