Introduction
This article presents a discussion on how we can capture method execution metadata such as execution time, method name, controller name, etc, and save these details in an Oracle database. The database used is Oracle and Entity Developer is used to connect to the database.
Pre-requisites
You’ll need the following tools to deal with code examples:
- Visual Studio 2019 Community Edition (download)
- Oracle Express Edition (download)
- dotConnect for Oracle (download)
Create a new ASP.NET Core Web API Project
Earlier, we mentioned the necessary tools to proceed to the practical scenarios. The time has come to use those tools.
First, we need to create a new ASP.NET Core Web API project,
- Open Visual Studio 2019.
- Click Create a new project.
- Select ASP.NET Core Web Application and click Next.
- Specify the project name and location to store that project in your system. Optionally, checkmark the Place solution and project in the same directory checkbox.
- Click Create.
- In the Create a new ASP.NET Core Web Application window, select API as the project template.
- Select ASP.NET Core 5 or later as the version.
- Disable the Configure for HTTPS and Enable Docker Support options (uncheck them).
- Since we won’t use authentication in this example, specify authentication as No Authentication.
- Click Create to finish the process.
Now, create a new API controller named DefaultController. Replace the default generated code of the controller class with the following code:
We’ll use this project in the subsequent sections of this article.
Install NuGet Packages
To get started you should install the dotConnect for Oracle package in your project. You can install it either from the NuGet Package Manager tool inside Visual Studio or, from the NuGet Package Manager console using the following command:
If the installation is successful, you're all set to get started using dotConnect for Oracle.
Retrieving Execution Metadata from an Action Method
In this section, we’ll examine how we can retrieve method execution metadata of an action method in ASP.NET Core.
Create an Action Filter
Create a new action filter class named MethodExecutionMetadataFilter by extending the ActionFilterAttribute class. Replace the default generated code with the following code:
There are two overridden methods in the MethodExecutionMetadata class, OnActionExecuting and OnActionExecuted. While the former is executed before an action method is executed, the latter is executed once the execution of an action method is over. The StopWatch is started in the OnActionExecuting method, and the elapsed time is retrieved from the ElaspedMilliseconds property and then stored in the response header using the header key "Execution Time: ".
When you execute the application from Postman, here’s how the output will look like,
Figure 1
Let’s now retrieve the controller and method names of the currently running action method. Create a class named MethodExecutionMetadata in a file having the same name and write the following code there:
Now replace the existing source code of the MethodExecutionMetadataFilter with the following code:
Create Database Table
Use the following script to create a new database table in Oracle.
We’ll use this table to store method execution metadata of our action method.
Creating an OracleConnection
Now provide the Oracle database credentials in your application to establish a connection to the database. You can save this information configurable by storing in the application’s config file as well.
The code snippet given below illustrates how you can create an instance of OracleConnection:
You should include the following namespace in your program:
Persist Method Execution Metadata in Oracle Database Table
The following code listing illustrates how you can save method execution metadata in a database table in Oracle:
The above code should be written inside the OnMethodExecuted method of our action filter class. The following is the complete source code of the action filter class:
Apply the MethodExecutionTimeFilter
You can apply the MethodExecutionTimeFilter to our controllers or the action methods of our controller classes. The following code snippet illustrates how this filter can be applied at the controller level:
Summary
This article discussed how to capture and store method execution metadata in an ASP.NET Core application. You can use this knowledge to capture and store more detailed information of your action methods.
No comments:
Post a Comment