DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Choosing a Library to Build a REST API in Java
  • What Is API-First?
  • Building a REST Service That Collects HTML Form Data Using Netbeans, Jersey, Apache Tomcat, and Java
  • Spring Boot - How To Use Native SQL Queries | Restful Web Services

Trending

  • Understanding the Shift: Why Companies Are Migrating From MongoDB to Aerospike Database?
  • How To Build Resilient Microservices Using Circuit Breakers and Retries: A Developer’s Guide To Surviving
  • Analyzing Techniques to Provision Access via IDAM Models During Emergency and Disaster Response
  • Distributed Consensus: Paxos vs. Raft and Modern Implementations
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Using the Refit REST Library With Client-Side Blazor

Using the Refit REST Library With Client-Side Blazor

Let's see the magic of Refit and Blazor working together.

By 
$$anonymous$$ user avatar
$$anonymous$$
·
Sep. 03, 19 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
16.6K Views

Join the DZone community and get the full member experience.

Join For Free

refit

Time to see what a good refit can do.

Refit, a REST library for .NET, takes your API interfaces and dynamically generates a service using HttpClient to make the requests. This cuts out a lot of repetitive code and can be used with client-side Blazor. Here's how.

Refit

Writing code for external API requests is fairly standard and laborious. Create a request, serialize any data to JSON, send the request, deserialize the response

Refit cuts this out. All you have to do is create an interface and let Refit do the rest. Here is an example from the Refit docs.

Create an interface.

public interface IGitHubApi
{
    [Get("/users/{user}")]
    Task<User> GetUser(string user);
}


Generate an implementation using the RestService and call.

var gitHubApi = RestService.For<IGitHubApi>("https://api.github.com");

var octocat = await gitHubApi.GetUser("octocat");


Done.

Using Refit With Client-Side Blazor

I was happy to discover that it is possible to use Refit with client-side Blazor. The HttpClient looks like the standard class used in .NET for HTTP requests and responses but it uses an HttpMessageHandler called WebAssemblyHttpMessageHandler.

All network traffic must go through the browser using the JavaScript fetch API. This handler passes the HTTP request from C# to JavaScript.

You can continue to use HttpClientFactory and create your own typed clients as long as you use the WebAssemblyHttpMessageHandler. To use Refit, I am using the Refit.HttpClientFactory NuGet package. In Startup I can then do this (more info in the docs)

public void ConfigureServices(IServiceCollection services)
{
    services.AddTransient<WebAssemblyHttpMessageHandler>();

    services.AddRefitClient<IBaconService>().ConfigureHttpClient(c =>
            {
                c.BaseAddress = new Uri("https://iambacon.co.uk/api");
            })
            .ConfigurePrimaryHttpMessageHandler<WebAssemblyHttpMessageHandler>();
}


And now Refit in Blazor works!

Summary

As I begin to use Blazor, I am starting to see that it is quite flexible. This is an example of how the HttpClient is still very configurable and that things like HttpClientFactory, Refit, and Polly can still be used even though the requests and responses are going through the Fetch API in the browser.

Further Reading

Blazor Form Validation

Blazor REST Web Protocols Library

Published at DZone with permission of $$anonymous$$, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Choosing a Library to Build a REST API in Java
  • What Is API-First?
  • Building a REST Service That Collects HTML Form Data Using Netbeans, Jersey, Apache Tomcat, and Java
  • Spring Boot - How To Use Native SQL Queries | Restful Web Services

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!