I’m trying to do some internal demos of the temporal dotnet-sdk for a project but am having issues triggering a workflow from an API call. I am sure I am just going about things the wrong way but would appreciate some guidance.
I am using the MoneyTransfer example workflow and just trying to throw the trigger into an API POST call that looks like:
using Microsoft.AspNetCore.Mvc;
using Temporalio.MoneyTransferProject.MoneyTransferWorker;
using Temporalio.Client;
using TemporalTestAPI.Models;
namespace TemporalTestAPI.Controllers
{
[Route("api/workflow")]
[ApiController]
public class WorkflowController(ITemporalClient client) : ControllerBase
{
private const string TASK_QUEUE = "MONEY_TRANSFER_TASK_QUEUE";
[HttpPost("start")]
public async Task<IActionResult> Start([FromBody] WorkflowStartDto dto)
{
var details = new PaymentDetails(
SourceAccount: dto.SourceAccount,
TargetAccount: dto.TargetAccount,
Amount: dto.Amount,
ReferenceId: dto.ReferenceId
);
string workflowId = $"pay-invoice-{Guid.NewGuid()}";
try
{
WorkflowHandle handle = await client.StartWorkflowAsync(
(MoneyTransferWorkflow wf) => wf.RunAsync(details),
new WorkflowOptions(id: workflowId, taskQueue: TASK_QUEUE)
);
return Ok(handle);
}
catch (Exception e)
{
return BadRequest(e);
}
}
}
}
My program.cs looks like:
using Microsoft.AspNetCore.StaticFiles;
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers(options =>
{
options.ReturnHttpNotAcceptable = true;
}).AddNewtonsoftJson()
.AddXmlDataContractSerializerFormatters();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddProblemDetails(options =>
{
options.CustomizeProblemDetails = ctx =>
{
ctx.ProblemDetails.Extensions.Add("server", Environment.MachineName);
};
});
builder.Services.AddTemporalClient("localhost:7233", "default");
WebApplication app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
When I run this (with an external worker) I keep getting a loop saying “withdrawing $400 from account 85-150” which is just the first step of the workflow, it never continue past that point. I just want the endpoint to return the workflow id and the run id.
I appreciate any guidance!