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

后端 未结 2 1898
生来不讨喜
生来不讨喜 2020-12-10 17:55

I followed the instructions at:

https://docs.microsoft.com/en-us/aspnet/core/tutorials/grpc/grpc-start?view=aspnetcore-3.1&tabs=visual-studio

to create a

相关标签:
2条回答
  • 2020-12-10 18:10

    you can configure the .Net core grpc server on insecure through config

    There are 2 ways,

    launchSettings,json

    {
      "profiles": {
        "DemoService": {
          "commandName": "Project",
          "launchBrowser": false,
          "applicationUrl": "http://localhost:5001",
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          }
        }
      }
    }
    

    Or

    appsettings,json

    "Kestrel": {
        "EndpointDefaults": {
          "Protocols": "Http2"
        },
        "EndPoints": {
          "Http": {
            "Url": "http://localhost:5001"
          }
        }
      }
    
    0 讨论(0)
  • 2020-12-10 18:18

    Over SSL or not, you need to turn on Http2 in ASP.NET Core server. So in appsettings.json, do this.

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

    Insecure .NET Framework Client + ASP.NET Core Server

    • ASP.NET Core Server
      1. Remove app.UseHttpsRedirection() and app.UseHsts() in the StartUp class ConfigureServices(IApplicationBuilder app);
      2. Expose the insecure port, typically 80 or 5000 during development.
      3. Use the code below to create insecure channel in .NET Framework client.
    var channel = new Channel("localhost", 5001, secureCredentials);
    

    Secure SSL connection .NET Framework Client + ASP.NET Core Server

    I got it working with SSL port by using the same Server's certificate in .pem format in the client.

    SslCredentials secureCredentials = new SslCredentials(File.ReadAllText("certificate.pem"));
    var channel = new Channel("localhost", 5001, secureCredentials);
    
    

    A bit of explanation. An ASP.NETCore template in VS 2019 uses a development certificate with pfx file at %AppData%\ASP.NET\Https\ProjectName.pfx and password = %AppData%\Microsoft\UserSecrets\{UserSecretsId}\secrets.json {:Kestrel:Certificates:Development:Password} Value You can get the UserSecretsId id from the ProjectName.csproj. This will be different for each ASP.NET Core Project.

    I used the below command to convert the pfx + password combination to a certificate.pem file.

    openssl pkcs12 -in "<DiskLocationOfPfx>\ProjectName.pfx" -out "<TargetLocation>\certifcate.pem" -clcerts
    

    This will prompt for the pfx password. Use the password from the above secrets.json.

    Give some passphrase for the certificate.pem to be generated(At least 4 letter).

    Copy this cerificate.pem for the gRPC .NET Framework client to access and use in

    SslCredentials secureCredentials = new SslCredentials(File.ReadAllText("<DiskLocationTo the Folder>/certificate.pem"))
    var channel = new Channel("localhost", 5001, secureCredentials);
    
    

    Note that port 5001 I used is the SSL port of my ASP.NET Core application.

    For Production Scenarios

    Use a valid certificate from certificate signing authority and use same certificate in ASP.NET Core Server and .NET Framework client as pfx and pem respectively.

    0 讨论(0)
提交回复
热议问题