C# GRPC集成测试仅服务器端已生成。

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

C# GRPC intergation test only server side is generated

问题

我想为一个C# GRPC服务创建一个集成测试

```csharp
public class DataProviderTests
{
    private const string Host = "localhost";
    private const int Port = 8013;

    private Server _server;
    private GrpcChannel _channel;

    public DataProviderTests()
    {
        _server = new Server
        {
            Services = { DataProvider.BindService(new DataProviderService()) },
            Ports = { new ServerPort(Host, Port, ServerCredentials.Insecure) }
        };

        _server.Start();

        _channel = GrpcChannel.ForAddress("https://localhost:8013");
    }

在测试中使用客户端:

using var channel = GrpcChannel.ForAddress("https://localhost:8013");
var client = new DataProvider.DataProviderClient(channel);

由于GRPC服务器项目作为依赖项附加,生成的代码中没有DataProviderClient。而DataProvider生成的类位于服务器项目中。

在测试的csproj中使用以下设置:

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

那么如何确保在测试项目中生成了客户端呢?

谢谢!


<details>
<summary>英文:</summary>

I wanted to create an integration test for a C# GRPC service:

     public class DataProviderTests
    {
        private const string Host = &quot;localhost&quot;;
        private const int Port = 8013;

        private Server _server;
        private GrpcChannel _channel;

        public DataProviderTests()
        {
            _server = new Server
            {
                Services = { DataProvider.BindService(new DataProviderService()) },
                Ports = { new ServerPort(Host, Port, ServerCredentials.Insecure) }
            };

            _server.Start();

            _channel = GrpcChannel.ForAddress(&quot;https://localhost:8013&quot;);
        }


And to use the client in the test:


     using var channel = GrpcChannel.ForAddress(&quot;https://localhost:8013&quot;);
     var client = new DataProvider.DataProviderClient(channel);

But there is no DataProviderClient in the generated code, since the GRPC server project is attached as dependency. And the DataProvider generated class is located in the server project.

 I use the following settings in the test&#39;s csproj:


    &lt;ItemGroup&gt;
		&lt;Protobuf Include=&quot;Protos\data.proto&quot; GrpcServices=&quot;Client&quot; /&gt;
	&lt;/ItemGroup&gt;

So how can I reach that I had a Client genarted in the test project?

Thank you!

</details>


# 答案1
**得分**: 1

1. **你的测试项目应该包含对服务器项目的引用**。要做到这一点,在Visual Studio中,右键单击测试项目,选择`添加` -> `引用...`。然后,在可能的项目列表中,选择服务器项目,然后点击`确定`按钮。

2. **修改** `Protobuf` 项,以在测试项目的 `csproj 文件` 中创建 **客户端** 和 **服务器端代码**。要做到这一点,将GrpcServices属性从`Client`更改为`Both`。以下是修改后的Protobuf项的示例:
```xml
<ItemGroup>
  <Protobuf Include="Protos\data.proto" GrpcServices="Both" />
</ItemGroup>

通过将GrpcServices设置为BothProtobuf编译器将生成 服务器端客户端代码

  1. 重新构建解决方案,以使 Protobuf编译器 生成修改后的代码。一旦构建完成,您应该能够在测试项目中使用生成的客户端代码。

  2. 在测试代码中,您可以使用生成的 客户端代码 创建一个客户端对象。例如:

using var channel = GrpcChannel.ForAddress("https://localhost:8013");
var client = new DataProvider.DataProviderClient(channel);

您可以看到 DataProviderprotobuf文件 的名称(不包括.proto扩展名),而 DataProviderClient 是由 Protobuf编译器 生成的 客户端类 的名称。此 客户端对象 可用于测试您的服务功能并向服务器发送RPC请求。

就是这样!通过这些修改,您应该能够创建可在测试项目中使用的客户端代码,并测试您的GRPC服务。

英文:

> So how can I reach that I had a Client genarted in the test project?

  1. Your test project should include a reference to the server project. To do this, in Visual Studio, right-click the test project and choose Add -> Reference.... Then, from the list of possible projects, choose the server project, and then click OK button.
  2. Modify the Protobuf item to create client-side and server-side code in the test project's csproj file. Change the GrpcServices attribute from Client to Both to do this. Here is an illustration of how the revised Protobuf item should look like:
    xml
&lt;ItemGroup&gt;
  &lt;Protobuf Include=&quot;Protos\data.proto&quot; GrpcServices=&quot;Both&quot; /&gt;
&lt;/ItemGroup&gt;

By setting GrpcServices to Both, the Protobuf compiler will generate both server-side and client-side code.

  1. Rebuild the solution to get the Protobuf compiler to produce the revised code. You should be able to utilise the resulting client-side code in your test project once the build is finished.
  2. In the test code, you can create a client object using the generated client code. For example:
using var channel = GrpcChannel.ForAddress(&quot;https://localhost:8013&quot;);
var client = new DataProvider.DataProviderClient(channel);

You can see that DataProvider is the name of the protobuf file (without the .proto extension) and DataProviderClient is the name of the client-side class that was generated by the `Protobuf compiler. This client object can be used to test the functionality of your service and send RPC requests to the server.

That's it! With these modifications, you ought to be able to create client-side code for use in your test project and to test your GRPC service.

huangapple
  • 本文由 发表于 2023年5月6日 22:24:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76189407.html
匿名

发表评论

匿名网友

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

确定