학술논문
If You'Re Already Using
; Code, You Need Not Be Bothered With The Boilerplate;As+You+Can+See+From+The+Preceding+Code">Snippets, Because Refit Can Handle All Of These Tasks With Just A Few Lines Of C# Code.
String BaseAddress = "Http://Localhost:59904/";Var Endpoint = RestService.ForAs You Can See From The Preceding Code; Sections, Refit Can Save Us A Lot Of Time And Effort By Eliminating The Need To Write The Boilerplate Code.
In The Next; Api, We'Ll Implement A Simple Web Api In Asp.Net Core. After We Create Our; 2022, We'Ll Implement A Refit Client To Consume It.
Create An Asp.Net Core Web Api Project In Visual Studio 2022
To Create An Asp.Net Core 8 Web Api Project In Visual Studio;
We'Ll+Use+This+Asp%2ENet+Core+Web+Api+Project+To+Create+Our+Api+In+The+Sections+Below%2E<%2FP>
Create+The+Contact+Model+Class<%2FH3>
Create+A+New+Class+Named+Contact+In+The+Web+Api+Project+You+Just+Created+And+Enter+The+Code+Given+Below%2E<%2FP>Namespace+Refit%5Fdemo{+Public+Class+Contact+{+Public+Int+Id+{+Get%3B+Set%3B+}+Public+String+Firstname+{+Get%3B+Set%3B+}+Public+String+Lastname+{+Get%3B+Set%3B+}+Public+String+Address+{+Get%3B+Set%3B+}+Public+String+Phone+{+Get%3B+Set%3B+}+}}
We'Ll+Use+The+Contact+Class+In+The+Next+Section+To+Work+With+Data%2E<%2FP>
Create+The+Contactrepository">Class
next, " And "Enable Docker" Remain Unchecked. We Won'T Be Using Any Of Those Features Here.
We'Ll Use This Asp.Net Core Web Api Project To Create Our Api In The Sections Below.
Create The Contact Model Class
Create A New Class Named Contact In The Web Api Project You Just Created And Enter The Code Given Below.
Namespace Refit_demo{ Public Class Contact { Public Int Id { Get; Set; } Public String Firstname { Get; Set; } Public String Lastname { Get; Set; } Public String Address { Get; Set; } Public String Phone { Get; Set; } }}We'Ll Use The Contact Class In The Next Section To Work With Data.
Create The Contactrepository; Brevity, We'Ll Create A Repository Class To Work With The Contact Data. For The Sake Of Simplicity And; Methods, We'Ll Store Our Data In A List In Memory. You Can Feel Free To Change This Implementation To Store The Data In A Database As Per Your Requirements. The ContactRepository Class Implements The IContactRepository. This Interface Contains The Declaration Of Two; Namely; Parameter, The GetContact And The GetContacts Methods. While The Former Returns One Contact Record Based On The Id Passed To It As A.; =1, The Latter Returns All Contacts.The Following Code Listing Illustrates Both The IContactRepository Interface And The ContactRepository Class.
Public Interface IContactRepository{ Public Contact GetContact(Int Id); Public List GetContacts();}Public Class ContactRepository: IContactRepository{ Private Readonly List Contacts = New List(); Public ContactRepository() { Contacts = New List() { New Contact() { Id ; "Keaton", FirstName =; "Underwood", LastName =; Road, Address = "12/3 Abc; Chicago; Usa"; "1234567890"}, Phone =; 2, New Contact(){ Id =; "John", FirstName =; "Smith", LastName =; York, New; +C%2EId+==+Id%29%3B+}+Public+List+Getcontacts%28%29+{+Return+Contacts%3B+}}You+Can+Register+An+Instance+Of+Type+Icontactrepository+With+The+Services+Collection+In+The+Program%2ECs+Using+The+Following+Piece+Of">Code.
builder.services.addscopedC => C.Id == Id); } Public List Getcontacts() { Return Contacts; }}You Can Register An Instance Of Type Icontactrepository With The Services Collection In The Program.Cs Using The Following Piece Of; This, Contactrepository>();This Will Enable You To Use Dependency Injection To Create An Instance Of Type Icontactrepository In The Application.
Create The Api Controller
Let Us Now Create The Controller Class For Our Contacts Api. To Do
; >+Get%28%29+{+Return+Await+%5Fcontactrepository%2EGetcontacts%28%29%3B+}+[Httpget%28"{Id}"%29]+Public+Async+Task+Get%28Int+Id%29+{+Return+Await+%5Fcontactrepository%2EGetcontact%28Id%29%3B+}+}}Note+How+We+Have+Used+Constructor+Injection+To+Create+An+Instance+Of+Type+Icontactrepository+In+The+Preceding+Code+Listing%2E<%2FP>
In+The+Next">Sections, Create A New Api Controller Named Contactscontroller And Replace The Generated Code With The Following Code.
Using Microsoft.Aspnetcore.Mvc;Namespace Refit_demo.Controllers{ [Route("Api/[Controller]")] [Apicontroller] Public Class Contactscontroller : Controllerbase { Private Readonly Icontactrepository _contactrepository; Public Contactscontroller(Icontactrepository Contactrepository) { _contactrepository = Contactrepository; } [Httpget] Public Async Task> Get() { Return Await _contactrepository.Getcontacts(); } [Httpget("{Id}")] Public Async Task Get(Int Id) { Return Await _contactrepository.Getcontact(Id); } }}Note How We Have Used Constructor Injection To Create An Instance Of Type Icontactrepository In The Preceding Code Listing.
In The Next; In+The+"Create+New+Project"">Window, We'Ll Create A Console Application Project And Build The Refit Client That Will Consume Our Contacts Api.
Create A .Net Core Console Application Project In Visual Studio
Follow The Steps Outlined Below To Create A New .Net Core Console Application Project In Visual Studio.
Launch The Visual Studio Ide. Click On "Create New Project." In The "Create New Project"; Click+Next%2E<%2FLi> In+The+"Configure+Your+New+Project"">Window, Select "Console App (.Net Core)" From The List Of Templates Displayed. Click Next. In The "Configure Your New Project"; Next, Specify The Name And Location For The New Project. Click Next. In The "Additional Information" Window Shown; Click+Create%2E<%2FLi>We'Ll+Use+This+%2ENet+Core+Console+Application+Project+To+Create+Our+Refit+Api+Client%2E<%2FP>
Install+The+Refit+Nuget+Package<%2FH3>
To+Install+Refit+Into+Your">Project, Choose ".Net 8.0 (Long Term Support)" As The Framework Version You Want To Use.
Click Create. We'Ll Use This .Net Core Console Application Project To Create Our Refit Api Client.
Install The Refit Nuget Package
To Install Refit Into Your; Window, Select The Project In The Solution Explorer; In+The+NuGet+Package+Manager">Window, Then Right-Click And Select "Manage NuGet Packages."
In The NuGet Package Manager; Alternatively, Search For The Refit Package And Install It.; Pm>+Install-Package+Refit<%2FP>
Create+The+Refit+Api+Client<%2FH3>
Now+Replace+The+Generated+Code+In+The+Program%2ECs+File+With+The+Following+Code+Listing%2E<%2FP>Using+Refit%3BString+Baseaddress+=+"Http%3A%2F%2FLocalhost%3A59904%2F"%3BVar+Contactsapi+=+Restservice%2EFor%28Baseaddress%29%3BVar+Contacts+=+Await+Contactsapi%2EGetcontacts%28%29%3BForeach+%28Var+Contact+In+Contacts%29{+Console%2EWriteline%28%24"{Contact%2EId}+|+{Contact%2EFirstname}+|+{Contact%2ELastname}"%29%3B}Console%2EReadline%28%29%3B[Headers%28"Accept%3A">Application/json", You Can Install The Package(S) Via The Nuget Package Manager Console By Entering The Commands Shown Below.
Pm> Install-Package Refit
Create The Refit Api Client
Now Replace The Generated Code In The Program.Cs File With The Following Code Listing.
Using Refit;String Baseaddress = "Http://Localhost:59904/";Var Contactsapi = Restservice.For(Baseaddress);Var Contacts = Await Contactsapi.Getcontacts();Foreach (Var Contact In Contacts){ Console.Writeline($"{Contact.Id} | {Contact.Firstname} | {Contact.Lastname}");}Console.Readline();[Headers("Accept:; +GetContact%28Int+Id%29%3B+[Get%28"%2FApi%2FContacts"%29]+Public+Task>+GetContacts%28%29%3B}Public+Class+Contact{+Public+Int+Id+{+Get%3B+Set%3B+}+Public+String+FirstName+{+Get%3B+Set%3B+}+Public+String+LastName+{+Get%3B+Set%3B+}+Public+String+Address+{+Get%3B+Set%3B+}+Public+String+Phone+{+Get%3B+Set%3B+}}Execute+The+Application<%2FH2>
Since+There+Are+Two+Applications+In+This">Example, "Content-Type: Application/Json")]Public Interface IContactService{ [Get("/Api/Contacts")] Public Task GetContact(Int Id); [Get("/Api/Contacts")] Public Task> GetContacts();}Public Class Contact{ Public Int Id { Get; Set; } Public String FirstName { Get; Set; } Public String LastName { Get; Set; } Public String Address { Get; Set; } Public String Phone { Get; Set; }}Execute The Application
Since There Are Two Applications In This; First, You Should Run Both One By One.; Launched, Run The Api Application Followed By The Api Client Console Application. When Both Applications Have Been; Refit, You'Ll Observe The Data Retrieved From The Contacts Api Application Displayed At The Console As Shown In Figure 1.
Refit Is A Great Choice For Implementing Http Rest Api Clients. Refit Greatly Simplifies The Boilerplate Code Required To Connect To And Work With Rest Apis In Your Asp.Net Core Applications. One Important Point To Note Is That When You'Re Using; Thu, All Requests Must Be Asynchronous. Refit Does Not Support Synchronous Network Calls.
]]>; Kanjilal, 04 Jul 2024 02:00:00 -0700 Joydip
You+Can+Register+An+Instance+Of+Type+Icontactrepository+With+The+Services+Collection+In+The+Program%2ECs+Using+The+Following+Piece+Of">Code.
builder.services.addscopedYou Can Register An Instance Of Type Icontactrepository With The Services Collection In The Program.Cs Using The Following Piece Of; This, Contactrepository>(); This Will Enable You To Use Dependency Injection To Create An Instance Of Type Icontactrepository In The Application. Let Us Now Create The Controller Class For Our Contacts Api. To Do Note+How+We+Have+Used+Constructor+Injection+To+Create+An+Instance+Of+Type+Icontactrepository+In+The+Preceding+Code+Listing%2E<%2FP> In+The+Next">Sections, Create A New Api Controller Named Contactscontroller And Replace The Generated Code With The Following Code. Note How We Have Used Constructor Injection To Create An Instance Of Type Icontactrepository In The Preceding Code Listing. In The Next; Follow The Steps Outlined Below To Create A New .Net Core Console Application Project In Visual Studio. We'Ll+Use+This+%2ENet+Core+Console+Application+Project+To+Create+Our+Refit+Api+Client%2E<%2FP> To+Install+Refit+Into+Your">Project, Choose ".Net 8.0 (Long Term Support)" As The Framework Version You Want To Use. We'Ll Use This .Net Core Console Application Project To Create Our Refit Api Client. To Install Refit Into Your; Window, Select The Project In The Solution Explorer; In+The+NuGet+Package+Manager">Window, Then Right-Click And Select "Manage NuGet Packages." In The NuGet Package Manager; Alternatively, Search For The Refit Package And Install It.; Pm>+Install-Package+Refit<%2FP> Now+Replace+The+Generated+Code+In+The+Program%2ECs+File+With+The+Following+Code+Listing%2E<%2FP>Using+Refit%3BString+Baseaddress+=+"Http%3A%2F%2FLocalhost%3A59904%2F"%3BVar+Contactsapi+=+Restservice%2EFor Pm> Install-Package Refit Now Replace The Generated Code In The Program.Cs File With The Following Code Listing. Since+There+Are+Two+Applications+In+This">Example, "Content-Type: Application/Json")]Public Interface IContactService{ [Get("/Api/Contacts")] Public Task Since There Are Two Applications In This; First, You Should Run Both One By One.; Launched, Run The Api Application Followed By The Api Client Console Application. When Both Applications Have Been; Refit, You'Ll Observe The Data Retrieved From The Contacts Api Application Displayed At The Console As Shown In Figure 1. Refit Is A Great Choice For Implementing Http Rest Api Clients. Refit Greatly Simplifies The Boilerplate Code Required To Connect To And Work With Rest Apis In Your Asp.Net Core Applications. One Important Point To Note Is That When You'Re Using; Create The Api Controller
> Get() { Return Await _contactrepository.Getcontacts(); } [Httpget("{Id}")] Public Async Task
Create A .Net Core Console Application Project In Visual Studio
Install+The+Refit+Nuget+Package<%2FH3>
Install The Refit Nuget Package
Create+The+Refit+Api+Client<%2FH3>
Create The Refit Api Client
>+GetContacts%28%29%3B}Public+Class+Contact{+Public+Int+Id+{+Get%3B+Set%3B+}+Public+String+FirstName+{+Get%3B+Set%3B+}+Public+String+LastName+{+Get%3B+Set%3B+}+Public+String+Address+{+Get%3B+Set%3B+}+Public+String+Phone+{+Get%3B+Set%3B+}}
Execute+The+Application<%2FH2>
> GetContacts();}Public Class Contact{ Public Int Id { Get; Set; } Public String FirstName { Get; Set; } Public String LastName { Get; Set; } Public String Address { Get; Set; } Public String Phone { Get; Set; }}
Execute The Application