I am able to send the message to Service Bus and return a Http response using below code.
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
using System.Net;
using System.Text.Json.Serialization;
namespace _78837659
{
public class Function1
{
private readonly ILogger<Function1> _logger;
private readonly IPassfortDataService _passfortDataService;
private readonly IMyService _myService;
public Function1(ILogger<Function1> logger, IPassfortDataService passfortDataService, IMyService myService)
{
_logger = logger;
_myService = myService;
_passfortDataService = passfortDataService;
}
[Function("Process-Duplicates")]
public async Task<DispatchedMessages> ProcessDataAsync(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "myroute")] HttpRequestData req)
{
// Process your data
await _passfortDataService.ProcessDataAsync();
HttpResponseData response = req.CreateResponse(HttpStatusCode.OK);
await response.WriteStringAsync("HTTP response: Message sent");
return new DispatchedMessages()
{
Messages = _myService.MessagesToBeSent.Select(x => x.ToJson()),
HttpResponse = response
};
}
}
public class DispatchedMessages
{
[JsonIgnore]
[ServiceBusOutput("myqueue", Connection = "ServiceBusConnection")]
public IEnumerable<string>? Messages { get; set; }
public HttpResponseData HttpResponse { get; set; }
}
}
You should have below codes in the mentioned files.
.csproj-
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
<OutputType>Exe</OutputType>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>_78837659</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.21.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.ServiceBus" Version="5.20.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.17.0" />
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.2.0" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
<ItemGroup>
<Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext" />
</ItemGroup>
</Project>
Program.cs-
using _78837659;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.ConfigureServices(services =>
{
services.AddApplicationInsightsTelemetryWorkerService();
services.ConfigureFunctionsApplicationInsights();
services.AddSingleton<IPassfortDataService, PassfortDataService>();
services.AddSingleton<IMyService, MyService>();
})
.Build();
host.Run();
I am able to see below response while invoking the URL.

