Search code examples
c#visual-studioasp.net-coreprotocol-buffers

Class not found when using .proto file in Visual Studio 2022 and writing C# service with improper class name. How can I fix the issue?


I wrote a a protobuf file in my C# ASP.NET Core gRPC Service project, trying to follow the example, and now I need to write the code for it, but it doesn't seem to be able to find the class name. I edited the .csproject file as many others have suggested to add the path to the .proto file, but it still doesn't seem to be working. How can I fix the problem and compile the service?

This error is being produced: CS0246 The type or namespace name 'Database' could not be found (are you missing a using directive or an assembly reference?)

Below is the .proto file, Protos\database.proto, which I wrote:

syntax = "proto3";

option csharp_namespace = "LiteDistCoordinator.Protos";

enum DataType {
  NULL = 0;
  INTEGER = 1;
  REAL = 2;
  TEXT = 3;
  BLOB = 4;
}

message Table {
  int64 columns = 1;
  int64 rows = 2;
  repeated DataType column_data_types = 3;
  repeated Value data = 4;
}

service DatabaseService {
  rpc CreateTable(CreateTableRequest) returns (NonQueryResponse);
  rpc InsertInto(InsertIntoRequest) returns (NonQueryResponse);
  rpc UpdateAll(UpdateAllRequest) returns (NonQueryResponse);
  rpc UpdateWhere(UpdateWhereRequest) returns (NonQueryResponse);
  rpc SelectFrom(SelectFromRequest) returns (QueryResponse);
  rpc SelectAllFrom(SelectAllFromRequest) returns (QueryResponse);
  rpc SelectFromWhere(SelectFromWhereRequest) returns (QueryResponse);
  rpc SelectAllFromWhere(SelectAllFromWhereRequest) returns (QueryResponse);
  rpc DeleteFrom(DeleteFromRequest) returns (NonQueryResponse);
  rpc DeleteAllFrom(DeleteAllFromRequest) returns (NonQueryResponse);
  rpc DropTable(DropTableRequest) returns (NonQueryResponse);
}

message NonQueryResponse {
  int32 status_code = 1;
  string error_message = 2;
}

message QueryResponse {
    int32 status_code = 1;
    string error_message = 2;
    Table table_data = 3;
}

message CreateTableRequest {
  string table_name = 1;
  repeated ColumnDefinition columns = 2;
}

message InsertIntoRequest {
  string table_name = 1;
  repeated string columns = 2;
  repeated ColumnValuePair values = 3;
}

message ColumnDefinition {
  string name = 1;
  DataType type = 2;
}

message UpdateAllRequest {
  string table_name = 1;
  repeated ColumnValuePair new_values = 2; 
}

message UpdateWhereRequest {
  string table_name = 1;
  repeated ColumnValuePair new_values = 2; 
  string conditions = 3; 
}

message SelectFromRequest {
  string table_name = 1;
  repeated string columns = 2; 
}

message SelectAllFromRequest {
  string table_name = 1;
}

message SelectFromWhereRequest {
  string table_name = 1;
  repeated string columns = 2; 
  string conditions = 3; 
}

message SelectAllFromWhereRequest {
  string table_name = 1;
  string conditions = 2; 
}

message DeleteFromRequest {
  string table_name = 1;
  string conditions = 2; 
}

message DeleteAllFromRequest {
  string table_name = 1;
}

message DropTableRequest {
  string table_name = 1;
}

message Value {
  oneof value_type {
    string string_value = 1;
    int64 int_value = 2;
    double real_value = 3;
    bytes blob_value = 4;
    NullValue null_value = 5; 
  }
}

message ColumnValuePair {
  string column = 1; 
  Value value = 2;   
}

message NullValue {}

Below is the C# code, Services\DatabaseService.cs, where the error occurs:

using Grpc.Core;
using LiteDistCoordinator;

namespace LiteDistCoordinator.Services
{
    public class DatabaseService : Database.DatabaseBase    // PROBLEM HERE: the class name "Database" has a red squiggly under it
    {
        private readonly ILogger<DatabaseService> _logger;
        public DatabaseService(ILogger<DatabaseService> logger)
        {
            _logger = logger;
        }

        // code not written yet...
    }
}

Here is the .csproj file:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

  <ItemGroup>
    <Protobuf Include="Protos\greet.proto" GrpcServices="Server" />
    <Protobuf Include="Protos\testlist.proto" GrpcServices="Server" />
    <Protobuf Include="Protos\database.proto" GrpcServices="Server" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Grpc.AspNetCore" Version="2.57.0" />
    <PackageReference Include="Grpc.Tools" Version="2.63.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
  </ItemGroup>

</Project>

Here is the directory tree of my project file in case you need to know:

C:\USERS\CUTEK\SOURCE\REPOS\LITEDIST\LITEDISTCOORDINATOR
│  appsettings.Development.json
│  appsettings.json
│  LiteDistCoordinator.csproj
│  LiteDistCoordinator.csproj.user
│  Program.cs
│
├─bin
│  └─Debug
│      └─net8.0
├─obj
│  │  LiteDistCoordinator.csproj.nuget.dgspec.json
│  │  LiteDistCoordinator.csproj.nuget.g.props
│  │  LiteDistCoordinator.csproj.nuget.g.targets
│  │  project.assets.json
│  │  project.nuget.cache
│  │
│  └─Debug
│      └─net8.0
│          │  .NETCoreApp,Version=v8.0.AssemblyAttributes.cs
│          │  12fbc7ffe642ca7e_database.protodep
│          │  12fbc7ffe642ca7e_greet.protodep
│          │  12fbc7ffe642ca7e_testlist.protodep
│          │  apphost.exe
│          │  LiteDistCoordinator.AssemblyInfo.cs
│          │  LiteDistCoordinator.AssemblyInfoInputs.cache
│          │  LiteDistCoordinator.assets.cache
│          │  LiteDistCoordinator.csproj.AssemblyReference.cache
│          │  LiteDistCoordinator.csproj.BuildWithSkipAnalyzers
│          │  LiteDistCoordinator.csproj.CoreCompileInputs.cache
│          │  LiteDistCoordinator.csproj.FileListAbsolute.txt
│          │  LiteDistCoordinator.GeneratedMSBuildEditorConfig.editorconfig
│          │  LiteDistCoordinator.GlobalUsings.g.cs
│          │  LiteDistCoordinator.MvcApplicationPartsAssemblyInfo.cache
│          │  LiteDistCoordinator.sourcelink.json
│          │
│          ├─Protos
│          │      Database.cs
│          │      DatabaseGrpc.cs
│          │      Greet.cs
│          │      GreetGrpc.cs
│          │      Testlist.cs
│          │      TestlistGrpc.cs
│          │
│          ├─ref
│          ├─refint
│          └─staticwebassets
├─Properties
│      launchSettings.json
│
├─Protos
│      database.proto
│      greet.proto
│      testlist.proto
│
└─Services
        DatabaseService.cs
        GreeterService.cs
        TestListService.cs

If any further clarification is needed, please let me know in the comments.


Solution

  • In your proto file, you declared the service name as:

    option csharp_namespace = "LiteDistCoordinator.Protos";
    ...
    service DatabaseService {
    

    So in your C# code, the class needs to extend from the base type of the above name:

    public class DatabaseService : 
                 LiteDistCoordinator.Protos.DatabaseService.DatabaseServiceBase