0

I have an ASP.Net Core gRPC project that I'd like to run integration tests on. As such, I set up an integration test project.

My directory structure looks something like this:

- src
  - MyService.Grpc
    - Protos
      - my-service.proto
    - Startup.cs
- tests
  - MyService.Grpc.Tests

In order to set up the WebApplicationFactory to call the Startup class in MyService.Grpc and to use the generated client provided by Grpc.Net.Client, I have the test project set up like this:

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

    <PropertyGroup>
        <TargetFramework>net5.0</TargetFramework>

        <IsPackable>false</IsPackable>
    </PropertyGroup>

    <ItemGroup>
        <!-- Proto file reference -->
        <Protobuf Include="../../src/MyService.Grpc/Protos/my-service.proto" GrpcServices="Client" />
        <!-- ASP.NEt Core gRPC dependencies -->
        <PackageReference Include="Google.Protobuf" Version="3.17.3" />
        <PackageReference Include="Grpc.Net.Client" Version="2.38.0" />
        <PackageReference Include="Grpc.Tools" Version="2.38.1">
          <PrivateAssets>all</PrivateAssets>
          <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>
        <!-- Testing framework references -->
        <PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="5.0.8" />
        <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
        <PackageReference Include="xunit" Version="2.4.1" />
        <PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
            <PrivateAssets>all</PrivateAssets>
        </PackageReference>
        <PackageReference Include="coverlet.collector" Version="3.1.0">
            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
            <PrivateAssets>all</PrivateAssets>
        </PackageReference>
    </ItemGroup>

    <ItemGroup>
        <ProjectReference Include="../../src/MyService.Grpc/MyService.Grpc.csproj" />
    </ItemGroup>

Note that I have both the gRPC server project (MyService.Grpc.csproj) and the proto file (my-service.proto) referenced from the test project.

Everything compiles but I ran into some compile warnings that I would like to get rid of.

  MyService.cs(44, 67): [CS0436] The type 'MyRequest' in 'tests\MyService.Grpc.Tests\obj\Debug\net5.0\MyService.cs' conflicts with the imported type 'MyRequest' in 'MyService.Grpc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Using the type defined in 'tests\MyService.Grpc.Tests\obj\Debug\net5.0\MyService.cs'.

I understand what the compiler is complaining about because the type MyRequest is defined twice: once generated by Grpc.Net.Client from the proto file and once by the project reference import. At the same time, I cannot get rid of either reference in the test project because I need both of them.

My question is how can I get rid of this warning without importing the same types twice in my tests?

rexcfnghk
  • 14,435
  • 1
  • 30
  • 57
  • can you override the namespace in the .proto file in the test project? namespaces don't matter with protobuf (at least, not in terms of payloads) – Marc Gravell Jul 27 '21 at 08:35
  • @MarcGravell there is only one .proto file and it is under the main project (the test project only references the proto file via a relative path). I am not sure how can I override namespaces with only one .proto file. – rexcfnghk Jul 27 '21 at 08:43
  • well, at the `protoc` level: `--csharp_opt=base_namespace=Foo` - not sure if this is available via the tooling you're using, though – Marc Gravell Jul 27 '21 at 09:11
  • It doesn't work even when I set `option csharp_namespace` in the proto file to something else because the same class is still imported twice – rexcfnghk Jul 27 '21 at 09:17

0 Answers0