.Net CoreDevelopment

Experimenting with gRPC in .NET Core.

Modern applications are distributed, loose-coupled and service oriented. Decoupled systems perform and scale better, maintenance is far easier (by reducing complexity into smaller more maintainable parts). Communication system that glue together these decoupled parts is important part of the system.

Windows Communication Foundation (WCF) has always been de-facto framework for communication between decoupled systems/processes in .NET ecosystem. But, with .Net Core this changed, because Microsoft announced that it will not support WCF in .NET Core.

WCF and .NET Core

There has been much debate about this topic, whether this communication stack should be included in .NET Core or not . Microsoft, as a main developer of .NET Core (technically speaking it’s under MIT license and therefore open source) announced that they will not include WCF into .NET Core (https://devblogs.microsoft.com/dotnet/supporting-the-community-with-wf-and-wcf-oss-projects/).

In my opinion, this is rather bold decision, because, since introduction of WCF in .Net Framework 3.0, WCF infiltrated almost everywhere in .NET app portfolio. Especially, this is true in enterprise segment. Hence, I can not even imagine enterprise ready app without WCF. Yes, there are “alternatives”, e.g. RESTful services, but in enterprise environments WCF excelled because of (just to name a few) these features:

  • Security: RestApi rely on web security (in general, HTTPS transport, with standard web auth protocols), but WCF with some helpers extends security also on enterprise level (transport & message security, enterprise grade authentication systems, auditing, access control, Windows integrated security, …).
  • Interoperability, Integration & Versatility: with WCF, enterprises connected all sorts of systems, from simple HTTP services to very complex full duplex binary communication systems in intranet and internet. Can connect COM/COM+, .NET Remoting, POX apps, other SOAP based services, etc….
  • Transactions, Reliable Messaging: Support for higher level protocols e.g. Reliable Messaging, Transactions on top of WS-*.

But on the other hand, Microsoft’s decision not to support WCF in .NET Core, does not surprise me due to these facts:

  • WCF is to much coupled with Windows platform. The idea of .NET Core to support at least Windows, Linux and MacOS causes a problem.
  • WCF is highly configurable, which is generally a good thing, but it’s also hard to orchestrate all those settings to work well together. For me, putting all these parameters right was always a headache!
  • Micro – lightweight and very fast (alternatives) communication frameworks emerged in last few years.
  • Restfull Services become de-facto standard for process/service communication!

As said, Microsoft will not directly support WCF in .NET Core. There are some initiatives to support WCF in .Net Core, but, I think WCF will slowly become obsolete technology and being replaced with other modern technologies. One of them could be gRPC.

gRPC

gRPC ( general-purpose RPC – Remote Procedure Call) is a modern open source high performance, lightweight communication framework designed for making traditional RPC framework that can run in any environment. It can efficiently connect services in and across data centers with pluggable support for load balancing, tracing, health checking and authentication. It is also applicable in last mile of distributed computing to connect devices, mobile applications and browsers to backend services.

gRPC has simple service definitions (using Protocol Buffer), therefore can be easily used in different languages and platforms. It scales well and can be used for simple case to very complex scenarios.

It’s good to know how C# gRPC really works. C# application calls underlying C# Stubs. These calls pass through interceptors, and via C# wrapping library where the calls are translated into C calls. The gRPC C-core will encode the RPC as HTTP/2, optionally encrypt the data with TLS, and then write it to the network.

Reference: https://grpc.io/blog/grpc-stacks/

Simple example of gRPC in .NET Core.

I wanted to check how gRPC and .NET play together. For that purpose, as I usually do, I prepared simple “big button” project and check basic scenario!

To get started, I quickly checked documentation at: https://grpc.io/docs/quickstart/csharp/ and dig into code.

Service definition and contract library

I created empty class library project in my solution. My project targeted .NET Core 3.0 (Preview). I put in following NuGet dependencies:

Then, I added new file named ServiceDefinition.proto containing simple service and data contract/message definition:

Next, I set up Build Action to Protobuf Compiler, set properties to tell Protobuf compiler what type of stubs to generate: client only, server only, client and server, none.

Setting protobuf compiler in Visual Studio 2019

Protobuf compiler basically picks gRPC proto service definition file, generates C# stub classes to be later compiled with C# compiler. Intermediate file looks like this, an can be found in obj folder after compilation.

So, everything compiled. Next step was to create simple client and server app for “both side of the wire”. For that, I created WPF apps (well, WPF is new player in .NET Core 3.0 and I just wanna play with it) . But in general, it does not matter where to host app, it can be console, winforms (also new .Net Core 3.0 player), asp.net web runtime….

Server

Server listens to request and return with response messages. My server side code very elementary and very simple!

In general, I created instance of Grpc.Core.Server and defined services and ports where server is listening. When this is set, .Start() method is invoked in order to start listening for incoming requests.

The important part was to implement abstraction class JenxSimpleGrpcService.JenxSimpleGrpcServiceBase, like:

Client

Client just connect to server and send message, the code is:

Demo

Conclusion.

gRPC looks promising technology to replace or to partially substitute WCF in .NET Core. gRPC is inline with .NET Core philosophy: it’s fast, cross platform, lightweight, easy to use, mobile & cloud enabled.

Initially developed by Google, under Cloud Native Computing Foundation (https://www.cncf.io), used by big players, like: Square, Netflix, CoreOS, Docker, CockroachDB, Cisco this technology has definitively bright future.

In this blog post I showed simple usage of gRPC with C# and .NET Core 3 (Preview 4).

You can download code for this experiment here: https://github.com/josipx/Jenx.Grpc.SimpleDemo

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.