Skip to content
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

How to use this in a CMake C# project? #2517

Open
bebuch opened this issue Aug 26, 2024 · 2 comments
Open

How to use this in a CMake C# project? #2517

bebuch opened this issue Aug 26, 2024 · 2 comments
Labels
question Further information is requested

Comments

@bebuch
Copy link

bebuch commented Aug 26, 2024

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.

@bebuch bebuch added the question Further information is requested label Aug 26, 2024
@JamesNK
Copy link
Member

JamesNK commented Aug 26, 2024

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.

@bebuch
Copy link
Author

bebuch commented Aug 27, 2024

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:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants