Fast HTTP Media Streaming Web API built with ASP.NET Core 9 | C#
- Customizable chunk size - Configurable buffer size for streaming
- HTTP byte range support - Full support for HTTP Range requests
- Video player compatibility - Works with almost every modern video player
- SQL Server FileStream backend - Efficient streaming from SQL Server FileStream
- Local buffer fallback - Automatic fallback to local file system when available
- Modern .NET 9 architecture - Built with latest ASP.NET Core practices
- Dependency injection - Fully configured with DI container
- Async/await pattern - Non-blocking I/O operations
- Logging support - Comprehensive logging with structured logging
- Memory caching - Intelligent caching of media metadata
- .NET 9 - Latest .NET runtime
- ASP.NET Core 9 - Modern web framework
- SQL Server - Database with FileStream support
- Microsoft.Data.SqlClient - Modern SQL Server data provider
- IMemoryCache - Built-in memory caching
- .NET 9 SDK or later
- SQL Server with FileStream enabled
- Windows OS (for SQL Server FileStream support)
-
Clone the repository
git clone <repository-url> cd VideoStream
-
Configure the connection string Edit the connection string in appsettings.json:
{ "ConnectionStrings": { "Media": "integrated security=true;Persist Security Info=true;Initial Catalog=dbMedia;Data Source=localhost;TrustServerCertificate=true;" } } -
Set up the database Run the SQL script to create the database and tables:
# Using SQL Server Management Studio (SSMS): # 1. Open SSMS and connect to your SQL Server instance # 2. Open the database-setup.sql file # 3. Execute the script # Or using sqlcmd command line: sqlcmd -S localhost -E -i database-setup.sql
-
Configure video streaming settings Customize settings in appsettings.json:
{ "VideoStream": { "BufferPaths": ["B:\\", "F:\\"], "ReadStreamBufferSize": 262144, "CacheExpirationSeconds": 30, "ExecutionTimeoutSeconds": 600 } } -
Build and run the application
cd src dotnet build dotnet run -
Access the API
- The API will be available at
https://localhost:5001(or the configured port) - Stream endpoint:
GET /api/stream/{id}
- The API will be available at
Since SQL Server FileStream requires Windows Authentication, ensure:
-
Database setup: Your SQL Server has FileStream enabled
-
Table structure: Your
MediaStreamtable should have:id(identifier)FileSize(bigint)FileType(nvarchar)FileExt(nvarchar)FileData(varbinary(max) FILESTREAM)
-
Authentication: Configure Windows Authentication between your app and SQL Server
-
Database Creation: Use the provided SQL script
database-setup.sqlto create the database and tables
The repository includes:
database-setup.sql- Complete database setup script that creates:- Database
dbMediawith FileStream support - Table
MediaStreamwith proper indexes - Trigger for automatic timestamp updates
- Database
GET /api/stream/{id}- Stream media file with optional Range header support- Supports HTTP Range requests for partial content delivery
- Automatically handles MIME type detection based on file extension
- MP3 (audio/mpeg)
- MP4 (video/mp4)
- OGG (application/ogg)
- OGV (video/ogg)
- OGA (audio/ogg)
- WAV (audio/x-wav)
- WebM (video/webm)
- Modern project structure - SDK-style project file
- Dependency injection - Service-based architecture
- Configuration system - JSON-based configuration
- Async patterns - Full async/await support
- Memory management - Improved memory usage and garbage collection
- Cross-platform - Can run on Windows, Linux, and macOS (except for SQL FileStream)
- Moved from
System.WebtoMicrosoft.AspNetCore - Replaced
MemoryCache.DefaultwithIMemoryCache - Updated from
WebConfigurationManagertoIConfiguration - Replaced
ApiControllerwithControllerBase - Updated HTTP client patterns
- Streaming architecture - Direct stream-to-stream copying
- Buffer management - Configurable buffer sizes
- Caching strategy - Metadata caching with TTL
- Async I/O - Non-blocking file operations
- Memory efficiency - Minimal memory footprint for large files
- SQL Server FileStream not working: Ensure FileStream is enabled in SQL Server configuration
- Authentication errors: Verify Windows Authentication is properly configured
- Path not found: Check buffer paths exist and are accessible
- Performance issues: Adjust buffer size and caching settings
The application uses structured logging. Check logs for detailed error information and performance metrics.
Follow these storage rules to ensure consistent and performant media delivery:
- Primary metadata: Use SQL Server (FileStream) for storing all media file metadata and as the final persistent layer for file contents after buffers. The SQL table is the authoritative source of truth for file metadata.
- Buffer priority order: Configure buffers from fastest to slowest (for example: in-memory cache -> local SSD -> network-attached storage -> archival). When searching for media, the service must check buffers in that priority order and return the first match found.
- Fetching strategy: If media is found in any buffer layer, serve it from the buffer. Only when buffers do not contain the media should the service fetch the file from the SQL FileStream layer.
- FileSize field semantics: Store the actual file size in bytes in the SQL table
FileSize. Do not store on-disk allocation size (which varies due to fragmentation, cluster size, or sparse files); use the exact byte length of the logical file content. - Error symptoms for size mismatch: If you see
ERR_HTTP2_PROTOCOL_ERRORin browsers or HTTP 500 responses from the API while streaming media, a common cause is that theFileSizevalue in the database does not match the actual number of bytes returned by the stream. Verify thatFileSizeequals the true byte length of the file.
Practical checklist:
- Ensure
FileSizeis computed from the file byte length before inserting/updating metadata in SQL. - Maintain buffer path ordering in
appsettings.jsonunderVideoStream:BufferPathsfrom fastest to slowest. - Use memory caching for metadata and small files, and persistent buffers for larger files.
- Add validation in upload/ingest workflows to compare computed file bytes vs stored
FileSizeand log mismatches as errors.
This project is licensed under the MIT License.