grpc/grpc-dotnet

How to use this in a CMake C# project?

bebuch opened this issue · 2 comments

I need to write a simple gRPC server for my Proto file in C#. Unfortunately, I have very little experience with C#. I am a C++ developer.

For internal project reasons, I would like to use CMake. What should a minimal project look like in which a gRPC server is implemented with grpc-dotnet?

It would be great if you could add a CMake project to the examples directory.

My current state is:

cmake_minimum_required(VERSION 3.11)

set(CMAKE_GENERATOR_PLATFORM Win32)
set(CMAKE_DOTNET_SDK "Microsoft.NET.Sdk")
set(CMAKE_DOTNET_TARGET_FRAMEWORK "net8.0")
set(CMAKE_DOTNET_TARGET_FRAMEWORK_VERSION "v4.8")

project(AppName LANGUAGES CSharp)

file(GLOB SOURCE_FILES "*.cs")
add_executable(App ${SOURCE_FILES})
set_target_properties(App PROPERTIES VS_DOTNET_REFERENCE_Grpc_AspNetCore "Grpc.AspNetCore")

This enables me to create a VS Solution the I can open with Visual Studio. There I need to install Grpc.AspNetCore via right click on the project / Manage NuGet Packages.

I didn't find out yet how to add the Proto file.

You'll need to call protoc manually to generate CS files and then include those generate files.

Grpc.Tools is based around msbuild and integrates protoc into an msbuild compilation.

Thanks for the answer!

I have now found a working workflow. I add the Proto file to my executable, so I can see it in the Project:

cmake_minimum_required(VERSION 3.11)

set(CMAKE_GENERATOR_PLATFORM Win32)
set(CMAKE_DOTNET_SDK "Microsoft.NET.Sdk")
set(CMAKE_DOTNET_TARGET_FRAMEWORK "net8.0")
set(CMAKE_DOTNET_TARGET_FRAMEWORK_VERSION "v4.8")

project(AppName LANGUAGES CSharp)

file(GLOB SOURCE_FILES "*.cs" "Interface.proto")
add_executable(App ${SOURCE_FILES})
set_target_properties(App PROPERTIES VS_DOTNET_REFERENCE_Grpc_AspNetCore "Grpc.AspNetCore")

This generates a .csproj project with:

    <None Include="C:\path\to\Interface.proto">
      <Link>Interface.proto</Link>
    </None>

I replace this in the text editor with:

    <Protobuf Include="Interface.proto" GrpcServices="ServiceNameInProtoFile" Link="Interface.proto" />

(Note: My Interface.proto is located on the build directory.)

Then I open Visual Studio. Right click on the project. Manage NuGet Packages. Install Grpc.AspNetCore.

Then build.


It probably needs an extension of CMake to make the proto file not a None but a Protobuf XML node.

I have created a corresponding issue in CMake: