CSLA.NET 5.4.2
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;
9using System.Linq;
10using System.Net.Http;
11using System.Threading;
12using System.Threading.Tasks;
13using Csla.Core;
15using Csla.Server;
16using Google.Protobuf;
17using Grpc.Net.Client;
20
21namespace Csla.Channels.Grpc
22{
28 {
34 public GrpcProxy(string dataPortalUrl)
35 : this(null, dataPortalUrl)
36 { }
37
43 public GrpcProxy(GrpcChannel channel)
44 : this(channel, ApplicationContext.DataPortalUrlString)
45 { }
46
53 public GrpcProxy(GrpcChannel channel, string dataPortalUrl)
54 {
55 _channel = channel;
56 DataPortalUrl = dataPortalUrl;
57 }
58
59 private GrpcChannel _channel;
60 private static GrpcChannel _defaultChannel;
61
66 protected virtual GrpcChannel GetChannel()
67 {
68 if (_channel == null)
69 {
70 _defaultChannel ??= GrpcChannel.ForAddress(DataPortalUrl);
71 _channel = _defaultChannel;
72 }
73 return _channel;
74 }
75
80 protected static void SetChannel(GrpcChannel channel)
81 {
82 _defaultChannel = channel;
83 }
84
85 private GrpcService.GrpcServiceClient _grpcClient;
86
91 protected virtual GrpcService.GrpcServiceClient GetGrpcClient()
92 {
93 return _grpcClient ??= new GrpcService.GrpcServiceClient(GetChannel());
94 }
95
105 protected override async Task<byte[]> CallDataPortalServer(byte[] serialized, string operation, string routingToken, bool isSync)
106 {
107 ByteString outbound = ByteString.CopyFrom(serialized);
108 var request = new RequestMessage
109 {
110 Body = outbound,
111 Operation = CreateOperationTag(operation, ApplicationContext.VersionRoutingTag, routingToken)
112 };
113 ResponseMessage response;
114 if (isSync)
115 response = GetGrpcClient().Invoke(request);
116 else
117 response = await GetGrpcClient().InvokeAsync(request);
118 return response.Body.ToByteArray();
119 }
120
121 internal async Task<ResponseMessage> RouteMessage(RequestMessage request)
122 {
123 return await GetGrpcClient().InvokeAsync(request);
124 }
125
126 private string CreateOperationTag(string operatation, string versionToken, string routingToken)
127 {
128 if (!string.IsNullOrWhiteSpace(versionToken) || !string.IsNullOrWhiteSpace(routingToken))
129 return $"{operatation}/{routingToken}-{versionToken}";
130 return operatation;
131 }
132 }
133}
Implements a data portal proxy to relay data portal calls to a remote application server by using gRP...
Definition: GrpcProxy.cs:28
override async Task< byte[]> CallDataPortalServer(byte[] serialized, string operation, string routingToken, bool isSync)
Create message and send to Grpc server.
Definition: GrpcProxy.cs:105
GrpcProxy(GrpcChannel channel)
Creates an instance of the object, initializing it to use the supplied GrpcChannel object.
Definition: GrpcProxy.cs:43
virtual GrpcChannel GetChannel()
Gets the GrpcChannel used by the gRPC client.
Definition: GrpcProxy.cs:66
GrpcProxy(string dataPortalUrl)
Creates an instance of the object, initializing it to use the supplied URL.
Definition: GrpcProxy.cs:34
GrpcProxy(GrpcChannel channel, string dataPortalUrl)
Creates an instance of the object, initializing it to use the supplied GrpcChannel object and URL.
Definition: GrpcProxy.cs:53
static void SetChannel(GrpcChannel channel)
Sets the GrpcChannel used by gRPC clients.
Definition: GrpcProxy.cs:80
virtual GrpcService.GrpcServiceClient GetGrpcClient()
Get gRPC client object used by data portal.
Definition: GrpcProxy.cs:91
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.