English 中文(简体)
ASP.NETCORE如何使用RPC调用其他API接口
原标题:How ASP.NET CORE uses RPC to call other API interfaces

我现在有了一个新的 asp.net Core web api 的项目, 我用它作为主要语言, 但是有些界面和 java 完成了, 现在公司已经改用 asp. net 核心作为主要语言。 我可以使用 asp. net 核心来完成这些界面, 但是它没有意义。 所以我在网上检查了信息, 并建议我使用 rpc 来调用上一个界面。 我该怎么做?

以一个 Java 界面为例

@ApiOperation(value = "Send email registration verification code")
    @GetMapping("send_code")
    public JsonData sendRegisterCode(@ApiParam("Recipient") @RequestParam(value = "to", required = true) String to,
                                     @ApiParam("Verification code") @RequestParam(value = "captcha", required = true) String captcha,
                                     HttpServletRequest request
    ) {

        String key = getCaptchaKey(request);
        String cacheCaptcha = redisTemplate.opsForValue().get(key);
        //Matching graphic verification code

        if (captcha != null && cacheCaptcha != null && captcha.equalsIgnoreCase(cacheCaptcha)) {
            //success
            redisTemplate.delete(key);
            JsonData jsonData = notifyService.sendCode(SendCodeEnum.USER_REGISTER, to);
            return jsonData;

        } else {
            return JsonData.buildResult(BizCodeEnum.CODE_CAPTCHA_ERROR);
        }

    }

    private String getCaptchaKey(HttpServletRequest request) {
        String ip = CommonUtil.getIpAddr(request);
        String userAgent = request.getHeader("User-Agent");
        String key = "user-service:captcha:" + CommonUtil.MD5(ip + userAgent);
        return key;
    }

我该如何称呼修改过的pi?

问题回答

现为2024/05/21, 我也有类似的要求, 并写下一个图书馆来实施一个简单的RPC框架 , 依据 ASP. NET Core:

ProjectFolder/
|
├── ServiceA/
|   ├── SampleRpcController.cs    <-- Implement RPC via interface (Server Side)
|   ├── Program.cs
|   └── ...
|
├── ServiceA.Shared/
|   ├── ISampleRpcService.cs      <-- Define RPC interface (Shared Project)
|   └── ...
|
├── ServiceB/
|   ├── AppController.cs          <-- Call RPC via interface (Client Side)
|   ├── Program.cs
|   └── ...

顺着那种感觉

  • Define a RPC Interface with some Attributes provided by ASP.NET Core
// In ServiceA.Shared/ISampleRpcService.cs
[HttpRoute("/api/v1/public")]
public interface ISampleRpcService : IRpcController
{
    [HttpGet("int")]
    int Add(int a, int b);
}
  • Implement the RPC Interface in Service A with Controller (Server Side)
// In ServiceA/SampleRpcService.cs
public class SampleRpcController : ControllerBase, ISampleRpcService
{
    public int Add(int a, int b)
    {
        return a + b;
    }
}
  • Call the RPC Server via RPC Interface in Service B (Client Side)
// In ServiceB/AppController.cs
public class AppController
{
    private readonly ISampleRpcService _sampleRpcService;

    public AppController(ISampleRpcService sampleRpcService)
    {
        _sampleRpcService = sampleRpcService;
    }

    public int CallRPC()
    {
        return _sampleRpcService.Add(1, 2);
    }
}

如果您有兴趣, 欢迎使用此库!

您可以使用 grpc 模板创建工程 :

参考文件:

https://learn.microsoft.com/en-us/aspnet/core/grpc/??view=aspnetcore-3.1,rel=“不跟随 norefererr'>https://learn.microsoft.com/en-us/aspnet/core/grpc/?view=aspnetcore-3.1

如果您想要对现有工程使用, 您可以做到这一点 :

1. <% 1 > Add 需要 Nuget 软件包 :

Install-Package Grpc.Net.Client
Install-Package Google.Protobuf
Install-Package Grpc.Tools

2.<强/强/强/强>欢迎.proto

在 gRPC 客户端项目中创建 Protos 文件夹 。

Copy the Protosgreet.proto file from the gRPC Greeter service to the Protos folder in the gRPC client project.

Update the namespace inside the greet.proto file to the project s namespace: option csharp_namespace = "GrpcGreeterClient";

添加一个项目组, 包含一个与 heat. proto 文件相关的元素 :

<ItemGroup>
  <Protobuf Include="Protosgreet.proto" GrpcServices="Client" />
</ItemGroup>

3. 关于创建Greeter客户端和在GRPC Greeter服务下测试GRPC客户端的具体步骤,请参阅正式文件:

< a href=>https://learn.microsoft.com/en-us/aspnet/core/tutorics/grpc/grpc-start?view=aspnetcore-6.0&tabs=可视-studio' rel=“不跟随无雷">https://learn.microsoft.com/en-us/aspnet/core/tutorics/grpc/grpc-start?view=aspnetcore-6.0&tabs=可视-studio





相关问题
GWT + GAE python: frameworks for COMET & RPC

Let s say I want to use Google GWT on the client side and Google AppEngine Python on the server side. Furthermore, I want to be able to use RPC calls to the server as well as performing COMET based ...

Remote COM server instantiation

I have a COM interface to start and use a program. This works great on a local machine. Is there a possibility to start that program on another machine, through the network without installing other ...

Invoking ONC RPC from Ruby?

I have a Ruby (Rails) app that needs to make some calls to a service exposed via ONC RPC. Is there a way I can readily accomplish this in Ruby or do I need to create a proxy service using another ...

IPC between Python and C#

I want to pass data between a Python and a C# application in Windows (I want the channel to be bi-directional) In fact I wanna pass a struct containing data about a network packet that I ve captured ...

ClassFormatError: 56 while using hessian in j2me

I am trying to use the hessian j2me implementation @ http://hessian.caucho.com/ using java me sdk 3.0. http://hessian.caucho.com/doc/hessian-overview.xtp#Hessian%20Client%20for%20a%20cell-phone ...

热门标签