Microsoft Solutions
Backend Development
AI Consulting
E-Commerce Apps
Frontend Development
Cloud Solutions
Frontend
Backend
Mobile App
Microsoft
Industry we Served
In today's fast-paced world, real-time applications are critical for businesses. From stock tracking to e-commerce order management, the need for instant updates is ubiquitous. This blog demonstrates how to create a real-time Order Management System using .NET Core Web API, SignalR, and a simple Angular client.
Run the following command to create a new Web API project:
bashdotnet new webapi -n OrderManagementApi
Navigate to the project directory:
bashcd OrderManagementApi
Install SignalR and Entity Framework Core:
bashdotnet add package Microsoft.AspNetCore.SignalR dotnet add package Microsoft.EntityFrameworkCore dotnet add package Microsoft.EntityFrameworkCore.SqlServer dotnet add package Microsoft.EntityFrameworkCore.Tools
Create a Models
folder and add an Order
class:
csharppublic class Order
{
public int Id { get; set; }
public string ProductName { get; set; }
public int Quantity { get; set; }
public DateTime OrderDate { get; set; }
public string Status { get; set; }
}
Create an OrderContext
class in a Data
folder:
csharpusing Microsoft.EntityFrameworkCore;
public class OrderContext : DbContext
{
public OrderContext(DbContextOptions<OrderContext> options) : base(options) { }
public DbSet<Order> Orders { get; set; }
}
Configure the database connection in appsettings.json
:
json"ConnectionStrings": {
"DefaultConnection": "Server=localhost;Database=OrderDB;Trusted_Connection=True;"
}
Update the Program.cs
file to add DbContext:
csharpbuilder.Services.AddDbContext<OrderContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
Add an OrdersController
:
csharp[ApiController]
[Route("api/[controller]")]
public class OrdersController : ControllerBase
{
private readonly OrderContext _context;
public OrdersController(OrderContext context)
{
_context = context;
}
[HttpGet]
public async Task<ActionResult<IEnumerable<Order>>> GetOrders()
{
return await _context.Orders.ToListAsync();
}
[HttpPost]
public async Task<ActionResult<Order>> AddOrder(Order order)
{
_context.Orders.Add(order);
await _context.SaveChangesAsync();
return CreatedAtAction(nameof(GetOrders), new { id = order.Id }, order);
}
}
Create an OrderHub
class:
csharpusing Microsoft.AspNetCore.SignalR;
public class OrderHub : Hub
{
public async Task NotifyOrderCreated(Order order)
{
await Clients.All.SendAsync("OrderCreated", order);
}
}
Register SignalR in Program.cs
:
csharpbuilder.Services.AddSignalR();
app.MapHub<OrderHub>("/orderHub");
Install Angular CLI and create a new project:
bashnpm install -g @angular/cli
ng new order-management-client
cd order-management-client
Install SignalR client:
bashnpm install @microsoft/signalr
Create an OrderService
:
typescriptimport { Injectable } from '@angular/core';
import * as signalR from '@microsoft/signalr';
@Injectable({
providedIn: 'root',
})
export class OrderService {
private hubConnection: signalR.HubConnection;
constructor() {
this.hubConnection = new signalR.HubConnectionBuilder()
.withUrl('https://localhost:5001/orderHub')
.build();
this.hubConnection.start().catch(err => console.error(err));
}
listenForNewOrders(callback: (order: any) => void) {
this.hubConnection.on('OrderCreated', callback);
}
}