CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
GrpcProxy.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="GrpcProxy.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Implements a data portal proxy to relay data portal</summary>
7//-----------------------------------------------------------------------
8using System.Threading.Tasks;
10using Google.Protobuf;
11using Grpc.Net.Client;
12
13namespace Csla.Channels.Grpc
14{
20 {
28 public GrpcProxy(ApplicationContext applicationContext, GrpcChannel channel, GrpcProxyOptions options)
29 : base(applicationContext)
30 {
31 _channel = channel;
33 }
34
35 private GrpcChannel _channel;
36 private static GrpcChannel _defaultChannel;
37
42 protected virtual GrpcChannel GetChannel()
43 {
44 if (_channel == null)
45 {
46 _defaultChannel ??= GrpcChannel.ForAddress(DataPortalUrl);
47 _channel = _defaultChannel;
48 }
49 return _channel;
50 }
51
56 protected static void SetChannel(GrpcChannel channel)
57 {
58 _defaultChannel = channel;
59 }
60
61 private GrpcService.GrpcServiceClient _grpcClient;
62
67 protected virtual GrpcService.GrpcServiceClient GetGrpcClient()
68 {
69 return _grpcClient ??= new GrpcService.GrpcServiceClient(GetChannel());
70 }
71
81 protected override async Task<byte[]> CallDataPortalServer(byte[] serialized, string operation, string routingToken, bool isSync)
82 {
83 ByteString outbound = ByteString.CopyFrom(serialized);
84 var request = new RequestMessage
85 {
86 Body = outbound,
87 Operation = CreateOperationTag(operation, ApplicationContext.VersionRoutingTag, routingToken)
88 };
89 ResponseMessage response;
90 if (isSync)
91 response = GetGrpcClient().Invoke(request);
92 else
93 response = await GetGrpcClient().InvokeAsync(request);
94 return response.Body.ToByteArray();
95 }
96
97 internal async Task<ResponseMessage> RouteMessage(RequestMessage request)
98 {
99 return await GetGrpcClient().InvokeAsync(request);
100 }
101
102 private string CreateOperationTag(string operatation, string versionToken, string routingToken)
103 {
104 if (!string.IsNullOrWhiteSpace(versionToken) || !string.IsNullOrWhiteSpace(routingToken))
105 return $"{operatation}/{routingToken}-{versionToken}";
106 return operatation;
107 }
108 }
109}
Provides consistent context information between the client and server DataPortal objects.
static string VersionRoutingTag
Gets a value representing the application version for use in server-side data portal routing.
Implements a data portal proxy to relay data portal calls to a remote application server by using gRP...
Definition: GrpcProxy.cs:20
override async Task< byte[]> CallDataPortalServer(byte[] serialized, string operation, string routingToken, bool isSync)
Create message and send to Grpc server.
Definition: GrpcProxy.cs:81
virtual GrpcChannel GetChannel()
Gets the GrpcChannel used by the gRPC client.
Definition: GrpcProxy.cs:42
static void SetChannel(GrpcChannel channel)
Sets the GrpcChannel used by gRPC clients.
Definition: GrpcProxy.cs:56
virtual GrpcService.GrpcServiceClient GetGrpcClient()
Get gRPC client object used by data portal.
Definition: GrpcProxy.cs:67
GrpcProxy(ApplicationContext applicationContext, GrpcChannel channel, GrpcProxyOptions options)
Creates an instance of the object, initializing it to use the supplied GrpcChannel object and URL.
Definition: GrpcProxy.cs:28
string DataPortalUrl
Data portal server endpoint URL
Implements a data portal proxy to relay data portal calls to a remote application server.
string DataPortalUrl
Gets the URL address for the data portal server used by this proxy instance.