A simple application to import csv file and store as Json and also in a SQL Database.
The application is composed of following components.
The API
component is responsible for uploading the csv
file and storing it at a temporary location for further processing
This component is responsible for transforming the csv file into JSON and store on disk.
This component is responsible for transforming the csv file and storing in a Database.
Hangfire server is responsible for running background scheduled jobs to read csv files from storage and process them accordingly.
- Visual Studio 2019 (v16.8.3)
- SQL Server 2016
- .NET 5.0
Open appsettings.json
and configure the following settings
"ConnectionStrings": {
"Default": "Data Source=.;Initial Catalog=db;Integrated Security=True"
},
Configure storage locations for each job
"JsonJobConfig": {
"SourcePath": "D:\\JsonJob\\Src",
"ProcessingPath": "D:\\JsonJob\\Processing",
"FailedPath": "D:\\JsonJob\\Failed",
"ProcessedPath": "D:\\JsonJob\\Processed"
},
"DbJobConfig": {
"SourcePath": "D:\\DbJob\\Src",
"ProcessingPath": "D:\\DbJob\\Processing",
"FailedPath": "D:\\DbJob\\Failed",
"ProcessedPath": "D:\\JsonJob\\Processed"
},
SourcePath
is the location whereapi
will store the uploadedcsv
fileProcessingPath
is the location to store files which are being processedFailedPath
is the location where files which couldn't be processed will be storedProcessedPath
is store the processed files
To run the application in standalone mode please simply run the API project and upload the csv
file from swagger
.
FileUploadController
receives the request and delegates toIFileUploadService
IFileUploadService
copies the file toSourcePath
folder for each transformerIMediator
publishes a notification of typeFileUploadedEvent
DbTransformer
andJsonTransformer
listen to the above published notification and start processing the uploaded file
To run the application with Hangfire server follow below steps.
- Comment below code from
FileUploadController
to stop publishing notification
await _mediator.Publish(new FileUploadedEvent
{
FileName = filename
});
- Set both
Data.Transformer.Api
andDataTransformer.Hangfire.Server
projects as startup projects and run both - Upload
csv
file fromSwagger
- Two Hangfire background services are scheduled to run every 30 minutes, but can be triggered manually from hangfire dashboard, jobs can be triggered by going to
https://localhost:44364/hangfire/recurring
- Trigger
JsonTransformationJob
manually and once the processing is finished, the processed Json file should be available atProcessedPath
forJsonJobConfig
- Trigger
SqlTransformationJob
manually and after processing is finished, records should be available in theProducts
table of database.
- Monitoring for files i.e.
pending
,processing
,processed
orfailed
- Retry logic with
Polly
(or any) to try processing the file for few times before moving to Failed folder.