。NET Core 3.1 gRPC Docker:无法使原型路径相对

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了。NET Core 3.1 gRPC Docker:无法使原型路径相对相关的知识,希望对你有一定的参考价值。

问题描述

由于Could not make proto path relative : error : Protos/GreeterService.proto: No such file or directory [/src/ProjectName/ProjectName.csproj],通过Docker发布默认的.NET Core 3.1 gRPC服务(由Visual Studio Community 2019 16.5.4生成)失败。>

如何复制

  1. [使用.net Core 3.1和Docker支持创建一个新的gRPC项目(Linux)
  2. 打开生成的Dockerfile
  3. 从第一个COPY的复制路径中删除目录(这是默认Dockerfile中的错误:]

  4. COPY ["ProjectName/ProjectName.csproj", "ProjectName/"] => COPY ["ProjectName.csproj", "ProjectName/"]

  5. 运行docker build .

  6. 现在行RUN dotnet build "ProjectName.csproj" -c Release -o /app/build引发错误:

Protos : warning : directory does not exist. [/src/ProjectName/ProjectName.csproj]
Could not make proto path relative : error : Protos/GreeterService.proto: No such file or directory [/src/ProjectName/ProjectName.csproj]

但是如果您进入容器,该文件实际上恰好位于此位置:

root@1b49365bc690:/src/ProjectName# ls
ProjectName.csproj  obj

我尝试了什么

关于此问题,我找不到很多,因此,我发现的唯一有用的看似答案是this SO answer,建议在.csproj文件中添加ProtoRoot="Protos",但可惜这没有帮助。

此外,我尝试从具有不同目标路径组合的不同工作目录中手动执行dotnet build命令,但没有一个有效。详细标志也没有提供任何有用的信息:

    Task "ProtoCompile"
      /root/.nuget/packages/grpc.tools/2.27.0/tools/linux_x64/protoc --csharp_out=obj/Release/netcoreapp3.1 --plugin=protoc-gen-grpc=/root/.nuget/packages/grpc.tools/2.27.0/tools/linux_x64/grpc_csharp_plugin --grpc_out=obj/Release/netcoreapp3.1 --grpc_opt=no_client --proto_path=/root/.nuget/packages/grpc.tools/2.27.0/build/native/include --proto_path=Protos --dependency_out=obj/Release/netcoreapp3.1/1255d1a520d30ea4_greet.protodep --error_format=msvs Protos/greet.proto
1:7>Protos : warning : directory does not exist. [/src/ProjectName/ProjectName.csproj]
1:7>Could not make proto path relative : error : Protos/greet.proto: No such file or directory [/src/ProjectName/ProjectName.csproj]

我希望有人可以帮助我解决这个问题;预先谢谢你。

资源

Dockerfile:

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["ProjectName.csproj", "ProjectName/"]
RUN dotnet restore "ProjectName/ProjectName.csproj"
COPY . .
WORKDIR "/src/ProjectName"
RUN dotnet build "ProjectName.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "ProjectName.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "ProjectName.dll"]

greet.proto:

syntax = "proto3";

option csharp_namespace = "ProjectName";

package greet;

// The greeting service definition.
service Greeter 
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply);


// The request message containing the user's name.
message HelloRequest 
  string name = 1;


// The response message containing the greetings.
message HelloReply 
  string message = 1;

。csproj

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

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <UserSecretsId>secretsId</UserSecretsId>
    <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
  </PropertyGroup>

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

  <ItemGroup>
    <PackageReference Include="Grpc.AspNetCore" Version="2.27.0" />
    <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.10.8" />
  </ItemGroup>

</Project>

问题描述由于无法发布相对原型路径而导致通过Docker发布默认的.NET Core 3.1 gRPC服务(由Visual Studio Community 2019 16.5.4生成)失败:错误:Protos / ...

答案
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base WORKDIR /app EXPOSE 80 EXPOSE 443 FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build WORKDIR /src COPY "ProjectName.csproj" . RUN dotnet restore "ProjectName.csproj" COPY . . RUN dotnet build "ProjectName.csproj" -c Release -o /app/build FROM build AS publish RUN dotnet publish "ProjectName.csproj" -c Release -o /app/publish FROM base AS final WORKDIR /app COPY --from=publish /app/publish . ENTRYPOINT ["dotnet", "ProjectName.dll"]

以上是关于。NET Core 3.1 gRPC Docker:无法使原型路径相对的主要内容,如果未能解决你的问题,请参考以下文章

从.NET Core 3.1(Docker)迁移后,.NET 6 应用程序日志为 JSON 格式 [重复]

.net core 使用Https总结

.NET Core 3.1 覆盖 docker compose 中的自定义值

如何使用 Visual Studio 2019 在 Docker 容器中运行 ASP.NET Core 3.1 项目?

C# 和 Docker - 无法从容器化 .NET Core 3.1 Web API 连接到容器化 MySQL 服务器

ASP.NET Core 3.1 Angular Web App 在 Windows 上构建和运行,但在 Mac OSX 和 Docker 上失败