Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 1a4f22c

Browse files
committed
First iteration of making ServiceStack.Redis compilable on .NET Core
1 parent 4f70932 commit 1a4f22c

File tree

13 files changed

+94
-15
lines changed

13 files changed

+94
-15
lines changed

src/ServiceStack.Redis.NetCore/ServiceStack.Interfaces/project.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
"configurations": {
33
"Debug": {
44
"buildOptions": {
5-
"define": ["NETFX_CORE", "DEBUG", "TRACE"]
5+
"define": ["NETSTANDARD", "DEBUG", "TRACE"]
66
}
77
},
88
"Release": {
99
"buildOptions": {
10-
"define": ["NETFX_CORE", "TRACE"],
10+
"define": ["NETSTANDARD", "TRACE"],
1111
"optimize": true
1212
}
1313
}
@@ -30,5 +30,5 @@
3030
"bin" : { "assembly":"../../../lib/netcore/ServiceStack.Interfaces.dll", "pdb" : "../../../lib/netcore/ServiceStack.Interfaces.pdb" }
3131
}
3232
},
33-
"version": "4.1.0-*"
33+
"version": "4.0.61"
3434
}

src/ServiceStack.Redis.NetCore/ServiceStack.Redis/project.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,30 @@
1414
},
1515
"dependencies": {
1616
"NETStandard.Library": "1.6.0",
17+
"System.Runtime.Serialization.Formatters": "4.0.0-*",
1718
"ServiceStack.Interfaces" : "4.0.*",
1819
"ServiceStack.Text" : "4.0.*",
1920
"ServiceStack.Common" : "4.0.*",
2021
},
2122
"compile": ["../../ServiceStack.Redis/**/*.cs"],
2223
"frameworks": {
23-
"netstandard1.1": {
24+
"netstandard1.3": {
2425
"dependencies" : {
2526
"System.Linq.Expressions" : "4.1.0",
2627
"System.Runtime.Serialization.Primitives" : "4.1.1-*",
2728
"System.Runtime.Serialization.Xml" : "4.1.1",
2829
"System.Net.Http" : "4.1.0",
2930
"System.Net.Requests" : "4.0.11",
31+
"System.Net.Security" : "4.0.0",
3032
"System.IO" : "4.1.0",
3133
"System.Dynamic.Runtime" : "4.0.11",
3234
"Microsoft.CSharp" : "4.0.1-*",
3335
"System.Reflection": "4.1.0-*",
3436
"System.Reflection.Extensions" : "4.0.1",
3537
"System.Reflection.Emit" : "4.0.1",
36-
"System.Reflection.Emit.Lightweight": "4.0.1"
38+
"System.Reflection.Emit.Lightweight": "4.0.1",
39+
"System.Collections.Specialized": "4.0.1",
40+
"System.Collections.NonGeneric": "4.0.1"
3741
}
3842
}
3943

src/ServiceStack.Redis/RedisNativeClient_Utils.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using System.Security.Cryptography;
2222
using System.Text;
2323
using System.Threading;
24+
using System.Threading.Tasks;
2425
using ServiceStack.Text;
2526

2627
namespace ServiceStack.Redis
@@ -575,7 +576,11 @@ protected T SendReceive<T>(byte[][] cmdWithBinaryArgs,
575576
}
576577

577578
Interlocked.Increment(ref RedisState.TotalRetryCount);
579+
#if NETSTANDARD
580+
Task.Delay(GetBackOffMultiplier(++i));
581+
#else
578582
Thread.Sleep(GetBackOffMultiplier(++i));
583+
#endif
579584
}
580585
}
581586
}

src/ServiceStack.Redis/RedisPubSubServer.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Diagnostics;
33
using System.Text;
44
using System.Threading;
5+
using System.Threading.Tasks;
56
using ServiceStack.Logging;
67
using ServiceStack.Text;
78

@@ -40,7 +41,9 @@ public class RedisPubSubServer : IRedisPubSubServer
4041
private int noOfContinuousErrors = 0;
4142
private string lastExMsg = null;
4243
private int status;
44+
#if !NETSTANDARD
4345
private Thread bgThread; //Subscription controller thread
46+
#endif
4447
private long bgThreadCount = 0;
4548

4649
private const int NO = 0;
@@ -108,6 +111,9 @@ public IRedisPubSubServer Start()
108111
if (OnStart != null)
109112
OnStart();
110113

