Learn ASP.NET Core 3.0 : MVC, Razor Pages, Web API, Entity Framework Core, and Blazor.
Microsoft's official documentation can be found here. Looking for professional online training courses? Next weekend batches are starting in November 2019. More details here.

CRUD using gRPC, EF Core, and ASP.NET Core (Part - 4)

In Part -1, Part - 2, and Part -3 of this article you learned to create and consume the EmployeeCRUD service in ASP.NET Core. This concluding part of this series discusses some limitations that you should be aware of while deploying gRPC services with real-world applications.

If you read the official documentation of gRPC available here, you will find the following:

This means you cannot host a gRPC service under:

  • IIS
  • Azure App Service

Why so? Basically Azure App Service also uses IIS for hosting the apps. gRPC uses HTTP/2 protocol. More specifically it uses what is known as response trailing headers. Http.Sys is part of the IIS kernel and as of this writing it doesn't support response trailing headers required for gRPC communication.

 So, what are the options for hosting a gRPC service?

The easiest way is to host a gRPC service in ASP.NET Core's built-in web server - Kestrel. In fact when you create a new gRPC project in Visual Studio it uses Kestrel to run the application.

As you can see, by default the gRPC service is made available at https://localhosy:5001. Note that this URL uses HTTPS and a self-signed certificate is used. You might want to change the port number at which the gRPC service is made available. You can do that in two ways.

You can change the port by adding Kestrel section to appsetttings.json file. In fact if you open appsettings.json you will find this already added for you:

"Kestrel": {
  "EndpointDefaults": {
    "Protocols": "Http2"
  }
}

You can modify this section to customize the URL at which the gRPC service is published.

"Kestrel": {
  "EndpointDefaults": {
    "Protocols": "Http2"
  },
  "Endpoints": {
    "HttpsDefaultCert": {
      "Url": "https://localhost:12345"
    }
  }
}

As you can see, Endpoint section has been added to specify a new end point URL for publishing the gRPC service. If you run the application now, you will see this:

The URL now reflects the changed port number (12345).

You should now use this new URL in the client code to establish the communication channel:

var channel = GrpcChannel.ForAddress("https://localhost:12345");

You can also change the end point URL using code as shown below:

public static IHostBuilder CreateHostBuilder
(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseUrls("https://localhost:12345");
            webBuilder.UseStartup<Startup>();
        });

This code from Program.cs uses the UseUrls() method to change the end point URL.

You might be looking to host your gRPC service in a cloud environment. If so you can use Azure Kubernetes Service (AKS) to host your gRPC services. You can read more about AKS here.

As of this writing there is no specific timeline as to when IIS and Azure App Service will support gRPC. It seems that this will call for kernel level changes in IIS and you can read some discussion around this topic here.

That's it for now! Keep coding!!


Bipin Joshi is an independent software consultant, trainer, author, yoga mentor, and meditation teacher. He has been programming, meditating, and teaching for 24+ years. He conducts instructor-led online training courses in ASP.NET family of technologies for individuals and small groups. He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced the Yoga way of life he also teaches Ajapa Yoga to interested individuals. To know more about him click here.

Get article updates : Facebook  Twitter  LinkedIn

Posted On : 18 November 2019


Tags : ASP.NET ASP.NET Core Data Access MVC C# Visual Studio


Subscribe to our newsletter

Get monthly email updates about new articles, tutorials, code samples, and how-tos getting added to our knowledge base.

  

Receive Weekly Updates