ตัวอย่าง
ต้องการตรวจสอบการทำงานของ Web API ชื่อ MyWeb
โดยใน API มีการ Insert และ Query ข้อมูลจาก PostgeSQL ผ่าน Entity Framework สิ่งที่ต้องการตรวจสอบ คือ ความเร็วของ API และคำสั่ง SQL ทั้งหมดที่เกิดขึ้น
ขั้นตอน
- สร้างโปรเจคผ่าน Command line
$ dotnet new webapi --language C# --output src/MyWeb
2. ติดตั้ง Package MiniProfiler
และ PostgreSQL
$ dotnet add package MiniProfiler.EntityFrameworkCore
$ dotnet add package MiniProfiler.AspNetCore.Mvc
$ dotnet add pacakge Npgsql.EntityFrameworkCore.PostgreSQL
3. แก้ไขไฟล์ src/MyWeb/Startup.cs
โดยเพิ่ม MiniProfiler
และ DbContext
ดังนี้
4. เพิ่มไฟล์ src/MyWeb/MyContext.cs
สำหรับติดต่อฐานข้อมูล
5. เพิ่ม API src/Controllers/StudentController.cs
สำหรับ Insert และ Query
6. ทดสอบโดยเรียก API ผ่าน curl
$ dotnet run --project src/MyWeb/MyWeb.csproj$ curl http://localhost:5000/api/student/insert
$ curl http://localhost:5000/api/student/query
7. ตรวจสอบการทำงานของ API ด้วย MiniProfiler ผ่าน URL