如何用参数从另一个服务调用一项服务并获取响应

发布于 2025-02-02 01:18:08 字数 511 浏览 3 评论 0原文

目前,我有两个在不同端点运行的服务。例如(这不是我的真实情况):

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一曲琵琶半遮面シ 2025-02-09 01:18:08

如评论中所建议的,您可以使用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

回首观望 2025-02-09 01:18:08

评论后,您说您有单独的Web API服务,

在这种情况下,您可以执行此服务来检索结果:

public async Task<List<HomeWorkModel>> CheckHomeWorkService(int 
homeWorkNumber, string providedSolution)
    {
        using (var httpClient = new HttpClient())
        {
            using (var response = await 
httpClient.GetAsync(Constants.ApiBaseUrl + "/CheckHomeWork?n=" + 
homeWorkNumber + "&sol=" + providedSolution))
            {
                if (response.StatusCode == 
System.Net.HttpStatusCode.OK)
                {
                    string apiResponse = await 
response.Content.ReadAsStringAsync();
                    return 
JsonConvert.DeserializeObject<List<HomeWorkModel>>(apiResponse);
                }
                else
                {
                    return null;
                }
            }
        }
    }
public class HomeworkModel
{
  Public bool Result {get; set;}
}

而:
“常数”是另一个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 :

public async Task<List<HomeWorkModel>> CheckHomeWorkService(int 
homeWorkNumber, string providedSolution)
    {
        using (var httpClient = new HttpClient())
        {
            using (var response = await 
httpClient.GetAsync(Constants.ApiBaseUrl + "/CheckHomeWork?n=" + 
homeWorkNumber + "&sol=" + providedSolution))
            {
                if (response.StatusCode == 
System.Net.HttpStatusCode.OK)
                {
                    string apiResponse = await 
response.Content.ReadAsStringAsync();
                    return 
JsonConvert.DeserializeObject<List<HomeWorkModel>>(apiResponse);
                }
                else
                {
                    return null;
                }
            }
        }
    }
public class HomeworkModel
{
  Public bool Result {get; set;}
}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文