Porticle.Grpc.GuidMapper 1.2.0

Suggested Alternatives

Porticle.Grpc.TypeMapper

Additional Details

Package has bin renamed, because i now supports multiple types, not only Guids

dotnet add package Porticle.Grpc.GuidMapper --version 1.2.0
                    
NuGet\Install-Package Porticle.Grpc.GuidMapper -Version 1.2.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Porticle.Grpc.GuidMapper" Version="1.2.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Porticle.Grpc.GuidMapper" Version="1.2.0" />
                    
Directory.Packages.props
<PackageReference Include="Porticle.Grpc.GuidMapper">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Porticle.Grpc.GuidMapper --version 1.2.0
                    
#r "nuget: Porticle.Grpc.GuidMapper, 1.2.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Porticle.Grpc.GuidMapper@1.2.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Porticle.Grpc.GuidMapper&version=1.2.0
                    
Install as a Cake Addin
#tool nuget:?package=Porticle.Grpc.GuidMapper&version=1.2.0
                    
Install as a Cake Tool

Porticle.Grpc.GuidMapper

A Roslyn-based post-processor for protoc-generated files that adds native Guid, Guid? and string? types in gRPC services.

Build State

Build and Release

Nuget

NuGet Latest Version NuGet Downloads

Overview

This library allows you to automatically convert PROTO string or StringValue fields to C# Guid, Guid? or string? types in protoc-generated files, enabling seamless integration of GUIDs in your gRPC services without manual conversion code.

Installation

Install the package via NuGet:

dotnet add package Porticle.Grpc.GuidMapper

After installing the Package, this Post build step ist dynamically added to your build.

<Project>
    <Target Name="RunProtoPostProcessing" AfterTargets="Protobuf_Compile">
        <ItemGroup>
            <_FilesToPostProcess Include="$(MSBuildProjectDirectory)\%(Protobuf_Compile.OutputDir)\%(Protobuf_Compile.Filename)" />
        </ItemGroup>
        <Message Text="Proto Postprocessing" Importance="high" />
        <Exec Command="dotnet &quot;$(MSBuildThisFileDirectory)..\tools\$(TargetFramework)\Porticle.Grpc.GuidMapper.dll&quot; -- %(_FilesToPostProcess.Identity)" Condition="'@(_FilesToPostProcess)' != ''" />
    </Target>
</Project>

Dont wonder ist you cant se it in your csproj file. It is dynamically added when your build is processed.

Usage

There are three things you can do in your .proto files:

  • Add // [GrpcGuid] as comment to a string field - Converts the corresponding c# string property to Guid
  • Add // [GrpcGuid] as comment to a StringValue field - Converts the corresponding c# string property to Guid?
  • Add // [NullableString] as comment to a StringValue field - Converts the corresponding c# string property to string?

First an Example of a default .proto file

Without GuidMapper

syntax = "proto3";

import "google/protobuf/wrappers.proto";

message User {
  // Guid of the user object   
  string id = 1;

  // Optional parent UserId
  google.protobuf.StringValue optional_parent_user_id = 2;

  // Optional description
  google.protobuf.StringValue description = 3;

  // List of roles 
  repeated string role_ids = 4;
}

Will result in generated code like this, everything is a string

/// <summary>Guid of the user object</summary>
public string Id {
  get { return id_; }
  set { id_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); }
}

/// <summary>Optional Guid of the parent UserId</summary>
public string OptionalParentUserId {
  get { return optionalParentUserId_; }
  set { optionalParentUserId_ = value; }
}

/// <summary>Optional description string</summary>
public string Description {
  get { return description_; }
  set { description_ = value; }
}

/// <summary>List of roles</summary>
public pbc::RepeatedField<string> RoleIds {
  get { return roleIds_; }
}

With GuidMapper

syntax = "proto3";

import "google/protobuf/wrappers.proto";

message User {
  // [GrpcGuid] Guid of the user object   
  string id = 1;

  // [GrpcGuid] Optional Guid of the parent UserId
  google.protobuf.StringValue optional_parent_user_id = 2;

  // [NullableString] Optional description string
  google.protobuf.StringValue description = 3;

  // [GrpcGuid] List of roles 
  repeated string role_ids = 4;
}

Will result in generated code like this, using string? Guid and Guid?

/// <summary>[GrpcGuid] Guid of the user object</summary>
public global::System.Guid Id {
  get { return global::System.Guid.Parse(id_); }
  set { id_ = (value).ToString("D"); }
}

/// <summary>[GrpcGuid] Optional Guid of the parent UserId</summary>
public global::System.Guid? OptionalParentUserId {
  get { if(optionalParentUserId_==null) return default; return global::System.Guid.Parse(optionalParentUserId_); }
  set { optionalParentUserId_ = (value)?.ToString("D"); }
}


#nullable enable
/// <summary>[NullableString] Optional description string</summary>
public string? Description {
  get { return description_; }
  set { description_ = value; }
}
#nullable disable

/// <summary>[GrpcGuid] List of roles</summary>
public IList<Guid> RoleIds {
  get {return new RepeatedFieldGuidWrapper(roleIds_); }
}

What currently is not Possible?

  • Mapping repeated google.protobuf.StringValue to List<Guid?> or List<string?> because grpc internally uses RepeatedField<string> instead of RepeatedField<StringValue>. This may be a bug in protoc compiler, because it is also not possible to add null to repeated google.protobuf.StringValue because ther is a not null check in the Add function in RepeatedField<T>

  • This Tool actually don't works when protoc / Grpc.Tools is compiled with GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.2.0 1,017 7/23/2025 1.2.0 is deprecated because it is no longer maintained.
1.1.4 355 7/20/2025
1.1.0 181 7/17/2025
1.0.16 187 7/13/2025
1.0.15 155 7/13/2025
1.0.14 179 7/13/2025
1.0.13 181 7/7/2025
1.0.12 132 7/13/2025
1.0.11 184 7/7/2025

First test version