Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to realize user session State transfer by Abp + Grpc

Shulou Source: shulou.com Published: 2022-06-02 06:14:15 10月12日 Update

This article mainly explains "Abp + Grpc how to achieve user session state transfer", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "Abp + Grpc how to achieve user session state transfer" bar!

0. Background

In the actual project, I use the Abp framework, but the Abp framework does not officially encapsulate the module for Grpc. Based on this, I combine Abp and MagicOnion to encapsulate an Abp.Grpc module, which includes two parts of the package: the server side and the calling side. With these two packages, you can easily integrate Grpc into the Abp framework to achieve internal communication between services.

However, there will be a problem in the actual use. When A service invokes B service, the current login user of A service is admin, and the value of IAbpSession that invokes B service is still empty. At this time, there will be a problem when the internal implementation of B service uses IAbpSession.

This is because when called through the Grpc interface, nothing such as Token is passed, and the data attached to the IAbpSession itself inside the B service is obtained from the HttpContext, so the B service currently has no user state.

1. Solve

Fortunately, IAbpSession provides a Use method through which we can temporarily change the value inside the IAbpSession when. The definition is as follows:

IDisposable Use (int? TenantId, long? UserId)

The method of use is as follows:

Modification of 2.Grpc interface

Here, the Abp.Grpc library uses the MagicOnion library to implement the Grpc interface, and the underlying serialization uses MessagePack, which is as fast as Protocol Buffer.

2.1 Service definition

When a service defines an interface, it must attach a GrpcSession parameter that is used by the caller to pass its IAbpSession value. For example, I have an interface method that returns the user Id value received by the server.

2.2 Service provider

When implementing ITestGrpcService, the service provider needs to start wrapping the code with using statements at the beginning of the code.

2.3 Service caller

The service caller passes the Session state of the current service to the interface directly when invoking the Grpc interface.

2.4 final effect

When the client invokes the GRPC interface, it passes its Session state to the server through GrpcSession, so that the server can share the drawing state of the client.

3.Abp.Grpc project address

Abp.Grpc library address: https://github.com/GameBelial/Abp.Grpc

4. Implemented DEMO address

Server: https://github.com/GameBelial/Abp.Grpc.Server.Demo

Client: https://github.com/GameBelial/Abp.Grpc.Client.Demo

Original address: https://www.cnblogs.com/myzony/p/9454612.html

Thank you for reading, the above is the content of "Abp + Grpc how to achieve user session state transfer". After the study of this article, I believe you have a deeper understanding of how Abp + Grpc achieves user session state transmission, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Service status interface user address method time customer framework problem learning code supplier content parameter actual client module project encapsulation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple MySQL Shulou Information Xiaomi Docker