Search code examples
c#.netvisual-studiogrpc

Failing to import gRPC namespace in .net program when compiling (CS0246 Error)


So I'm at a point where I have no clue what is going on and I think it may be because I'm not understanding gRPC compilation properly? I'm not great at programming, but I'm trying to create a .net gRPC client program in VS2022 and I've imported the packages from nuget and created a protos/ .proto file which looks like this

syntax = "proto3";

option csharp_namespace = "ThreatForge_ALPHA_Client";

package ThreatForge;


message ConnectRequest {
  string teamServerIP = 1;
  string accessToken = 2;
}

message ConnectResponse {
  bool success = 1;
  string message = 2;
}


service ThreatForgeSettings {
  
  
  rpc ConnectToTeamServer(ConnectRequest) returns (ConnectResponse);
  

}

and if i compile with just the .proto file I do get the corresponding .cs and Grpc.cs files that it should create, but when I try to reference the namespace in a class file I get the cs0246 error.

my class is

using Grpc.Core;
using Grpc.Net.Client;
using System.Threading.Tasks;
using ThreatForge_ALPHA_Client;

namespace ThreatForge_ALPHA
{

    public class Settings_gRPC
    {
        public string TeamServerIP { get; set; }
        public string TeamServerAccessToken { get; set; }
        public string TeamServerStatus { get; private set; }

        private GrpcChannel channel;

        public Settings_gRPC()
        {
            channel = GrpcChannel.ForAddress("https://server-address:port");
        }

        public async Task ConnectToTeamServer()
        {
            var client = new ThreatForgeSettings.ThreatForgeSettingsClient(channel);
            var request = new ConnectRequest
            {
                TeamServerIP = this.TeamServerIP,
                AccessToken = this.TeamServerAccessToken
            };

....

Whats confusing me about this is that as long as I have the import "using ThreatForge_ALPHA_Client;" it shows that its being used and i don't get errors for the "ThreatForgeSettings" and "ConnectRequest" but if i remove it the two items become unknown. So it would appear that they are being seen pre-compilation as when I attempt to compile and run I get the error

Settings-gRPC.cs(8,7,8,31): error CS0246: The type or namespace name 'ThreatForge_ALPHA_Client' could not be found (are you missing a using directive or an assembly reference?)

So I'm thinking that its something about the build process where when I'm building its not finding the proper files but I don't understand enough about the build process to really make any changes.

figure

I've been trying to follow along this tutorial from MS https://learn.microsoft.com/en-us/aspnet/core/tutorials/grpc/grpc-start?view=aspnetcore-7.0&tabs=visual-studio and it would appear I've done everything as they have for the client side of the house yet I'm running into this issue. I've look at the .proto file and it does show the build action as protobuf but doens't show the gRPC items like you get in the .net gRPC server service and i'm not sure if thats the issue? enter image description here

also made sure to add the lines

  <ItemGroup>
    <Protobuf Include="Protos\ThreatForge.proto" GrpcServices="Client" />
  </ItemGroup>

into my csproj file

So really at this point any direction or help would be greatly appreciated.


Solution

  • ok so i figured out that it was indeed an issue with the the way the build was occurring.

    By putting in the line

    <CoreCompileDependsOn>$(CoreCompileDependsOn);Protobuf_Compile</CoreCompileDependsOn>
    

    into my csproj file under the individual property groups for the compiling sections it forces the proto file to be compiled when ran each time. Now everything works as expected.

    an example of what this looks like for those who may also run into this issue is this.

      <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
    <DebugSymbols>true</DebugSymbols>
    <OutputPath>bin\x64\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <DebugType>full</DebugType>
    <PlatformTarget>x64</PlatformTarget>
    <LangVersion>7.3</LangVersion>
    <ErrorReport>prompt</ErrorReport>
    <Prefer32Bit>true</Prefer32Bit>
    <CoreCompileDependsOn>$(CoreCompileDependsOn);Protobuf_Compile</CoreCompileDependsOn>