114+
#if NETSTANDARD
115+
RunLoop();
116+
#else
111117
//Don't kill us if we're the thread that's retrying to Start() after a failure.
112118
if (bgThread != Thread.CurrentThread)
113119
{
@@ -126,6 +132,7 @@ public IRedisPubSubServer Start()
126132
Log.Debug("Retrying RunLoop() on Thread: " + bgThread.Name);
127133
RunLoop();
128134
}
135+
#endif
129136
}
130137
catch (Exception ex)
131138
{
@@ -327,8 +334,11 @@ private void RunLoop()
327334
if (AutoRestart && Interlocked.CompareExchange(ref status, 0, 0) != Status.Disposed)
328335
{
329336
if (WaitBeforeNextRestart != null)
337+
#if NETSTANDARD
338+
Task.Delay(WaitBeforeNextRestart.Value);
339+
#else
330340
Thread.Sleep(WaitBeforeNextRestart.Value);
331-
341+
#endif
332342
Start();
333343
}
334344
}
@@ -428,6 +438,7 @@ public void Restart()
428438
Stop(shouldRestart:true);
429439
}
430440

441+
#if !NETSTANDARD
431442
private void KillBgThreadIfExists()
432443
{
433444
if (bgThread != null && bgThread.IsAlive)
@@ -447,6 +458,7 @@ private void KillBgThreadIfExists()
447458
bgThread = null;
448459
}
449460
}
461+
#endif
450462

451463
private void SleepBackOffMultiplier(int continuousErrorsCount)
452464
{
@@ -461,7 +473,11 @@ private void SleepBackOffMultiplier(int continuousErrorsCount)
461473
if (Log.IsDebugEnabled)
462474
Log.Debug("Sleeping for {0}ms after {1} continuous errors".Fmt(nextTry, continuousErrorsCount));
463475

476+
#if NETSTANDARD
477+
Task.Delay(nextTry);
478+
#else
464479
Thread.Sleep(nextTry);
480+
#endif
465481
}
466482

467483
public static class Operation //dep-free copy of WorkerOperation
@@ -555,6 +571,7 @@ public virtual void Dispose()
555571
Log.Error("Error OnDispose(): ", ex);
556572
}
557573

574+
#if !NETSTANDARD
558575
try
559576
{
560577
Thread.Sleep(100); //give it a small chance to die gracefully
@@ -564,6 +581,7 @@ public virtual void Dispose()
564581
{
565582
if (this.OnError != null) this.OnError(ex);
566583
}
584+
#endif
567585

568586
DisposeHeartbeatTimer();
569587
}

src/ServiceStack.Redis/RedisSentinel.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Collections.Generic;
1010
using System.Linq;
1111
using System.Threading;
12+
using System.Threading.Tasks;
1213
using ServiceStack;
1314
using ServiceStack.Logging;
1415
using ServiceStack.Text;
@@ -329,8 +330,11 @@ private RedisSentinelWorker GetValidSentinelWorker()
329330
}
330331

331332
this.failures = 0; //reset
333+
#if NETSTANDARD
334+
Task.Delay(WaitBetweenFailedHosts);
335+
#else
332336
Thread.Sleep(WaitBetweenFailedHosts);
333-
337+
#endif
334338
throw new RedisException("No Redis Sentinels were available", lastEx);
335339
}
336340

src/ServiceStack.Redis/RedisSentinelResolver.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Diagnostics;
44
using System.Linq;
55
using System.Threading;
6+
using System.Threading.Tasks;
67
using ServiceStack.Logging;
78
using ServiceStack.Text;
89

@@ -150,7 +151,11 @@ public virtual RedisClient CreateRedisClient(RedisEndpoint config, bool master)
150151
Interlocked.Increment(ref RedisState.TotalForcedMasterFailovers);
151152

152153
sentinel.ForceMasterFailover();
154+
#if NETSTANDARD
155+
Task.Delay(sentinel.WaitBetweenFailedHosts);
156+
#else
153157
Thread.Sleep(sentinel.WaitBetweenFailedHosts);
158+
#endif
154159
role = client.GetServerRole();
155160
}
156161
}
@@ -191,7 +196,11 @@ public virtual RedisClient CreateRedisClient(RedisEndpoint config, bool master)
191196
throw new TimeoutException("Max Wait Between Sentinel Lookups Elapsed: {0}"
192197
.Fmt(sentinel.MaxWaitBetweenFailedHosts.ToString()));
193198

