-
Notifications
You must be signed in to change notification settings - Fork 24
Draft: add gRPC extensions an configuration class #542
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| // Copyright (c) 2025, Phoenix Contact GmbH & Co. KG | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text; | ||
| using System.ComponentModel; | ||
|
|
||
| namespace Moryx.Grpc; | ||
|
|
||
| /// <summary> | ||
| /// Configuration for the gRPC client. | ||
| /// </summary> | ||
| public class ClientConfig | ||
| { | ||
| /// <summary> | ||
| /// Host of the gRPC server | ||
| /// </summary> | ||
| /// <example>localhost</example> | ||
| /// <example>127.0.0.1</example> | ||
| [Description("Hostname of the gRPC server"), DefaultValue("localhost")] | ||
| public string Host { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Hostname of the gRPC server | ||
| /// </summary> | ||
| /// <example>50021</example> | ||
| [Description("Port of the gRPC server"), DefaultValue(50021)] | ||
| public int Port { get; set; } | ||
|
|
||
|
|
||
| /// <summary> | ||
| /// Whether to use TLS or not | ||
| /// </summary> | ||
| [Description("Wheter to use TLS or not"), DefaultValue(true)] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| public bool UseTls { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Path to `.cert` file | ||
| /// </summary> | ||
| /// <example>https://localhost:50021</example> | ||
| [Description("Path to `.crt` file, in case of TLS security"), DefaultValue("certificate.crt")] | ||
| public string CertPath { get; set; } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| | ||
| using Grpc.Core; | ||
| using Grpc.Net.Client; | ||
| using System; | ||
| using System.Net.Http; | ||
|
|
||
| namespace Moryx.Grpc | ||
| { | ||
| /// <summary> | ||
| /// Extension methods for ClientConfig | ||
| /// </summary> | ||
| public static class ClientConfigExtensions | ||
| { | ||
| /// <summary> | ||
| /// With a given <paramref name="config"/> creates an Address/URL | ||
| /// </summary> | ||
| /// <param name="config"></param> | ||
| /// <returns></returns> | ||
| public static string CreateAddress(ClientConfig config) | ||
| { | ||
| var uriBuilder = new UriBuilder() | ||
| { | ||
| Host = config.Host, | ||
| Port = config.Port, | ||
| Scheme = config.UseTls ? "https" : "http" | ||
| }; | ||
| return uriBuilder.Uri.ToString(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// WIth a given <paramref name="config"/> creates a channel options | ||
| /// </summary> | ||
| /// <param name="config"></param> | ||
| /// <returns></returns> | ||
| public static GrpcChannelOptions CreateChannelOptions(ClientConfig config) | ||
| { | ||
|
|
||
| if (config.UseTls) | ||
| { | ||
| // Make sure to use the certificate from the provided path | ||
| // Adding it to the (windows) certificates store did not work | ||
| // Maybe this can be improved in the future | ||
| var httpClientHandler = new HttpClientHandler | ||
| { | ||
| ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => | ||
| { | ||
| #if NET8_0_OR_GREATER | ||
| chain!.ChainPolicy.TrustMode = System.Security.Cryptography.X509Certificates.X509ChainTrustMode.CustomRootTrust; | ||
| chain.ChainPolicy.CustomTrustStore.Add(new System.Security.Cryptography.X509Certificates.X509Certificate2(config.CertPath!)); | ||
| #endif | ||
| return chain.Build(cert!); | ||
| } | ||
| }; | ||
| return new GrpcChannelOptions | ||
| { | ||
| HttpHandler = httpClientHandler, | ||
| }; | ||
| } | ||
|
|
||
| return new GrpcChannelOptions | ||
| { | ||
| Credentials = ChannelCredentials.Insecure, | ||
| }; | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,7 +15,10 @@ | |
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Newtonsoft.Json" /> | ||
| <PackageReference Include="Microsoft.Extensions.Logging"/> | ||
| <PackageReference Include="Microsoft.Extensions.Logging" /> | ||
|
|
||
| <PackageReference Include="Grpc.Net.Client" /> | ||
| <PackageReference Include="Grpc.Core" /> | ||
|
Comment on lines
+20
to
+21
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MORYX does not have any package references except dotnet and Newtonsoft (should be replaced by System.Text.Json later). If this should really part of the MORYX-Framework, this should be part of a separate package to keep the dependencies as minimal as possible |
||
| </ItemGroup> | ||
|
|
||
| <ItemGroup Condition="'$(TargetFramework)'=='netstandard2.0'"> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is 2.46/2.71 really necessary, or is 2.0.0 also enough? Let the developers decide which version is necessary.