I wanted to create an integration test for a C# GRPC service:
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");
}
And to use the client in the test:
using var channel = GrpcChannel.ForAddress("https://localhost:8013");
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's csproj:
<ItemGroup>
<Protobuf Include="Protos\data.proto" GrpcServices="Client" />
</ItemGroup>
So how can I reach that I had a Client genarted in the test project?
Thank you!
So how can I reach that I had a Client genarted in the test project?
Add
-> Reference...
. Then, from the list of possible projects, choose the server project, and then click OK
button.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<ItemGroup>
<Protobuf Include="Protos\data.proto" GrpcServices="Both" />
</ItemGroup>
By setting GrpcServices to Both
, the Protobuf compiler
will generate both server-side and client-side code.
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.using var channel = GrpcChannel.ForAddress("https://localhost:8013");
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.