Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize user session State transfer by Abp + Grpc

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report