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.
HttpProxy.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="HttpProxy.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 Csla.Core;
11using Csla.Server;
12using System;
13using System.Linq;
14using System.Net;
15using System.Net.Http;
16using System.Text;
17using System.Threading;
18using System.Threading.Tasks;
19
21{
27 {
33 public HttpProxy()
34 {
35 }
36
42 public HttpProxy(string dataPortalUrl)
43 {
44 DataPortalUrl = dataPortalUrl;
45 }
46
52 public HttpProxy(HttpClient httpClient)
53 {
54 _httpClient = httpClient;
55 }
56
63 public HttpProxy(HttpClient httpClient, string dataPortalUrl)
64 {
65 _httpClient = httpClient;
66 DataPortalUrl = dataPortalUrl;
67 }
68
69 private static HttpClient _httpClient;
70
75 protected virtual HttpClientHandler GetHttpClientHandler()
76 {
77 return new HttpClientHandler();
78 }
79
84 protected virtual HttpClient GetHttpClient()
85 {
86 if (_httpClient == null)
87 {
88 var handler = GetHttpClientHandler();
90 {
91 handler = new HttpClientHandler();
92 }
93 else
94 {
95 handler = new HttpClientHandler()
96 {
97 AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
98 };
99 }
100 _httpClient = new HttpClient(handler);
101 if (Timeout > 0)
102 {
103 _httpClient.Timeout = TimeSpan.FromMilliseconds(this.Timeout);
104 }
105 }
106
107 return _httpClient;
108 }
109
114 protected virtual WebClient GetWebClient()
115 {
116 return new DefaultWebClient(this.Timeout);
117 }
118
124 public static bool UseTextSerialization { get; set; } = false;
125
134 protected override async Task<byte[]> CallDataPortalServer(byte[] serialized, string operation, string routingToken, bool isSync)
135 {
136 return isSync
137 ? CallViaWebClient(serialized, operation, routingToken)
138 : await CallViaHttpClient(serialized, operation, routingToken);
139 }
140
146 protected virtual void SetHttpRequestHeaders(HttpRequestMessage request)
147 { }
148
149 private async Task<byte[]> CallViaHttpClient(byte[] serialized, string operation, string routingToken)
150 {
151 HttpClient client = GetHttpClient();
152 HttpRequestMessage httpRequest;
153 httpRequest = new HttpRequestMessage(
154 HttpMethod.Post,
155 $"{DataPortalUrl}?operation={CreateOperationTag(operation, ApplicationContext.VersionRoutingTag, routingToken)}");
156 SetHttpRequestHeaders(httpRequest);
158 httpRequest.Content = new StringContent(System.Convert.ToBase64String(serialized));
159 else
160 httpRequest.Content = new ByteArrayContent(serialized);
161 var httpResponse = await client.SendAsync(httpRequest);
162 await VerifyResponseSuccess(httpResponse);
164 serialized = Convert.FromBase64String(await httpResponse.Content.ReadAsStringAsync());
165 else
166 serialized = await httpResponse.Content.ReadAsByteArrayAsync();
167 return serialized;
168 }
169
170 private byte[] CallViaWebClient(byte[] serialized, string operation, string routingToken)
171 {
172 WebClient client = GetWebClient();
173 var url = $"{DataPortalUrl}?operation={CreateOperationTag(operation, ApplicationContext.VersionRoutingTag, routingToken)}";
175 {
176 var result = client.UploadString(url, System.Convert.ToBase64String(serialized));
177 serialized = System.Convert.FromBase64String(result);
178 }
179 else
180 {
181 var result = client.UploadData(url, serialized);
182 serialized = result;
183 }
184 return serialized;
185 }
186
187 private static async Task VerifyResponseSuccess(HttpResponseMessage httpResponse)
188 {
189 if (!httpResponse.IsSuccessStatusCode)
190 {
191 var message = new StringBuilder();
192 message.Append((int)httpResponse.StatusCode);
193 message.Append(": ");
194 message.Append(httpResponse.ReasonPhrase);
195 var content = await httpResponse.Content.ReadAsStringAsync();
196 if (!string.IsNullOrWhiteSpace(content))
197 {
198 message.AppendLine();
199 message.Append(content);
200 }
201 throw new HttpRequestException(message.ToString());
202 }
203 }
204
205 private string CreateOperationTag(string operatation, string versionToken, string routingToken)
206 {
207 if (!string.IsNullOrWhiteSpace(versionToken) || !string.IsNullOrWhiteSpace(routingToken))
208 return $"{operatation}/{routingToken}-{versionToken}";
209 return operatation;
210 }
211
212 private class DefaultWebClient : WebClient
213 {
214 private int Timeout { get; set; }
215
216 public DefaultWebClient(int timeout)
217 {
218 Timeout = timeout;
219 }
220
221 protected override WebRequest GetWebRequest(Uri address)
222 {
223 var req = base.GetWebRequest(address) as HttpWebRequest;
224 if (Timeout > 0)
225 req.Timeout = Timeout;
226 return req;
227 }
228 }
229 }
230}
Implements a data portal proxy to relay data portal calls to a remote application server.
virtual int Timeout
Gets or sets the Client timeout in milliseconds (0 uses default timeout).
string DataPortalUrl
Gets the URL address for the data portal server used by this proxy instance.
Implements a data portal proxy to relay data portal calls to a remote application server by using htt...
Definition: HttpProxy.cs:27
virtual void SetHttpRequestHeaders(HttpRequestMessage request)
Override to set headers or other properties of the HttpRequestMessage before it is sent to the server...
Definition: HttpProxy.cs:146
virtual HttpClient GetHttpClient()
Gets an HttpClient object for use in communication with the server.
Definition: HttpProxy.cs:84
HttpProxy(string dataPortalUrl)
Creates an instance of the object, initializing it to use the supplied URL.
Definition: HttpProxy.cs:42
virtual WebClient GetWebClient()
Gets an WebClient object for use in communication with the server.
Definition: HttpProxy.cs:114
HttpProxy()
Creates an instance of the object, initializing it to use the DefaultUrl values.
Definition: HttpProxy.cs:33
static bool UseTextSerialization
Gets or sets a value indicating whether to use text/string serialization instead of the default binar...
Definition: HttpProxy.cs:124
virtual HttpClientHandler GetHttpClientHandler()
Gets an HttpClientHandler for use in initializing the HttpClient instance.
Definition: HttpProxy.cs:75
HttpProxy(HttpClient httpClient, string dataPortalUrl)
Creates an instance of the object, initializing it to use the supplied HttpClient object and URL.
Definition: HttpProxy.cs:63
HttpProxy(HttpClient httpClient)
Creates an instance of the object, initializing it to use the supplied HttpClient object.
Definition: HttpProxy.cs:52
override async Task< byte[]> CallDataPortalServer(byte[] serialized, string operation, string routingToken, bool isSync)
Select client to make request based on isSync parameter and return response from server
Definition: HttpProxy.cs:134