Saturday, July 25, 2020

Why gRPC Not Replacing REST (Web API)?













Google's gRPC is actually a new take on an old approach known as RPC, or Remote Procedure Call, which has come a long way to stand out among Messaging, Queuing, COM, CORBA, SOAP, REST, and GraphQL in Microservice world.

Performance

gRPC is roughly 7 times faster than REST when receiving data & roughly 10 times faster than REST when sending data for this specific payload. This is mainly due to the tight packing of the Protocol Buffers and the use of HTTP/2 by gRPC. Protobuf is about 3x faster than Jackson and 1.33x faster than DSL-JSON for integer encoding. gRPC uses HTTP/2, it can multiplex multiple RPCs on the same TCP connection. gRPC supports bidirectional streaming. Therefore, gRPC has low latency and high throughput.

For in-depth analysis of gRPC vs REST, please visit https://medium.com/@EmperorRXF/evaluating-performance-of-rest-vs-grpc-1b8bdf0b22da

Interoperability 

gRPC's IDL (Interface Description Language) provides a simpler and more direct way of defining remote procedures than OpenAPI's approach of using URL paths, their parameters, and the HTTP methods that are used with them. It is a cross-platform/multi-language framework. 

Stability

In addition to gRPC is language-neutral, platform-neutral, and extensible, gRPC mitigates Microservice and Distributed Communication pitfalls. gRPC IDL is more stable than REST over time. REST or OpenAPI use HTTP verbs (GET, PUT, POST) and payload (usually JSON). 

.NET Integration

Speaking of .NET, gRPC is very easy to integrate with ASP.NET Core. Please refer C# 8 new feature Async Streming. However all things are shiny. Some .NET data types are not supported. The tooling is behind Web APIs in Visual Studio and Postman. For the UI facing (JavaScript in browser, not nodejs), the services have to be built in Web APIs.

In the end, I have to come to the conclusion – gRPC might be a success in performance critical Microservices but it will not replace REST/Web APIs. 

We will see.

Sunday, July 19, 2020

How to Add SignalR in ASP.NET Core?

SignalR provides an API for creating server-to-client remote procedure calls (RPC). The clients can be JavaScript, .NET, or Java functions. Here are some of the main benefits to use SignalR as push architecture for real time applications.
  1. Handles connection management automatically.
  2. automatically chooses the best transport method that is within the capabilities of the server and client. (WebSocket, Server Sent Event, and Long Poll)
  3. Sends messages to all connected clients simultaneously. For example, a chat room.
  4. Sends messages to specific clients or groups of clients.
  5. Scales to handle increasing traffic.
It's very easy to set up an ASP.NET Core project with SignalR support in Visual Studio 2019 with Web Development tools. Hereunder are the key steps to enable SignalR in a web project with a browser client.

The SignalR server library is included in the ASP.NET Core 3.1 shared framework. The JavaScript client library isn't automatically included in the project. 

In Solution Explorer, right-click  wwwroot/js/ folder, and select Add Client-Side Library. If you don't see it in the context menu, please check if you have VS extension Microsoft Library Manager. 


Only select signalr.js and signalr.min.js.

              

After this you can create JS client now. Here is an example of JS client.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
"use strict";

var connection = new signalR.HubConnectionBuilder().withUrl("/chatHub").build();

//Disable send button until connection is established
document.getElementById("sendButton").disabled = true;

connection.on("ReceiveMessage", function (user, message) {
    var msg = message.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
    var encodedMsg = user + " says " + msg;
    var li = document.createElement("li");
    li.textContent = encodedMsg;
    document.getElementById("messagesList").appendChild(li);
});

connection.start().then(function () {
    document.getElementById("sendButton").disabled = false;
}).catch(function (err) {
    return console.error(err.toString());
});

document.getElementById("sendButton").addEventListener("click", function (event) {
    var user = document.getElementById("userInput").value;
    var message = document.getElementById("messageInput").value;
    connection.invoke("SendMessage", user, message).catch(function (err) {
        return console.error(err.toString());
    });
    event.preventDefault();
});

For more details to use SignalR in ASP.NET Core 3.1, please refer to the link below.



Saturday, July 11, 2020

What is C# 8 Async Stream?

Asynchronous stream is a new feature introduced in .NET Standard 2.1 or C# 8. It is based on interface – IAsyncEnumerable<T>. It can be used with async/awaitThe main benefit with Async Stream is that the it can shorten the response time and reduce the usage of computing resource. The client (or the consumer) will get the a "stream" of items asynchronously, therefore act on each one item as soon as it is produced. 

Here is an example in my GitHub:

Tuesday, July 07, 2020

What's New in C# 8?

One Minute Answer: 

C# 8 was released in September 2019. It has come with a lot of good features.
  • Default Interface Methods
  • Property Pattern
  • Type Pattern
  • Tuple Pattern
  • Using declaration
  • Null-coalescing assignment
  • Indices and ranges *
  • Nullable reference types *
  • Asynchronous streams *

Add On Answer: 

  • C# 8 is fully supported in .NET Core 3.0 and .NET Standard 2.1
  • C# 8 is partially supported in .NET Frameworks 4.8 (*). But it will be fully supported in the up coming .NET 5.
  • C# 8 compiler exists Visual Studio 2019 v16.3 and above





Be A Developer That Uses AI

Developers will not be replaced by AI, they'll be replaced by developers that use AI. Generative AI tools are revolutionizing the way de...