0

So I am trying to build a .Net Core app that has both REST and gRPC.

Expected results: To have one app running that supports a working REST on one port and gRPC on another.

REST is easy. It's where the app starts. But to configure gRPC's port I saw I needed to create a Server instance:

        Server server = new Server
        {
            Services = { Greeter.BindService(new GreeterImpl()) }
            Ports = { new ServerPort("0.0.0.0", 5001, ServerCredentials.Insecure) }
        };

        server.Start();

That's all fine until we actually put it to use and like any other "Controller" GreeterImpl needs Dependency Injection of services:

        private readonly IExampleService _exampleService;

        public GreeterImpl (IExampleService exampleService)
        {
            _exampleService = exampleService;
        }

Now the first code snippet will not work since the "new GreeterImpl()" requires an IExampleService. I searched the web on how to get a ServerServiceDefinition (the thing returned from Greeter.BindService() ) without the use of concrete implementations but found nothing. So, how should this be done or am I on a totally wrong path?

  • Assuming you're using ASP.NET Core for your REST APIs, it is easier to use the [gRPC implementation that builds upon ASP.NET Core](https://github.com/grpc/grpc-dotnet). This way you end up sharing the same app func and service container across both gRPC and REST with them merely being different endpoints. – Sourabh Shirhatti Apr 15 '20 at 17:32

1 Answers1

0

So, I was going at it with the wrong idea. Turns our you can use

app.ApplicationServices.GetService(typeof({YOUR_SERVICE}))

Where "app" is "IApplicationBuilder" And then just use the resulting service in the ".BindService()". In the end we don't change the ".BindService()" but pass it a working service to bind.

Important note: You have to register your service first. In my case I used AutoFac to register it