Boilerplate – Layered Architecture Rest API using .Net Core 3.1


I am planning to create a collection of ready-to-start (development) production-ready solutions/repo boilerplates for .Net core 3.1 REST APIs with different architecture and patterns for various use cases and scenarios.

I am starting the mission with a version of layered architecture with a Repository pattern and entity framework core as ORM. I’ve followed the pattern of microservice and creating individual micro-API’s which can be scalable independently.

I still believe there are scenarios for building REST API using the layered architecture and repository pattern, and of course, this has some relevance now as well.

The code is available to download/clone from github here.


The architecture pattern I have followed is the bottom up Presentation Layer -> Business layer -> Data layer , all of the layers are loosely couple using DI container available in .Net Core.

alt text


Rest API : .net core 3. Web API Project.

ORM : EntityFrameworkCore 5.0 is used for ORM.

Unit Test: MS Test is used for unit testing the service/business layer classes and Moq framework is used to mock the data layer.

Validation : Fluent validation is used for the validation. (More about Fluent validation here)

Documentation: The swagger documentation has been used and it is configured using Swashbuckle in the middleware. ( More here)

Data Layer : Data layer is built using the repository pattern.

Authentication : AspNetCore.Authentication.JwtBearer Package is used to introduce JWT token-based authentication and authorization. This can be easily replaced with AAD or any other OAuth-based identity provider by configuring the middleware in start-up and changing the AutheticationService in the service layer. As of now, role-based authorization is also implemented using the JWT token.


Once cloned/downloaded you can run this API in IISExpres, IIS or Kestrel servers.


Please feel free to contribute to the mission. Suggestions, changes, or fixes are most welcomed, please feel free to clone the repo, create a branch and make your changes and submit a pull request.

Printing an rdlc report in a specific printer from an Asp.Net MVC application



Today I am sharing a small method which we have adopted for automatically printing an rdlc file in my Asp.Net MVC3 application upon a button client click.

During the development of an Asp.Net MVC application we had some bottlenecks in the printing of rdlc reports from the views. We were using the client definition (rdlc) of Reporting Service for report creation. But the things were stuck on one scenario where our client required to print the reports automatically by specifying a particular printer name which is presented. Upon clicking a button the reports need to be printed in a particular printer which was configured in the admin section.

After some analysis and googling we came to know that it is not a quick win. Finally we decided to achieve it with acrobat reader and java script in the pdf. (You must have an acrobat reader plugin installed in your browser)
As per our requirement, there is a printer setting where we are setting a particular printer for a particular report by providing the IP address of the system. According to these settings a particular report must print through a printer which is provided in the settings table upon clicking some button in the client.


First we have to design the report using rdlc. The next step is converting the rdlc to pdf from the action method in the controller. Also we need to inject a JavaScript for automatically printing the report in acrobat viewer. For that with the help of  iTextSharp we have converted the rdlc file into pdf at the same time we injected some JavaScript for automatic printing to the pdf.

You need to include the following namespaces which is in iTextSharp DLL
using iTextSharp.text.pdf;

The action method is as follows

public void RecieptPrint(long inv_ReceiptID)
      LocalReport localReport = new LocalReport();
      localReport.ReportPath = @"Reprt1.rdlc";
      DataTable dt = DataSelect();

            The following code is for dynamically setting the data source to rdlc. I already wrote about this here .

     ReportDataSource reportDataSource = new ReportDataSource();
     reportDataSource.Value = dt;
     reportDataSource.Name = "DataSet1";
     string reportType = "PDF";
     string mimeType;                  
     string encoding;
     string fileNameExtension = "pdf";
/* The DeviceInfo settings should be changed based on the reportType, here I have provided the size of the pdf based on my paper requirement. */

string deviceInfo =@”<DeviceInfo>
Warning[] warnings;
string[] streams;
byte[] renderedBytes;

Now we are going to converting the rdlc to byte array using the above device info for pdf .

renderedBytes= localReport.Render(reportType,deviceInfo,out mimeType, out   encoding, out   fileNameExtension,out streams,out warnings);

Here comes the important part of the solution. We are adding this byte array to our pdf file. Also we are injecting a JavaScript code which will execute upon loading the pf in thr reader and trigger the printing.

 var doc = new Document();
 var reader = new PdfReader(renderedBytes);
 using (FileStream fs = new FileStream(Server.MapPath(“~/Summary”+  Convert.ToString(Session[“CurrentUserName“]) + “.pdf“),     FileMode.Create))
     PdfStamper stamper = new PdfStamper(reader, fs);
     string Printer = "Printer_01"
     // This is the script for automatically printing the pdf in     acrobat viewer.
     stamper.JavaScript = “var pp = getPrintParams();
     pp.interactive = pp.constants.interactionLevel.automatic;    
     pp.printerName = ” + Printer + “;print(pp);\r”;

In view using an iframe or on a new window we can render this pdf and it will be printed automatically by executing the injected JavaScript.

Points of Interest

If we saved that pdf instead of printing while opening it will be printed automaticlay

Dynamically setting the Report Data source for rdlc in Report viewer

While using client report definition (rdlc) for reporting in a .net application we need to create a dataset for its datatsource. It may cause a problem of diiferent connections for each report are there in a single application . For avoiding  this we can dynamically set datasource for report during run time.

  1. To begin with we need to add a new rdlc file into the project.
Add a new rdlc file

2. Then add a Dataset for designing the report

Add a dataset

3. To populate the dataset add a table adapter with corresponding stored procedure or a query to fetch the data.


4. After creating the data adapter we can access this in our report definition (rdlc)

5. Once the data set s added, design the report(rdlc) based on your using this dataset . After designing remove this dataset from the project.

6. We need to add  MicrosoftReportViwer into our form/aspx page .

7. Then we can dynamically assign the data source from the data access or business logic in the code as detailed below.

 BLL.Class obj= new BLL.Class();
 // Getting the data 
 DataTable dtTest = obj.SelectData();

 reportViewer1.ProcessingMode = ProcessingMode.Local;
 LocalReport rep = reportViewer1.LocalReport;
 ReportDataSource rds = new ReportDataSource(dtTest);
 rep.ReportEmbeddedResource ="Report1.rdlc";
 rds.Name = "DataSet1_Data";

reportViewer1 : Name of the MicrosoftReportViwer control

We can also pass report parameters to rdlc as follows

//Report parameter
ReportParameter rp = new ReportParameter("content","HELLO");//(Param name,value)
this.reportViewer1.LocalReport.SetParameters(new ReportParameter[] { rp });