Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@

<PackageReference Update="Castle.Windsor" Version="6.0.0" />

<PackageReference Update="Grpc.Core" Version="2.46.6" />
<PackageReference Update="Grpc.Net.Client" Version="2.71.0" />
Comment on lines +52 to +53
Copy link
Member

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.

</ItemGroup>

</Project>
42 changes: 42 additions & 0 deletions src/Moryx/Grpc/ClientConfig.cs
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)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DefaultValue = true is not a good idea because you never cannot set it to false

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; }
}
66 changes: 66 additions & 0 deletions src/Moryx/Grpc/ClientConfigExtensions.cs
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,
};
}
}
}
5 changes: 4 additions & 1 deletion src/Moryx/Moryx.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The 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'">
Expand Down
Loading