I get this error:
ManagedIdentityCredential authentication failed: Retry failed after 6 tries. Retry settings can be adjusted in ClientOptions.Retry or by configuring a custom retry policy in ClientOptions.RetryPolicy. (No connection could be made because the target machine actively refused it.) (No connection could be made because the target machine actively refused it.
DurableTask.Core.Exceptions.TaskFailedException: Exception of type 'DurableTask.Core.Exceptions.TaskFailedException' was thrown.
at DurableTask.Core.TaskOrchestrationContext.ScheduleTaskInternal(String name, String version, String taskList, Type resultType, Object[] parameters) in //src/DurableTask.Core/TaskOrchestrationContext.cs:line 121
at DurableTask.Core.TaskOrchestrationContext.ScheduleTaskToWorker[TResult](String name, String version, String taskList, Object[] parameters) in //src/DurableTask.Core/TaskOrchestrationContext.cs:line 92
at DurableTask.Core.TaskOrchestrationContext.ScheduleTask[TResult](String name, String version, Object[] parameters) in /_/src/DurableTask.Core/TaskOrchestrationContext.cs:line 84
at Microsoft.DurableTask.Worker.Shims.TaskOrchestrationContextWrapper.CallActivityAsync[T](TaskName name, Object input, TaskOptions options)
--- End of inner exception stack trace ---
at Microsoft.DurableTask.Worker.Shims.TaskOrchestrationContextWrapper.CallActivityAsync[T](TaskName name, Object input, TaskOptions options)
Here is my code:
services.AddSingleton(services =>
{
var serviceBus = GetValueOrDefaultBase("serviceBus__fullyQualifiedNamespace");
return new ServiceBusClient(serviceBus, new DefaultAzureCredential());
});
await context.CallActivityAsync(nameof(SenderFunction), content);
await serviceBusRepository.SendMessageAsync(message);
public async Task SendMessageAsync(ServiceBusMessage message)
{
var serviceBusmessage = new ServiceBusMessage
{
Body = new BinaryData(message.Body),
MessageId = message.MessageId
};
if (message.ApplicationProperties != null)
{
foreach (var property in message.ApplicationProperties)
{
serviceBusmessage.ApplicationProperties.Add(property.Key, property.Value);
}
}
await _serviceBusSender.SendMessageAsync(serviceBusmessage);
}
I am able to send message to service bus using below approach:
Firstly, created function app and then enabled managed identity:
Then In Service Bus ---> Access Control IAM ---> Add Role ---> Data Sender:
Then:
After clicking next, now select the Function app managed identity and then select and then review and create.
Below Function Code worked for me:
Program.cs:
using Azure.Identity;
using Azure.Messaging.ServiceBus;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
var rith = new HostBuilder()
.ConfigureFunctionsWebApplication()
.ConfigureServices(cho =>
{
cho.AddApplicationInsightsTelemetryWorkerService();
cho.ConfigureFunctionsApplicationInsights();
cho.AddSingleton<ServiceBusClient>(provider =>
{
return new ServiceBusClient("rith02.servicebus.windows.net", new DefaultAzureCredential());
});
})
.Build();
rith.Run();
Function.cs:
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using Azure.Messaging.ServiceBus;
using Microsoft.Azure.Functions.Worker.Http;
namespace FunctionApp10
{
public class Function1
{
private readonly ILogger<Function1> ri_lg;
private readonly ServiceBusClient ri_sc;
public Function1(ILogger<Function1> logger, ServiceBusClient sbc)
{
ri_lg = logger;
ri_sc = sbc;
}
[Function("Function1")]
public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req)
{
ri_lg.LogInformation("Hello Rithwik Bojja");
await TestRith("rith", "Secret!!");
var rout = req.CreateResponse(System.Net.HttpStatusCode.OK);
await rout.WriteStringAsync("Welcome to Azure Functions!");
return rout;
}
private async Task TestRith(string qn, string msg)
{
var ri_sen = ri_sc.CreateSender(qn);
var sbc1 = new ServiceBusMessage(msg);
await ri_sen.SendMessageAsync(sbc1);
ri_lg.LogInformation($"Message sent to queue {qn}: {msg}");
}
}
}
Output:
This is how I send messages to Service Bus Queue from Azure Function App.