학술논문

발행년
-
(예 : 2010-2015)
'학술논문' 에서 검색결과 304건 | 목록 1~10
Periodical
Refit, >(Result);}

If You'Re Already UsingCode, You Need Not Be Bothered With The Boilerplate%28BaseAddress%29%3BVar+Contacts+=+Await+Endpoint%2EGetAuthors%28%29%3B

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.For(BaseAddress);Var Contacts = Await Endpoint.GetAuthors();

As 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 NextApi, We'Ll Implement A Simple Web Api In Asp.Net Core. After We Create Our2022, 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

  • In+The+"Create+New+Project"">Window, Follow The Steps Outlined Below.

  • Launch The Visual Studio 2022 Ide.
  • Click On "Create New Project."
  • In The "Create New Project"; 
  • Click+Next%2E<%2FLi>
  • In+The+"Configure+Your+New+Project"">Window, Select "Asp.Net Core Web Api" From The List Of Templates Displayed.
  • Click Next.
  • In The "Configure Your New Project"; Box, Specify The Name And Location For The New Project. Optionally Check The "Place Solution And Project In The Same Directory" Check; Next, Depending On Your Preferences.
  • Click Next.
  • In The "Additional Information" Window Shown; 
  • Elsewhere+In+The+"Additional+Information"">Window, Select ".Net 8.0 (Long Term Support)" As The Framework Version And Ensure That The "Use Controllers" Box Is Checked. We Will Be Using Controllers In This Project.
  • Elsewhere In The "Additional Information"; Support, Leave The "Authentication Type" Set To "None" (The Default) And Ensure The Check Boxes "Enable Open Api; Https, " "Configure For; 
  • Click+Create%2E<%2FLi>

    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.

  • Click Create.
  • 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 AndMethods, 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 TwoNamelyParameter, 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
    Road, Address = "12/3 Abc; ChicagoUsa"; 2, New Contact(){ Id =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.addscoped 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 BeenRefit, 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 UsingThu, All Requests Must Be Asynchronous. Refit Does Not Support Synchronous Network Calls.

    ]]>Kanjilal, 04 Jul 2024 02:00:00 -0700 Joydip

    InfoWorld.com. July 4, 2024
    Conference
    2016 IEEE International Conference on Computer and Information Technology (CIT) CIT Computer and Information Technology (CIT), 2016 IEEE International Conference on. :26-33 Dec, 2016
    Academic Journal
    IEEE Transactions on Intelligent Transportation Systems IEEE Trans. Intell. Transport. Syst. Intelligent Transportation Systems, IEEE Transactions on. 19(7):2290-2300 Jul, 2018
    Report
    ApJL, 848:L12, 2017
    검색 결과 제한하기
    제한된 항목
    [Author] Times S
    발행연도 제한
    -
    학술DB(Database Provider)
    저널명(출판물, Title)
    출판사(Publisher)
    자료유형(Source Type)
    주제어
    언어