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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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
  1. DZone
  2. Data Engineering
  3. Databases
  4. Next Gen API With .NET — Part 1: Hello World

Next Gen API With .NET — Part 1: Hello World

See how to create a next-generation API with .net using Resgate.

Samuel Jirenius user avatar by
Samuel Jirenius
·
Nov. 05, 19 · Tutorial
Like (4)
Save
Tweet
Share
18.05K Views

Join the DZone community and get the full member experience.

Join For Free

Image title

Hello World!

Introduction

Resgate, the tiny real-time API gateway with its 7MB docker image, may not seem like much, but it provides plenty of benefits.

It turns microservices from full-blown HTTP servers to cheap client services. It grants them the power to keep clients updated and synchronized, simplifies architecture, and reduces overhead while remaining as easy to work with as your ordinary REST API.

It uses a service-turned-client approach with NATS Server as the messaging hub, making technologies such as service discovery and separate event streams (eg. SignalR) redundant.

This article series will go through each example found in the ResgateIO.Service library for .NET, starting with a simple Hello World service.

You may also like:  Next Generation RESTful API Management

You will learn how to:

  • ✔️ Install Resgate and NATS
  • ✔️ Create a .NET Core service using ResgateIO.Service
  • ✔️ Access the API with HTTP
  • ✔️ Access the API with WebSocket
  • ✔️ Get updates on resource changes

Preparations

We can install NATS Server and Resgate with 3 docker commands:

docker network create res
docker run -d --name nats -p 4222:4222 --net res nats
docker run --name resgate -p 8080:8080 --net res resgateio/resgate --nats nats://nats:4222

(If you don’t use Docker, there are other ways to install NATS and Resgate.)

Done!

Create a Service

The example can also be downloaded from the GitHub repository.
  • Open Visual Studio 2017 (or your preferred .NET IDE).
  • From the File menu, select New > Project.
  • Select the Console App (.NET Core) template.
  • Name the project HelloWorld and click OK.

Create a new Console App (.NET Core)

Install ResgateIO.Service

  • From the Tools menu, select NuGet Package Manager > Package Manager Console.
  • Run the install command:
    Install-Package ResgateIO.Service
    Or, you can install it the way you are used to.

Write the Service

  • Open Program.cs in Solution Explorer.
  • Enter the following code:
using ResgateIO.Service;
using System;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            ResService service = new ResService("example");
            service.AddHandler("model", new DynamicHandler()
                .Get(r => r.Model(new
                {
                    message = "Hello, World!"
                }))
                .Access(r => r.AccessGranted()));
            service.Serve("nats://127.0.0.1:4222");
            Console.ReadLine();
        }
    }
}

Now, you can build and run the service by pressing F5.

(You did install NATS Server, right?)

Access the API With HTTP

  • Open your favorite browser.
  • Go to: http://localhost:8080/api/example/model
  • You should see:
{"message":"Hello, World!"}

It works!

Access the API With WebSocket

This tutorial does not cover writing the javascript client. Instead we will use the resgate.io - resource viewer tool.

  • Open Chrome and go to: https://resgate.io/viewer/
  • Enter the Resource ID example.model, and click View.

View the model in the resource viewer.

You should see the same message, but this time it is fetched over WebSocket.

Chrome allows websites to connect to localhost, while other browsers may give a security error.

Real-Time Synchronization

Now, this is the interesting part.

Resgate keeps clients synchronized when data changes. Let’s try it out:

  • In Visual Studio, stop the project.
  • In Program.cs, change the greeting message:
    message = "Hello, Resgate!"
  • Rebuild and run the service again by pressing F5.

In the resource viewer, you will see the message updating automatically.

The model message is updated in the resource viewer.
The message updates as soon as the service is restarted. The client is synchronized.

Next Steps

You’ve taken the first step in building next generation APIs using Resgate.

For more information, examples, guides, and live demos, visit Resgate.io.

Further Reading

The Future of API Management

Exploring the Next Gen of Alternative Data With the Coord Shared Vehicle API

API .NET microservice

Published at DZone with permission of Samuel Jirenius. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Problems of Cloud Cost Management: A Socio-Technical Analysis
  • DevSecOps Benefits and Challenges
  • Implementing Infinite Scroll in jOOQ
  • Top Five Tools for AI-based Test Automation

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: