-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMessageProducerController.cs
52 lines (45 loc) · 1.65 KB
/
MessageProducerController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using System.Net.Http;
using producer.Models;
using Newtonsoft.Json;
using System.Text;
using Newtonsoft.Json.Serialization;
using System.Collections.Generic;
using System.Net;
namespace producer.Controllers
{
[ApiController]
[Route("[controller]")]
public class MessageProducerController : ControllerBase
{
[HttpPost]
public async Task<IActionResult> Produce([FromBody]Produce produce)
{
var daprport = "3500";
var daprUrl = $"http://localhost:{daprport}/v1.0/bindings/message-queue";
for (var i = 0; i < produce.Count; i++)
{
var msg = new Message
{
Text = "Hello World"
};
var payload = new
{
data = msg,
operation = "create"
};
var client = new HttpClient();
var data = JsonConvert.SerializeObject(payload, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() });
var result = await client.PostAsync(daprUrl, new StringContent(data, Encoding.UTF8, "application/json"));
if (!result.IsSuccessStatusCode)
{
var text = result.Content.ReadAsStringAsync();
return StatusCode((int)HttpStatusCode.InternalServerError, text);
}
await Task.Delay(produce.IntervalMilliseconds);
}
return Ok();
}
}
}