199+
#if NETSTANDARD
200+
Task.Delay(sentinel.WaitBetweenFailedHosts);
201+
#else
194202
Thread.Sleep(sentinel.WaitBetweenFailedHosts);
203+
#endif
195204
}
196205
}
197206
catch (Exception ex)

src/ServiceStack.Redis/Support/Diagnostic/TrackingRedisClientProxy.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#if !NETSTANDARD
12
using System;
23
using System.Reflection;
34
using System.Runtime.Remoting.Messaging;
@@ -53,4 +54,5 @@ public override IMessage Invoke(IMessage msg)
5354
}
5455
}
5556
}
56-
}
57+
}
58+
#endif

src/ServiceStack.Redis/Support/Diagnostic/TrackingRedisClientsManager.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,12 @@ public TrackingRedisClientsManager(IRedisClientsManager redisClientsManager)
3131
this.redisClientsManager = redisClientsManager;
3232
Logger.DebugFormat("Constructed");
3333

34+
#if NETSTANDARD
35+
var timer = new Timer(state => this.DumpState(), null, TimeSpan.FromSeconds(30), TimeSpan.FromMinutes(1));
36+
#else
3437
var timer = new Timer(state => this.DumpState());
3538
timer.Change(TimeSpan.FromSeconds(30), TimeSpan.FromMinutes(1));
39+
#endif
3640
}
3741

3842
public void Dispose()

src/ServiceStack.Redis/Support/Locking/DistributedLock.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ public virtual long Lock(string key, int acquisitionTimeout, int lockTimeout, ou
4040
int count = 0;
4141
while (wasSet == 0 && count < tryCount && totalTime < acquisitionTimeout)
4242
{
43+
#if NETSTANDARD
44+
System.Threading.Tasks.Task.Delay(sleepIfLockSet);
45+
#else
4346
System.Threading.Thread.Sleep(sleepIfLockSet);
47+
#endif
4448
totalTime += sleepIfLockSet;
4549
ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0));
4650
newLockExpire = CalculateLockExpire(ts, lockTimeout);
@@ -78,7 +82,11 @@ public virtual long Lock(string key, int acquisitionTimeout, int lockTimeout, ou
7882
}
7983
}
8084
if (wasSet != LOCK_NOT_ACQUIRED) break;
85+
#if NETSTANDARD
86+
System.Threading.Tasks.Task.Delay(sleepIfLockSet);
87+
#else
8188
System.Threading.Thread.Sleep(sleepIfLockSet);
89+
#endif
8290
totalTime += sleepIfLockSet;
8391
}
8492
if (wasSet != LOCK_NOT_ACQUIRED)

src/ServiceStack.Redis/Support/ObjectSerializer.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System.IO;
2+
#if !NETSTANDARD
23
using System.Runtime.Serialization.Formatters.Binary;
4+
#endif
35

46
namespace ServiceStack.Redis.Support
57
{
@@ -9,8 +11,9 @@ namespace ServiceStack.Redis.Support
911
/// </summary>
1012
public class ObjectSerializer : ISerializer
1113
{
14+
#if !NETSTANDARD
1215
protected readonly BinaryFormatter bf = new BinaryFormatter();
13-
16+
#endif
1417

1518

1619
/// <summary>
@@ -20,12 +23,16 @@ public class ObjectSerializer : ISerializer
2023
/// <returns></returns>
2124
public virtual byte[] Serialize(object value)
2225
{
26+
#if NETSTANDARD
27+
return null;
28+
#else
2329
if (value == null)
2430
return null;
2531
var memoryStream = new MemoryStream();
2632
memoryStream.Seek(0, 0);
2733
bf.Serialize(memoryStream, value);
2834
return memoryStream.ToArray();
35+
#endif
2936
}
3037

3138
/// <summary>
@@ -35,13 +42,17 @@ public virtual byte[] Serialize(object value)
3542
/// <returns></returns>
3643
public virtual object Deserialize(byte[] someBytes)
3744
{
45+
#if NETSTANDARD
46+
return null;
47+
#else
3848
if (someBytes == null)
3949
return null;
4050
var memoryStream = new MemoryStream();
4151
memoryStream.Write(someBytes, 0, someBytes.Length);
4252
memoryStream.Seek(0, 0);
4353
var de = bf.Deserialize(memoryStream);
4454
return de;
55+
#endif
4556
}
4657
}
4758
}

0 commit comments

Comments
 (0)