When Microsoft started pushing .Net Core they announced they would not be supporting WCF. This caused quite a stir in the Microsoft community so Microsoft relented and added WCF support that became available with the Visual Studio 2017 version 15.5 release.
Nevertheless, Microsoft is still touting the advantages of gRPC rather than WCF for new .Net Core development. Reading through the gRPC documentation I am unhappy at the poor support for Guid, Decimal, DateTime, and TimeSpan types. These are all types we use a lot. If Google can have their own custom Protobuf types, why can't Microsoft?
Visual Studio has a project template of gRPC Service which, like the WCF Service Application project template, contains some example code. Let's see what's in here, and how we would write a client to consume their example service. I'm using Visual Studio 2019 Enterprise version 16.7.4.
Start a new project using the gRPC Service template. Call it HelloService.
Take all the defaults. Once the project has been created you will see a HelloService tab. Close it. You can build and run the service immediately. It is self hosting. There is a console program in program.cs that configures it and launches it. There are other hosting options that I'm not going to get into in this blog entry. You can read the Microsoft introduction to gRPC for WCF developers to understand how it works. Pay special attention to the proto file which replaces the interface that WCF users.Creating the reference.
Now select gRPC and click [Next]. I find it easiest to browse to the HelloService's proto file so click on [Browse] and browse to the proto file.
Back at the 'Add new gRPC service reference" window, click [Finish], then click [Close]. The HelloClient tab should now look like this.
Code Time!
Start by adding a using for Grpc.Net.Client and HelloService. Then add code to create a channel and an instance of the Greeter service. Program.cs looks like this.
namespace HelloClient
class Program
{
static void Main(string[] args)
GrpcChannel channel = GrpcChannel.ForAddress("https://localhost:5001");
}
}
Console.WriteLine(reply.Message);
class Program
{
static void Main(string[] args)
GrpcChannel channel = GrpcChannel.ForAddress("https://localhost:5001");
Console.WriteLine(reply.Message);
}
}
}
As you can see, the consumption of the gRPC service is quite similar to the consumption of a WCF service.PS. A few days after I wrote this blog entry I found this tutorial on microsoft.com which is almost identical.
No comments:
Post a Comment