在.NET 6中构建gRPC服务时出现错误。

huangapple go评论80阅读模式
英文:

error while building gRPC service in .net 6

问题

我尝试按照此教程的步骤,但无法运行 gRPC 服务。以下是输出:

在.NET 6中构建gRPC服务时出现错误。

原始输出文本:

生成开始...
1>------ 生成开始: 项目: GrpcGreeter,配置: 调试 Any CPU ------
1>生成开始于 2023年12月3日 03:04:46。
1>目标 GenerateTargetFrameworkMonikerAttribute:
1>  跳过目标“GenerateTargetFrameworkMonikerAttribute”,因为所有输出文件都是基于输入文件的最新状态。
1>目标 _Protobuf_CoreCompile:
1>  C:\Users\Paweł\.nuget\packages\grpc.tools.40.0\build\_protobuf\Google.Protobuf.Tools.targets(280,5): 错误 MSB6006: "C:\Users\Paweł\.nuget\packages\grpc.tools.40.0\tools\windows_x64\protoc.exe" 退出代码为 -1073741819。
1>在项目“GrpcGreeter.csproj”中的目标“_Protobuf_CoreCompile”生成完成 -- 失败。
1>
1>在项目“GrpcGreeter.csproj”中生成完成 -- 失败。
1>
1>生成失败。
1>
1>C:\Users\Paweł\.nuget\packages\grpc.tools.40.0\build\_protobuf\Google.Protobuf.Tools.targets(280,5): 错误 MSB6006: "C:\Users\Paweł\.nuget\packages\grpc.tools.40.0\tools\windows_x64\protoc.exe" 退出代码为 -1073741819。
1>    0 个警告
1>    1 个错误
1>
1>用时 00:00:01.70
========== 生成: 0 个成功,1 个失败,0 个最新,0 个跳过 ==========
========== 生成于凌晨3:04,共耗时02,212秒 ==========

我尝试过使用 .NET 5、6 和 7,但不幸的是都没有成功。
我怀疑问题可能与 nuget 包路径中的非ASCII字符有关,但我不确定。在此提前感谢任何帮助。

编辑:
以下是我的 proto 文件。我没有对它做任何更改,它已经由 Visual Studio 自动生成。

