如何用参数从另一个服务调用一项服务并获取响应
目前,我有两个在不同端点运行的服务。例如(这不是我的真实情况):
StudentService
CheckHomeWorkService
CheckhomeworkService
可以从许多服务中使用(例如Texterservice,WorkerService
)。它具有一个带有Checkhomework
操作的控制器。它有一些参数:
HomeWorkNumber (int)
ProvidedSolution (string).
它将返回成功或失败。
现在,在我的学生服务中,我有一个具有submithomework行动的控制器。它需要使用Checkhomeworkservice检查家庭作业,并将结果保存到数据库中。我该如何实施?
我正在使用ocelot api网关
,但它可以“重定向”到另一个服务,我需要保存对数据库的响应的结果
Currently, I have two services running at different endpoints. For example (this is not my real scenario):
StudentService
CheckHomeWorkService
CheckHomeWorkService
can be used from many services (For example TeacherService, WorkerService
). It has one controller with CheckHomeWork
action. It has some parameters:
HomeWorkNumber (int)
ProvidedSolution (string).
It will return success or failed.
Now, In my StudentService, I have a controller with SubmitHomeWork Action. It needs to check homework using CHeckHomeWorkService and save results to a database. How can I implement this?
I was using Ocelot Api GateWay
but it can 'redirect' to another service and I need to save the result of the response to the database
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如评论中所建议的,您可以使用httpclient调用其他API。但是,如果您想要一个更安全,更具性能的解决方案,也可以使用HTTPClientFactory。
请参见为什么要使用它
>
As suggested in the comments, you can use HttpClient to call the other APIs. However, if you want a more safe and performant solution, you can use also the HttpClientFactory.
see this why to use it
and see the official docu
评论后,您说您有单独的Web API服务,
在这种情况下,您可以执行此服务来检索结果:
而:
“常数”是另一个API的基本URL的HTTP地址
,但是如果使用旧解决方案,
则可以在控制器构造器中传递“ Checkhomeworkservice”(“依赖项注入”)
并随心所欲地调用服务方法
After your comment you say that you have separate WEB API services
in this case you can do this service to retrieve the result :
whereas :
"Constants.ApiBaseUrl" is http address for base Url of another API
But in case of the same API you will use old solution :
you can pass "CHeckHomeWorkService" in your controller constructor ("Dependency Injection")
and call service methods as you like