Developing a Microservice using ASP.NET with MySQL
Creating an ASP.NET Core Application Solution
- Open Visual Studio.
- On the start window, choose to create a new project.
3. On the Create a new project window, enter or type ASP.NET in the search box. Next, choose C# from the Language list, and then choose Windows from the Platform list.
4. After you apply the language and platform filters, choose the ASP.NET Core Web Application template, and then choose Next.
5. In the Configure your new project window, type or enter SampleMicroservice in the Project name box. Then, choose Create
6. Next, choose API as the type of the project and make sure that the “Enable Docker Support” option is selected with OS type as Linux.
7. The solution will look as shown below.
Adding Models
- Add a new folder named “Model” to the project.
2. In the Models folder, add a class named user
3. Choose the class and enter the class name, then click the add button
4. Add a few properties like user Id, log_id, first_name, last_name, age, role_id to the user class
Adding EF Core DbContext
A database context is needed so that the models could interact with the database.
- Add a new folder named DBContexts to the project.
2. Add a new class named userContext which includes the DbSet properties
3. Install Microsoft entityframework core
4. Add DbContext and entity to table
5. Add a connection string in the appsettings.json file.
6. Open the Startup.cs file to add the MYSQL db provider for EF Core. Add the code services.AddEntityFrameworkMySQL().AddDbContext<userContext>(options =>{options.UseMySQL(Configuration.GetConnectionString (“DefaultConnection”));});under ConfigureServices method. Note that in the GetConnectionString method the name of the key of the connection string is passed that was added in appsettings file.
Install Mysql data EnitityFrameWorkcore
Adding Repository
- Add a new folder named Repository in the project and add an Interface name IUserRepository in that folder. Add the methods in the interface that performs CRUD operations for User microservice.
2. Add a new concrete class named UserRepository in the same Repository folder that implements IUserRepository. All these methods need implementation.
Open the Startup class in the project and add the code as services.AddScoped<IUserRepository, UserRepository>(); inside the ConfigureServices method so that the repository’s dependency is resolved at run time when needed.
Adding Controller
- The microservice should have an endpoint for which a controller is needed, which exposes the HTTP methods to the client as endpoints of the service methods.
- Right-click on the Controllers folder and add a new Controller as shown below:
Step -1
Step -2
Step -3
Step -4
A controller can be deleted as it is not needed
Step -5
Run the Microservice
The service could be run via IIS Express i.e. Visual Studio default
Choose IIS Express in the Visual Studio as shown below and press F5 or click that IIS Express button itself
Step -6
GET
Perform a GET request now with the records are shown as a JSON result response.
Thanks for reading my story, Happy learning :)