protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
ZmqListener.TransactionReceived += (sender, eventArgs) =>
{
// Do Work Here
}
while (!stoppingToken.IsCancellationRequested)
{
var tokenSource = ZmqListener.Listen("tcp://192.160.0.1:5556", "Transaction");
}
}
public static class ZmqListener
{
public static event EventHandler<TransactionReceivedEventArgs> TransactionReceived;
public static CancellationTokenSource Listen(string uri, string messageType)
{
var source = new CancellationTokenSource();
var token = source.Token;
Task.Factory.StartNew(
() =>
{
using (var socket = new SubscriberSocket())
{
socket.Connect(uri);
socket.Subscribe(messageType);
while (!token.IsCancellationRequested)
{
HandleMessage(socket.ReceiveFrameString());
}
}
},
token);
return source;
}
private static void HandleMessage(string message)
{
var splitMessage = message.Split(' ');
// Do work here
TransactionReceived?.Invoke(sourceName, new TransactionReceivedEventArgs(message));
}
}
And make sure that your method should be modified by async Task:
public async Task YourMethod()
{
//your logic
}
Best Regatds,
Rena
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
I have remove the while loop in the ExecuteAync and it seems to work. May I ask, is this the correct approach?
You have not provide the whole code,so I cannot test the code and answer you whether it is correct or not.I suggest that you could set breakpoint to debug your source code.
Best Regards,
Rena
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
6 Points
40 Posts
donet core 3 worker service with ZMQ
Aug 29, 2019 10:53 AM|Albertk89|LINK
How do I make the ExecuteAsync wait ?
Participant
1060 Points
350 Posts
Re: donet core 3 worker service with ZMQ
Aug 30, 2019 08:42 AM|Rena Ni|LINK
Hi Albertk89,
Actually i do not know what is your scenario clearly.If you want to make the ExecuteAsync wait,you could use await like below:
And make sure that your method should be modified by async Task:
Best Regatds,
Rena
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
6 Points
40 Posts
Re: donet core 3 worker service with ZMQ
Aug 31, 2019 12:31 AM|Albertk89|LINK
I have remove the while loop in the ExecuteAync and it seems to work. May I ask, is this the correct approach? Thank you.
Participant
1060 Points
350 Posts
Re: donet core 3 worker service with ZMQ
Sep 02, 2019 09:30 AM|Rena Ni|LINK
Hi Albertk89,
You have not provide the whole code,so I cannot test the code and answer you whether it is correct or not.I suggest that you could set breakpoint to debug your source code.
Best Regards,
Rena
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.