Manik_trs
Manik_trs

Reputation: 51

ServiceStack Grpc - Generate proto file without invoking types/proto endpoint

I have a .Net Core 3.1 Grpc application created using ServiceStack framework. ServiceStack provides a way to auto-generate .proto files through /types/proto endpoint. It internally invokes GrpcProtoGenerator class to generate .proto file. It also requires AppHost, IHostingEnvironment and other objects to generate it.

However, I want to manually invoke GrpcProtoGenerator class for file generation instead of invoking /types/proto endpoint, but it's throwing an error, saying IHostingEnvironment is null. Is there a way to manually invoke the class by providing all necessary objects?

Upvotes: 2

Views: 186

Answers (1)

mythz
mythz

Reputation: 143284

A lot of ServiceStack Features especially ServiceStack Services require a fully initialized AppHost. You can checkout at GrpcTests for an example of how to initialize a grpc-enabled AppHost in code, after the AppHost is initialized you should then be able to invoke GrpcProtoGenerator manually.

This is what the /types/proto Service uses to generate the proto for all your gRPC Services:

var typesConfig = NativeTypesMetadata.GetConfig(request);
var metadataTypes = NativeTypesMetadata.GetMetadataTypes(Request, typesConfig);
var proto = new GrpcProtoGenerator(typesConfig).GetCode(metadataTypes, base.Request);
return proto;

The GrpcTests also shows examples of creating a proto for adhoc types:

static string GetServiceProto<T>()
    => GrpcConfig.TypeModel.GetSchema(MetaTypeConfig<T>.GetMetaType().Type, ProtoBuf.Meta.ProtoSyntax.Proto3);

    var schema = GetServiceProto<CustomRequestDto>();
    Assert.AreEqual(@"syntax = ""proto3"";

package ServiceStack.Extensions.Tests;
message CustomRequestDto {
       int32 PageName = 42;
       string Name = 105;
    }
    ", schema);

Upvotes: 1

Related Questions