14using System.Threading.Tasks;
32 : base(applicationContext)
34 _httpClient = httpClient;
38 private static HttpClient _httpClient;
46 return new HttpClientHandler();
55 if (_httpClient ==
null)
60 handler =
new HttpClientHandler();
64 handler =
new HttpClientHandler()
66 AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
69 _httpClient =
new HttpClient(handler);
72 _httpClient.Timeout = TimeSpan.FromMilliseconds(this.
Timeout);
85 return new DefaultWebClient(this.
Timeout);
103 protected override async Task<byte[]>
CallDataPortalServer(
byte[] serialized,
string operation,
string routingToken,
bool isSync)
106 ? CallViaWebClient(serialized, operation, routingToken)
107 : await CallViaHttpClient(serialized, operation, routingToken);
118 private async Task<byte[]> CallViaHttpClient(
byte[] serialized,
string operation,
string routingToken)
121 HttpRequestMessage httpRequest;
122 httpRequest =
new HttpRequestMessage(
124 $
"{DataPortalUrl}?operation={CreateOperationTag(operation, ApplicationContext.VersionRoutingTag, routingToken)}");
127 httpRequest.Content =
new StringContent(System.Convert.ToBase64String(serialized));
129 httpRequest.Content =
new ByteArrayContent(serialized);
130 var httpResponse = await client.SendAsync(httpRequest);
131 await VerifyResponseSuccess(httpResponse);
133 serialized = Convert.FromBase64String(await httpResponse.Content.ReadAsStringAsync());
135 serialized = await httpResponse.Content.ReadAsByteArrayAsync();
139 private byte[] CallViaWebClient(
byte[] serialized,
string operation,
string routingToken)
141 if (!WebCallCapabilities.AreSyncWebClientMethodsSupported())
146 var url = $
"{DataPortalUrl}?operation={CreateOperationTag(operation, ApplicationContext.VersionRoutingTag, routingToken)}";
151 var result = client.UploadString(url, System.Convert.ToBase64String(serialized));
152 serialized = System.Convert.FromBase64String(result);
156 var result = client.UploadData(url, serialized);
161 catch (WebException ex)
164 using (var reader =
new System.IO.StreamReader(ex.Response.GetResponseStream()))
165 message = reader.ReadToEnd();
166 throw new DataPortalException(message, ex);
170 private static async Task VerifyResponseSuccess(HttpResponseMessage httpResponse)
172 if (!httpResponse.IsSuccessStatusCode)
174 var message =
new StringBuilder();
175 message.Append((
int)httpResponse.StatusCode);
176 message.Append(
": ");
177 message.Append(httpResponse.ReasonPhrase);
178 var content = await httpResponse.Content.ReadAsStringAsync();
179 if (!
string.IsNullOrWhiteSpace(content))
181 message.AppendLine();
182 message.Append(content);
184 throw new HttpRequestException(message.ToString());
188 private string CreateOperationTag(
string operatation,
string versionToken,
string routingToken)
190 if (!
string.IsNullOrWhiteSpace(versionToken) || !
string.IsNullOrWhiteSpace(routingToken))
191 return $
"{operatation}/{routingToken}-{versionToken}";
195#pragma warning disable SYSLIB0014
196 private class DefaultWebClient : WebClient
198 private int Timeout {
get;
set; }
200 public DefaultWebClient(
int timeout) =>
Timeout = timeout;
202 protected override WebRequest GetWebRequest(Uri address)
204 var req = base.GetWebRequest(address) as HttpWebRequest;
210#pragma warning restore SYSLIB0014
Provides consistent context information between the client and server DataPortal objects.
Implements a data portal proxy to relay data portal calls to a remote application server by using htt...
virtual WebClient GetWebClient()
Gets an WebClient object for use in communication with the server.
virtual HttpClientHandler GetHttpClientHandler()
Gets an HttpClientHandler for use in initializing the HttpClient instance.
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
virtual void SetHttpRequestHeaders(HttpRequestMessage request)
Override to set headers or other properties of the HttpRequestMessage before it is sent to the server...
virtual HttpClient GetHttpClient()
Gets an HttpClient object for use in communication with the server.
static bool UseTextSerialization
Gets or sets a value indicating whether to use text/string serialization instead of the default binar...
HttpProxy(ApplicationContext applicationContext, HttpClient httpClient, HttpProxyOptions options)
Creates an instance of the type, initializing it to use the supplied HttpClient object and options.
string DataPortalUrl
Data portal server endpoint URL
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.
A strongly-typed resource class, for looking up localized strings, etc.
static string SyncDataAccessNotSupportedException
Looks up a localized string similar to Synchronous data access is not supported on this runtime.