Search code examples
c#azureazure-logic-appsazure-logic-app-standard

logic app local function unable to resolve service


I have a logic app standard workflow that calls the following .Net 472 local function:

namespace NS.Integration.ProcessManager.Function
{
    using System;
    using System.Threading.Tasks;
    using Azure.Identity;
    using Azure.Messaging.ServiceBus;
    using Microsoft.Extensions.Logging;
    using Microsoft.Azure.Functions.Extensions.Workflows;
    using Microsoft.Azure.WebJobs;

    public class SvcBusQReader(ILoggerFactory loggerFactory)
    {
        private readonly ILogger<SvcBusQReader> _logger = loggerFactory.CreateLogger<SvcBusQReader>();

        [FunctionName("ReadQueueSessionMessage")]
        public async Task<string> Run([WorkflowActionTrigger]
            string queueName, string sessionId)
        {
            var serviceBusNamespace = Environment.GetEnvironmentVariable("serviceBus_fullyQualifiedNamespace");
            if (string.IsNullOrEmpty(serviceBusNamespace))
            {
                throw new InvalidOperationException("Service Bus namespace is not configured.");
            }

            var credential = new DefaultAzureCredential();
            var client = new ServiceBusClient(serviceBusNamespace, credential);

            var receiverOptions = new ServiceBusSessionReceiverOptions
            {
                ReceiveMode = ServiceBusReceiveMode.PeekLock
            };

            var receiver = await client.AcceptSessionAsync(queueName, sessionId, receiverOptions);

            if (receiver == null)
            {
                throw new InvalidOperationException($"No session found with ID: {sessionId}");
            }

            var message = await receiver.ReceiveMessageAsync();
            if (message == null)
            {
                throw new InvalidOperationException($"No message found in session with ID: {sessionId}");
            }

            string messageBody = message.Body.ToString();
            await receiver.CompleteMessageAsync(message);
            return messageBody;
        }
    }
}

The action from the workflow that calls the function can be seen below:

"Call_a_local_function_in_this_logic_app": {
                        "inputs": {
                            "functionName": "ReadQueueSessionMessage",
                            "parameters": {
                                "queueName": "sbq-intg-procman-dev-response",
                                "sessionId": "@variables('sessionId')"
                            }
                        },
                        "type": "InvokeFunction"
                    }

When the call is made, I see the following exception (both testing locally from vscode and when deployed to azure):

Result: Failure Exception: System.InvalidOperationException: Unable to resolve service for type 'Microsoft.Extensions.Logging.ILoggerFactory' while attempting to activate 'NS.Integration.ProcessManager.Function.SvcBusQReader'. at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters) at Microsoft.Azure.Functions.Worker.Invocation.DefaultFunctionExecutor.d__4.MoveNext() in D:\a_work\1\s\src\DotNetWorker.Core\Invocation\DefaultFunctionExecutor.cs:line 34


Solution

  • This was down to user error. I had downgraded the csproj from a dotnet8 to 472 but forgot to change the package ref for Microsoft.Extensions.Logging and Microsoft.Extensions.Logging.Abstractions