-
Notifications
You must be signed in to change notification settings - Fork 8
gRPC service example
Ushakov Michael edited this page Sep 19, 2025
·
3 revisions
This page contains a full description about how to implement the gRPC service using Wissance.WebApiToolkit. Because gRPC way is to generate code from .proto-files therefore we can't fully automate gRPC service creation; however, we could use our own services as an aggregate in any gRPC service and using this approach we get the following limitations:
- We have to use double object conversion
Message-2-DTOandDTO-2-Message - Create additional class based on generated service
- In project that you need to generate a
gRPCservice we should create a.proto-filei.e. see our testgRPCservice.proto - In .csproj file we should add the following lines to generate
gRPCservice code:
<ItemGroup>
<PackageReference Include="Google.Protobuf" Version="3.30.1" />
<PackageReference Include="Grpc.AspNetCore" Version="2.70.0" />
<PackageReference Include="Grpc.AspNetCore.Server" Version="2.70.0" />
<PackageReference Include="Grpc.Core.Api" Version="2.70.0" />
<PackageReference Include="Grpc.Tools" Version="2.71.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<Protobuf Include="$(ProjectDir)WebServices\Grpc\Proto\*.proto" ProtoRoot="$(ProjectDir)WebServices\Grpc\Proto" GrpcServices="Both" OutputDir="$(ProjectDir)WebServices\Grpc\Generated" ProtoCompile="True" CompileOutputs="true" />
<Protobuf Include="$(ProjectDir)WebServices\Grpc\Proto\Common\*.proto" ProtoRoot="$(ProjectDir)WebServices\Grpc\Proto" GrpcServices="None" />
</ItemGroup>
<PropertyGroup Condition="'$(DesignTimeBuild)' == 'true' ">
<DisableProtobufDesignTimeBuild>true</DisableProtobufDesignTimeBuild>
</PropertyGroup>
<ItemGroup>
<Folder Include="WebServices\Grpc\Generated" />
</ItemGroup>where:
-
Protobuf Includepoints to the directory where all.proto-files are located. -
OutputDirtells wheregRPCcode is generating.
- Compile and generate
gRPCservice code - Create your service, i.e., in this example,
CodeService. This service class must be derived from the generated service and must have in its constructor one of the Service classes:
public CodeGrpcService(ResourceBasedDataManageableReadOnlyService<CodeDto, CodeEntity, int, EmptyAdditionalFilters> serviceImpl)
{
_serviceImpl = serviceImpl;
}- Also this service class should call factory method i.e. from this class:
private Code Convert(CodeDto dto)
{
if (dto == null)
return null;
return new Code()
{
Id = dto.Id,
Code_ = dto.Code,
Name = dto.Name
};
}