C
C#•6mo ago
Mozzarella

RunSynchronously may not be called on a task not bound to a delegate

I have a sync kafka method that interfaces message on kafka, whenever I use it on method I got error called 'RunSynchronously may not be called on a task not bound to a delegate, such as the task returned from an asynchronous method.' is there anyway to use and make a sure sendKafka Method completes when it is called without changing the implementation of SendKafka method itself?:
public void InterfaceUserInformation(IDbContext dbContext, Materiallot materiallot, MessageData messageData)
{
MessageBuilderMD messageBuilderMD = new MessageBuilderMD();
messageBuilderMD
.WithMessageData(messageData)
.WithNameOfRequest("To try")
.Build();
kafkaContext.SendKafka(messageBuilderMD.GetMessageData(), dbContext);

}
public void InterfaceUserInformation(IDbContext dbContext, Materiallot materiallot, MessageData messageData)
{
MessageBuilderMD messageBuilderMD = new MessageBuilderMD();
messageBuilderMD
.WithMessageData(messageData)
.WithNameOfRequest("To try")
.Build();
kafkaContext.SendKafka(messageBuilderMD.GetMessageData(), dbContext);

}
18 Replies
Alex
Alex•6mo ago
hellow is the context an async contexT? If its not try this public void InterfaceUserInformation(IDbContext dbContext, Materiallot materiallot, MessageData messageData) { MessageBuilderMD messageBuilderMD = new MessageBuilderMD(); messageBuilderMD .WithMessageData(messageData) .WithNameOfRequest("To try") .Build(); kafkaContext.SendKafka(messageBuilderMD.GetMessageData(), dbContext).GetAwaiter().GetResult(); } this should work or you can chnage the function too
public async Task InterfaceUserInformationAsync(IDbContext dbContext, Materiallot materiallot, MessageData messageData) { MessageBuilderMD messageBuilderMD = new MessageBuilderMD(); messageBuilderMD .WithMessageData(messageData) .WithNameOfRequest("To try") .Build(); // Await the asynchronous SendKafka method await kafkaContext.SendKafka(messageBuilderMD.GetMessageData(), dbContext); }
let me know if you have any questions
Mozzarella
MozzarellaOP•6mo ago
this context doesn't have a getAwaiter 😦
Alex
Alex•6mo ago
its expecting an async output i will try to see if it builds
Mozzarella
MozzarellaOP•6mo ago
Error (active) CS1061 '=Context' does not contain a definition for 'GetAwaiter' and no accessible extension method 'GetAwaiter' accepting a first argument of type
Angius
Angius•6mo ago
Not a task, then
Mozzarella
MozzarellaOP•6mo ago
wdm?
Angius
Angius•6mo ago
Exactly what I said .GetAwaiter() is a method of Task Only a Task can be awaited Ergo, not a task
Mozzarella
MozzarellaOP•6mo ago
yeah what would be a way to use that method?
Angius
Angius•6mo ago
What do the docs of the library you're using suggest?
Mozzarella
MozzarellaOP•6mo ago
well, there is no library the code itself packed into nuget, and I saw a code once,it was using cofluent
Angius
Angius•6mo ago
What do the docs of the nuget you're using suggest?
Mozzarella
MozzarellaOP•6mo ago
There is no documentation on this wrapper sadly
Angius
Angius•6mo ago
I'd use a wrapper that isn't shit, then
Mozzarella
MozzarellaOP•6mo ago
That connect with kafka using cofluent and make producer with consumer..sendkafka a method that produces a message Well ,I agree
Angius
Angius•6mo ago
There seems to be a well-documented official library by Confluent: https://docs.confluent.io/kafka-clients/dotnet/current/overview.html
Mozzarella
MozzarellaOP•6mo ago
I know, but I need to use exact method
Angius
Angius•6mo ago
Did you step through the code to see what exactly is throwing here? Also, the answer to
is there anyway to use and make a sure sendKafka Method completes when it is called without changing the implementation of SendKafka method itself?
is "no" You could do it like it's 2001 Javascript and use callbacks like a caveman You could do it properly with async/await But either would require the SendKafka method to be changed Or not written by a halfwit in the first place Assuming it is supposed to be observed like that and isn't supposed to be just a fire&forget
Mozzarella
MozzarellaOP•6mo ago
well thanks, seems like I need to wait him again

Did you find this page helpful?