```protobuf
syntax = "proto3";

option csharp_namespace = "GrpcGreeter";

package greet;

// 问候服务定义。
service Greeter {
  // 发送问候
  rpc SayHello (HelloRequest) returns (HelloReply);
}

// 包含用户姓名的请求消息。
message HelloRequest {
  string name = 1;
}

// 包含问候的响应消息。
message HelloReply {
  string message = 1;
}

以及 GreeterService.cs(也是自动生成的):

using Grpc.Core;
using GrpcGreeter;

namespace GrpcGreeter.Services
{
    public class GreeterService : Greeter.GreeterBase
    {
        private readonly ILogger<GreeterService> _logger;
        public GreeterService(ILogger<GreeterService> logger)
        {
            _logger = logger;
        }

        public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
        {
            return Task.FromResult(new HelloReply
            {
                Message = "Hello " + request.Name
            });
        }
    }
}
英文:

I try to follow this tutorial, but I can't run the gRPC service. Here is the output:
在.NET 6中构建gRPC服务时出现错误。

Raw output text:

Build started...
1&gt;------ Build started: Project: GrpcGreeter, Configuration: Debug Any CPU ------
1&gt;Build started 12.03.2023 03:04:46.
1&gt;Target GenerateTargetFrameworkMonikerAttribute:
1&gt;  Skipping target &quot;GenerateTargetFrameworkMonikerAttribute&quot; because all output files are up-to-date with respect to the input files.
1&gt;Target _Protobuf_CoreCompile:
1&gt;  C:\Users\Paweł\.nuget\packages\grpc.tools.40.0\build\_protobuf\Google.Protobuf.Tools.targets(280,5): error MSB6006: &quot;C:\Users\Paweł\.nuget\packages\grpc.tools.40.0\tools\windows_x64\protoc.exe&quot; exited with code -1073741819.
1&gt;Done building target &quot;_Protobuf_CoreCompile&quot; in project &quot;GrpcGreeter.csproj&quot; -- FAILED.
1&gt;
1&gt;Done building project &quot;GrpcGreeter.csproj&quot; -- FAILED.
1&gt;
1&gt;Build FAILED.
1&gt;
1&gt;C:\Users\Paweł\.nuget\packages\grpc.tools.40.0\build\_protobuf\Google.Protobuf.Tools.targets(280,5): error MSB6006: &quot;C:\Users\Paweł\.nuget\packages\grpc.tools.40.0\tools\windows_x64\protoc.exe&quot; exited with code -1073741819.
1&gt;    0 Warning(s)
1&gt;    1 Error(s)
1&gt;
1&gt;Time Elapsed 00:00:01.70
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
========== Build started at 3:04 AM and took 02,212 seconds ==========

I was trying with .net 5, 6 and 7, but unfortunately with no success.
I suspect, the problem might be with non-ascii characters in path to nuget packages, but I'm not sure. Thank you in advance for any help.

EDIT:
Here's my proto file. I haven't changed anything here, it was already generated by vs.

syntax = &quot;proto3&quot;;

option csharp_namespace = &quot;GrpcGreeter&quot;;

package greet;

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

// The request message containing the user&#39;s name.
message HelloRequest {
  string name = 1;
}

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

and GreeterService.cs (also auto-generated):

using Grpc.Core;
using GrpcGreeter;

namespace GrpcGreeter.Services
{
    public class GreeterService : Greeter.GreeterBase
    {
        private readonly ILogger&lt;GreeterService&gt; _logger;
        public GreeterService(ILogger&lt;GreeterService&gt; logger)
        {
            _logger = logger;
        }

        public override Task&lt;HelloReply&gt; SayHello(HelloRequest request, ServerCallContext context)
        {
            return Task.FromResult(new HelloReply
            {
                Message = &quot;Hello &quot; + request.Name
            });
        }
    }
}

答案1

得分: 1

验证 greet.proto 文件的属性...

  • 构建操作 - Protobuf 编译器

  • gRPC 存根类 - 仅服务器端

如果你无法看到这些属性,你也可以在项目文件中检查。

完成验证后,重新构建项目。

英文:

Verify greet.proto properties...

  • Build Action - Protobuf compiler

  • gRPC Stub Classes - Server only

在.NET 6中构建gRPC服务时出现错误。

If you are not able to see properties, you can check in project file as well.

在.NET 6中构建gRPC服务时出现错误。

Once you verify this, re-build project.

答案2

得分: 0

谢谢大家的帮助。我已经知道问题出在哪里了。事实证明,我的包路径中确实存在非ASCII字符(ł)的问题。解决方法是将 .nuget 目录从默认位置移动到另一个目录(路径中只包含ASCII字符)。然后,您需要在解决方案中添加一个 nuget.config 文件,内容如下:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <config>
    <add key="globalPackagesFolder" value="[rel_path]\.nuget\packages" />
    <add key="repositoryPath" value="[rel_path]\.nuget\packages" />
  </config>
</configuration>

其中 [rel_path] 是相对于 nuget.config 文件的当前位置的移动 .nuget 目录的相对路径。不过还是要感谢您的帮助。

英文:

Thank you all for your help. I already know what was wrong. It turned out, that it indeed was a problem with non-ascii character (ł) in my path to packages. The solution was to move .nuget directory from my default location to some other directory (with ascii-only characters in path). Then you need to add nuget.config file to solution with this content:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;configuration&gt;
  &lt;config&gt;
    &lt;add key=&quot;globalPackagesFolder&quot; value=&quot;[rel_path]\.nuget\packages&quot; /&gt;
    &lt;add key=&quot;repositoryPath&quot; value=&quot;[rel_path]\.nuget\packages&quot; /&gt;
  &lt;/config&gt;
&lt;/configuration&gt;

Where [rel_path] is relative path to your moved .nuget directory. Of course relative to current location of nuget.config file. Thank you for your help though.

huangapple
  • 本文由 发表于 2023年3月12日 10:22:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75710737.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定