6
Hello, I am working with C# and I have the following problem:
Imagine I have a route to my API api/estoque/inventario
where this route should make the calculation of the inventory of the entire application stock and on demand return the result. To set up this API route, I would have something similar to this:
namespace Api
{
public class Estoque { ... }
[Route("api/estoque/inventario")]
[ApiController]
public class EstoqueController: ControllerBase
{
[HttpGet]
public async Task<IActionResult<ICollection<Estoque>>> GetInventario()
{
try
{
// pega os produtos
// faz todas as regras de negócio (síncronas e assíncronas)
// retorna o estoque com o inventário (como?)
}
catch(Exception ex)
{
throw;
}
}
}
}
The problem is that even if I have an asynchronous task in my API, the answer to my client is not asynchronous. On the Client side, I use the library Rxjs together with the Angular and the service that makes the requests to the API has the return of an observable object, thus:
export const API_URL = '...';
export interface IEstoque { ... }
@Injectable({
providedIn: 'root',
})
export class EstoqueService {
private baseUrl: string = "/api/estoque";
constructor(public http: HttpClient) { }
public getInventario(): Observable<IEstoque[]> {
return this.http.get(`${API_URL}${this.baseUrl}/inventario`);
}
}
My intention is to use the list of processed items in stock along with the pipe Asyncpipe which exists at the angle, which makes it possible for me to make an asynchronous loop based on the return of service, thus:
<div *ngFor='let estoque of estoqueList | async'>
{{estoque | json}}
</div>
@Component({
selector: 'estoque-list',
templateUrl: 'estoque-list.component.html',
})
export class EstoqueList {
public estoqueList: Observable<IEstoque[]>;
constructor(public estoqueService: EstoqueService) {
this.estoqueList = estoqueService.getInventario();
}
}
What I need to do to get my route api/estoque/inventario
will send the inventory list asynchronously and continuously to my processing-based frontend?
In this case to send continuous data better use websockets. I do not know how do in c#.
– Eduardo Vargas
One thing I don’t understand, when you say the server side needs to be asynchronous, for what reason? You described that you want to "do the inventory calculation of the entire application stock and by demand return the result", what would that be on demand? You want an application that listens and as new data appear in stock it is already showing in this application in "real time"?
– echojn
The statement of his api is correct, now you need to see what’s left of the code within it. All chain calls, in all layers/projects you have a call from your api must return tasks
async
, and be called usingawait
, or will not function properly asynchronously– Ricardo Pontual
What I need to do is what Eduardo said, like websockets, only I have no idea how to do it in C#
– LeandroLuk
This would be an update in Altime Juliano. The apication itself is a logistics control application and this needs to be managed in Altime.
– LeandroLuk
Yes Ricardo, I am aware of this, so much so that I am generating an Api and not a layer of direct access to data.
– LeandroLuk
Wow, I don’t understand your doubt, the flames in the service and the method in the C are no longer asynchronous#?
– Marconi
Even if the calls are asynchronous in the service, the return of the content is not asynchronous, I have to process all the data before returning. The intention is that I may return gradually, I believe that as Eduardo said with websockets
– LeandroLuk
If you know some nodejs recommend https://socket.io/
– Eduardo Vargas
i have custom of working with socket.io in Nodejs, the problem is that the person of the company where I work does not want to change technology, therefore I am obliged to use C# + Aspnetcore + Angular...
– LeandroLuk
As much as you make a partial flush and watching the response stream, still what you would receive would be an incomplete, invalid Json. If you want a real-time broadcast you will need to completely change your architecture.
– Leandro Angelo
I was even seeing here about Laravel’s Broadcast, what he and Socket.io do is what I’m looking for
– LeandroLuk
If you want to work via websockets, you can use signalr on . net and make the communication. Asp.net/signalr. At the angle already have signaling libraries too.
– Jozimar Back
@When I say I need to send it asynchronously, it’s because I can’t do everything and then send... My idea is to send whenever you have completed a stock item so that it does not lock the application on both sides
– LeandroLuk
@Ricardopunctual I do this does not guarantee that the shipment to the customer will be asynchronous, ie by finalizing calculation
– LeandroLuk