4

I have created a gRPC service in .NET Core which needs to be used from a legacy WPF application running on .NET 4.7.2. The existing WPF application is huge and can't be converted immediately to .NET Core and the effort isn't worth it just to use gRPC. My question is, is there a way to use the gRPC service from .NET 4.7.2 WPF?

Ronnie Rahman
  • 316
  • 3
  • 9
  • Are you using Grpc.Core or grpc-dotnet? Only the former is compatible with the .NET Framework. – mm8 Apr 14 '20 at 12:50
  • 1
    I am using gRPC Core. It works fine. Cannot access the services from WPF using .NET 4.7.2. – Ronnie Rahman Apr 15 '20 at 10:56
  • Are you just wanting to communicate, as a client, to an existing gRPC service from a .NET Desktop WPF app? I do that, but all my gRPC services are not .NET core yet. Is that the issue? – Rob Goodwin Apr 15 '20 at 21:32
  • That's correct Rob. That's precisely what I want to do. How can you access the gRPC services from WPF? I can't add the proto files to WPF due to their differences in the .NET. Is there a way to consume the services from WPF? – Ronnie Rahman Apr 16 '20 at 12:43

2 Answers2

8

I finally got a working solution for this problem with:

  • Asp .NET Core 3.1 Server (using the new grpc-dotnet package) and
  • .NET Framework 4.7.2 WPF-Client (using old C wrapper grpc package)

The main problem was to find a solution to accept a self-signed SSL server certificate from a remote client, which is a mandatory for our scenario.

The server gets a generated certificate using a solution like provided here (solution also works with any valid certificate): https://gist.github.com/mivano/356d4f0354d997370e3c2e62809cdeef

  • adjusted Subject/FriendlyName to something more meaningful
  • adjusted DnsName to the IP or Hostname of the server (which is used by the clients)
  • adjusted NotAfter to desired end date
  • adjusted $pfxPassword

Important thing to mention here: the DNS or IP of the server is verified by the client so it has to be part of the certificate.

gRPC Server was configured this way (could also be achieved through .appsettings.json):

 webBuilder.ConfigureKestrel(
    options =>
    {
        options.Listen(
            IPAddress.Any,
            <your port>,
            listenOptions =>
            {
                listenOptions.UseHttps("<your.pfx path>", "<your passphrase>");
                listenOptions.Protocols = HttpProtocols.Http2;
            });
    });

gRPC Client:

  • Create a .pem File from your .pfx (using openssl):

openssl pkcs12 -in "<pfx path>.pfx" -out "<pem path>.pem" -clcerts

How do you create a gRPC client in .NET Framework?

  • read the .pem File in your client and use it for the gRPC channel:

Channel:

var channelCredentials = new SslCredentials(
    File.ReadAllText("<path to pem>.pem"), null, verifyPeerCallback => true);
var serviceChannel = new Channel("<DnsName from cert", <port>, channelCredentials);
var serviceProxy = new GrpcService.GrpcServiceClient(serviceChannel );

The client can also be implemented to dynamically download the certificate from the server using a regualar HttpClient.Get with a proper HttpClientHandler and attached ServerCertifacteCustomValidationCallback. The pem has to be created in memory before the service channel creation: https://github.com/grpc/grpc/issues/8978#issuecomment-283469676

JanW
  • 1,799
  • 13
  • 23
  • Got this working in .net fw by adding the CA.pem to the SslCredentials like shown here. Thanks JanW! – sevenam Apr 21 '23 at 06:13
0

As mm8 stated, Grpc.Core is the predecessor to the grpc that is baked into .Net Core grpc-donet. In general they are very similar.

I just went thru this process of converting a medium sized WPF Framework app to net core with grpc replacing out WCF. It has been a learning curve but the issues were not in the WPF area as much as trying to produce a standard way of passing data the way I wanted.

If there is interest I can produce a small book on my solution.

Jeff
  • 2,061
  • 4
  • 27
  • 45
  • Thank you Jeff for your comment. The app in concern took 10 years to develop by a team of 10+ developers so you can imagine that it would be impractical to convert it to .NET Core right now. However, I am more interested to know if I can consume the service exposed by gRPC from .NET 4.7.2? Is there a workaround? The gRPC documentation has been shy of mentioning anything that marries .NET Core and .NET Framework. – Ronnie Rahman Apr 15 '20 at 10:59
  • Can you make a .NET core service that consumes gRPC and then your .NET Framework service consumes this .NET core service? – Trent Sep 23 '20 at 17:06