因此,我正在尝试构建同时具有REST和gRPC的.Net Core应用程序。
预期结果::要运行一个在一个端口上支持REST并在另一个端口上支持gRPC的应用程序。
REST很容易。这是应用程序启动的地方。但是要配置gRPC的端口,我看到我需要创建一个Server实例:
Server server = new Server
{
Services = { Greeter.BindService(new GreeterImpl()) }
Ports = { new ServerPort("0.0.0.0", 5001, ServerCredentials.Insecure) }
};
server.Start();
这很好,直到我们实际使用它,并且像其他“控制器”一样,GreeterImpl需要服务的依赖注入:
private readonly IExampleService _exampleService;
public GreeterImpl (IExampleService exampleService)
{
_exampleService = exampleService;
}
由于“ new GreeterImpl()”需要IExampleService,因此第一个代码段将不起作用。 我在网上搜索了如何获取ServerServiceDefinition(从Greeter.BindService()返回的内容),而不使用具体的实现,但没有发现任何结果。那么,应该怎么做呢?还是我走在一条完全错误的道路上?
答案 0 :(得分:0)
所以,我的想法是错误的。轮到我们您可以使用
app.ApplicationServices.GetService(typeof({YOUR_SERVICE}))
其中“ app”是“ IApplicationBuilder” 然后,只需在“ .BindService()”中使用生成的服务即可。 最后,我们不会更改“ .BindService()”,而是将其传递给要绑定的工作服务。
重要说明:您必须先注册服务。就我而言,我使用AutoFac进行注册