|
@@ -34,6 +34,8 @@ namespace InABox.Clients
|
|
|
|
|
|
public class JsonClient<TEntity> : RemoteClient<TEntity> where TEntity : Entity, new()
|
|
|
{
|
|
|
+ private static RestClient cli = null;
|
|
|
+
|
|
|
private readonly bool _compression = true;
|
|
|
|
|
|
private BinarySerializationSettings BinarySerializationSettings;
|
|
@@ -44,11 +46,16 @@ namespace InABox.Clients
|
|
|
BinarySerializationSettings = binarySerializationSettings;
|
|
|
}
|
|
|
|
|
|
- public JsonClient(string url, bool useSimpleEncryption) : this(url, useSimpleEncryption, true, BinarySerializationSettings.Latest)
|
|
|
+ public static class StaticClients
|
|
|
+ {
|
|
|
+ public static Dictionary<string, RestClient> Clients = new Dictionary<string, RestClient>();
|
|
|
+ }
|
|
|
+
|
|
|
+ public JsonClient(string url, bool useSimpleEncryption) : this(url, useSimpleEncryption, false, BinarySerializationSettings.Latest)
|
|
|
{
|
|
|
}
|
|
|
|
|
|
- public JsonClient(string url) : this(url, false, true, BinarySerializationSettings.Latest)
|
|
|
+ public JsonClient(string url) : this(url, false, false, BinarySerializationSettings.Latest)
|
|
|
{
|
|
|
}
|
|
|
|
|
@@ -113,7 +120,8 @@ namespace InABox.Clients
|
|
|
//sw.Restart();
|
|
|
|
|
|
var uri = new Uri(URL);
|
|
|
- var cli = new RestClient(uri);
|
|
|
+ if (cli == null)
|
|
|
+ SetClient(URL);
|
|
|
var cmd = string.Format(
|
|
|
"{0}{1}?format={2}&responseFormat={3}&serializationVersion={4}",
|
|
|
Action,
|
|
@@ -122,35 +130,35 @@ namespace InABox.Clients
|
|
|
responseFormat,
|
|
|
BinarySerializationSettings.Version
|
|
|
);
|
|
|
- var req = new RestRequest(cmd, Method.POST)
|
|
|
+ var req = new RestRequest(cmd, Method.Post)
|
|
|
{
|
|
|
Timeout = Timeout.Milliseconds,
|
|
|
};
|
|
|
//Log(" * {0}{1}() Creating Uri, Client and RestRequest took {2}ms", Action, typeof(TEntity).Name, sw.ElapsedMilliseconds);
|
|
|
//sw.Restart();
|
|
|
|
|
|
- req.AdvancedResponseWriter = (stream, response) =>
|
|
|
- {
|
|
|
- //Log(" * {0}{1}() Response from Server took {2}ms ({3} bytes)", Action, typeof(TEntity).Name, sw.ElapsedMilliseconds, response.ContentLength);
|
|
|
- //length = response.ContentLength;
|
|
|
- //sw.Restart();
|
|
|
- try
|
|
|
- {
|
|
|
- if (responseFormat == SerializationFormat.Binary && typeof(TResponse).HasInterface<ISerializeBinary>())
|
|
|
- {
|
|
|
- result = (TResponse)Serialization.ReadBinary(typeof(TResponse), stream, BinarySerializationSettings);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- result = Serialization.Deserialize<TResponse>(stream, true);
|
|
|
- }
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
- Logger.Send(LogType.Information, "", $"Error deserializing response: {e.Message}");
|
|
|
- }
|
|
|
- //Log(" * {0}{1}() Deserializing Stream took {2}ms ({3} bytes)", Action, typeof(TEntity).Name, sw.ElapsedMilliseconds, response.ContentLength);
|
|
|
- };
|
|
|
+ //req.AdvancedResponseWriter = (stream, response) =>
|
|
|
+ //{
|
|
|
+ // //Log(" * {0}{1}() Response from Server took {2}ms ({3} bytes)", Action, typeof(TEntity).Name, sw.ElapsedMilliseconds, response.ContentLength);
|
|
|
+ // //length = response.ContentLength;
|
|
|
+ // //sw.Restart();
|
|
|
+ // try
|
|
|
+ // {
|
|
|
+ // if (responseFormat == SerializationFormat.Binary && typeof(TResponse).HasInterface<ISerializeBinary>())
|
|
|
+ // {
|
|
|
+ // result = (TResponse)Serialization.ReadBinary(typeof(TResponse), stream, BinarySerializationSettings);
|
|
|
+ // }
|
|
|
+ // else
|
|
|
+ // {
|
|
|
+ // result = Serialization.Deserialize<TResponse>(stream, true);
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // catch (Exception e)
|
|
|
+ // {
|
|
|
+ // Logger.Send(LogType.Information, "", $"Error deserializing response: {e.Message}");
|
|
|
+ // }
|
|
|
+ // //Log(" * {0}{1}() Deserializing Stream took {2}ms ({3} bytes)", Action, typeof(TEntity).Name, sw.ElapsedMilliseconds, response.ContentLength);
|
|
|
+ //};
|
|
|
|
|
|
if(requestFormat == SerializationFormat.Binary && request is ISerializeBinary binary)
|
|
|
{
|
|
@@ -163,7 +171,7 @@ namespace InABox.Clients
|
|
|
{
|
|
|
var json = Serialization.Serialize(request);
|
|
|
|
|
|
- req.AddOrUpdateParameter("application/json; charset=utf-8", json, ParameterType.RequestBody);
|
|
|
+ req.AddOrUpdateParameter("application/json", json, ParameterType.RequestBody);
|
|
|
req.RequestFormat = DataFormat.Json;
|
|
|
}
|
|
|
try
|
|
@@ -235,16 +243,27 @@ namespace InABox.Clients
|
|
|
}
|
|
|
|
|
|
req = null;
|
|
|
- cli = null;
|
|
|
//double elapsed = (DateTime.Now - now).TotalMilliseconds;
|
|
|
//Log(" * {0}{1}() completed in {2:F0}ms", Action, typeof(TEntity).Name, elapsed);
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
+ private void SetClient(string uRL)
|
|
|
+ {
|
|
|
+ var uri = new Uri(URL);
|
|
|
+ if (StaticClients.Clients.ContainsKey(URL))
|
|
|
+ cli = StaticClients.Clients[URL];
|
|
|
+ else
|
|
|
+ {
|
|
|
+ cli = new RestClient(uri);
|
|
|
+ StaticClients.Clients.Add(URL, cli);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
// SupportedTypes() function fallback for if the server is running on ServiceStack
|
|
|
private void SupportedTypesOld(RestClient cli, List<string> result)
|
|
|
{
|
|
|
- var req = new RestRequest("/operations/metadata?format=csv", Method.GET) { Timeout = 20000 };
|
|
|
+ var req = new RestRequest("/operations/metadata?format=csv", Method.Get) { Timeout = 20000 };
|
|
|
|
|
|
try
|
|
|
{
|
|
@@ -277,7 +296,7 @@ namespace InABox.Clients
|
|
|
var result = new List<string>();
|
|
|
var uri = new Uri(URL);
|
|
|
var cli = new RestClient(uri);
|
|
|
- var req = new RestRequest("/classes", Method.GET) { Timeout = 20000 };
|
|
|
+ var req = new RestRequest("/classes", Method.Get) { Timeout = 20000 };
|
|
|
|
|
|
try
|
|
|
{
|
|
@@ -311,7 +330,7 @@ namespace InABox.Clients
|
|
|
{
|
|
|
var uri = new Uri(URL);
|
|
|
var cli = new RestClient(uri);
|
|
|
- var req = new RestRequest("/info", Method.GET) { Timeout = 20000 };
|
|
|
+ var req = new RestRequest("/info", Method.Get) { Timeout = 20000 };
|
|
|
|
|
|
try
|
|
|
{
|