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

Commit e080e43

Browse files
committed
Make ServiceStack.Redis compilable on .NET Core
1 parent 1a4f22c commit e080e43

File tree

7 files changed

+144
-8
lines changed

7 files changed

+144
-8
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"ServiceStack.Common" : "4.0.*",
2121
},
2222
"compile": ["../../ServiceStack.Redis/**/*.cs"],
23+
"exclude": ["../../ServiceStack.Redis/RedisPipeline.cs"],
2324
"frameworks": {
2425
"netstandard1.3": {
2526
"dependencies" : {
@@ -37,7 +38,8 @@
3738
"System.Reflection.Emit" : "4.0.1",
3839
"System.Reflection.Emit.Lightweight": "4.0.1",
3940
"System.Collections.Specialized": "4.0.1",
40-
"System.Collections.NonGeneric": "4.0.1"
41+
"System.Collections.NonGeneric": "4.0.1",
42+
"System.Security.Cryptography.Algorithms" : "4.2.0"
4143
}
4244
}
4345

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#if NETSTANDARD
2+
using System;
3+
using System.IO;
4+
using System.Net.Sockets;
5+
6+
namespace ServiceStack.Redis
7+
{
8+
public sealed class BufferedStream : Stream
9+
{
10+
NetworkStream networkStream;
11+
12+
public BufferedStream(Stream stream)
13+
: this(stream, 0)
14+
{
15+
}
16+
17+
public BufferedStream(Stream stream, int bufferSize)
18+
{
19+
networkStream = stream as NetworkStream;
20+
21+
if (networkStream == null)
22+
throw new ArgumentNullException("stream");
23+
}
24+
25+
26+
/* public BufferedStream(Stream stream)
27+
: this(stream, 0)
28+
{
29+
}
30+
31+
public BufferedStream(Stream stream, int bufferSize) : base (stream)
32+
{
33+
if (stream == null)
34+
throw new ArgumentNullException("stream");
35+
}
36+
*/
37+
public override bool CanRead
38+
{
39+
get { return networkStream.CanRead; }
40+
}
41+
42+
public override bool CanSeek
43+
{
44+
get { return networkStream.CanSeek; }
45+
}
46+
47+
public override bool CanWrite
48+
{
49+
get { return networkStream.CanWrite; }
50+
}
51+
52+
public override long Position
53+
{
54+
get { return networkStream.Position; }
55+
set { networkStream.Position = value; }
56+
}
57+
58+
public override long Length
59+
{
60+
get { return networkStream.Length; }
61+
}
62+
63+
public override int Read(byte[] buffer, int offset, int length)
64+
{
65+
return networkStream.Read(buffer, offset, length);
66+
}
67+
68+
public override void Write(byte[] buffer, int offset, int length)
69+
{
70+
networkStream.Write(buffer, offset, length);
71+
}
72+
73+
public override void Flush()
74+
{
75+
networkStream.Flush();
76+
}
77+
78+
public override void SetLength(long length)
79+
{
80+
networkStream.SetLength(length);
81+
}
82+
83+
public override long Seek(long position, SeekOrigin origin)
84+
{
85+
return networkStream.Seek(position, origin);
86+
}
87+
}
88+
}
89+
#endif

src/ServiceStack.Redis/RedisNativeClient.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2480,19 +2480,31 @@ private void SafeConnectionClose()
24802480
{
24812481
// workaround for a .net bug: http://support.microsoft.com/kb/821625
24822482
if (Bstream != null)
2483+
#if NETSTANDARD
2484+
Bstream.Dispose();
2485+
#else
24832486
Bstream.Close();
2487+
#endif
24842488
}
24852489
catch { }
24862490
try
24872491
{
24882492
if (sslStream != null)
2493+
#if NETSTANDARD
2494+
sslStream.Dispose();
2495+
#else
24892496
sslStream.Close();
2497+
#endif
24902498
}
24912499
catch { }
24922500
try
24932501
{
24942502
if (socket != null)
2503+
#if NETSTANDARD
2504+
socket.Dispose();
2505+
#else
24952506
socket.Close();
2507+
#endif
24962508
}
24972509
catch { }
24982510

src/ServiceStack.Redis/RedisNativeClient_Utils.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ private void Connect()
8383
};
8484
try
8585
{
86+
#if NETSTANDARD
87+
socket.Connect(Host, Port);
88+
#else
8689
if (ConnectTimeout <= 0)
8790
{
8891
socket.Connect(Host, Port);
@@ -92,10 +95,15 @@ private void Connect()
9295
var connectResult = socket.BeginConnect(Host, Port, null, null);
9396
connectResult.AsyncWaitHandle.WaitOne(ConnectTimeout, true);
9497
}
98+
#endif
9599

96100
if (!socket.Connected)
97101
{
102+
#if NETSTANDARD
103+
socket.Dispose();
104+
#else
98105
socket.Close();
106+
#endif
99107
socket = null;
100108
DeactivatedAt = DateTime.UtcNow;
101109
return;
@@ -115,8 +123,13 @@ private void Connect()
115123
}
116124
else
117125
{
126+
#if NETSTANDARD
127+
var ctor = typeof(SslStream).GetTypeInfo().DeclaredConstructors
128+
.First(x => x.GetParameters().Length == 5);
129+
#else
118130
var ctor = typeof(SslStream).GetConstructors()
119131
.First(x => x.GetParameters().Length == 5);
132+
#endif
120133

121134
var policyType = AssemblyUtils.FindType("System.Net.Security.EncryptionPolicy");
122135
var policyValue = Enum.Parse(policyType, "RequireEncryption");
@@ -130,7 +143,11 @@ private void Connect()
130143
});
131144
}
132145

146+
#if NETSTANDARD
147+
sslStream.AuthenticateAsClientAsync(Host).Wait();
148+
#else
133149
sslStream.AuthenticateAsClient(Host);
150+
#endif
134151

135152
if (!sslStream.IsEncrypted)
136153
throw new Exception("Could not establish an encrypted connection to " + Host);
@@ -243,7 +260,11 @@ internal bool AssertConnectedSocket()
243260
log.Error(ErrorConnect.Fmt(Host, Port));
244261

245262
if (socket != null)
263+
#if NETSTANDARD
264+
socket.Dispose();
265+
#else
246266
socket.Close();
267+
#endif
247268

248269
socket = null;
249270

@@ -606,7 +627,11 @@ private Exception GetRetryableException(Exception outerEx)
606627
lastSocketException = socketEx;
607628

608629
if (socket != null)
630+
#if NETSTANDARD
631+
socket.Dispose();
632+
#else
609633
socket.Close();
634+
#endif
610635

611636
socket = null;
612637
return socketEx;
@@ -1206,8 +1231,13 @@ public string CalculateSha1(string luaBody)
12061231
throw new ArgumentNullException("luaBody");
12071232

12081233
byte[] buffer = Encoding.UTF8.GetBytes(luaBody);
1234+
#if NETSTANDARD
1235+
var sha1 = SHA1.Create();
1236+
return BitConverter.ToString(sha1.ComputeHash(buffer)).Replace("-", "");
1237+
#else
12091238
var cryptoTransformSHA1 = new SHA1CryptoServiceProvider();
12101239
return BitConverter.ToString(cryptoTransformSHA1.ComputeHash(buffer)).Replace("-", "");
1240+
#endif
12111241
}
12121242

12131243
public byte[] ScriptLoad(string luaBody)

src/ServiceStack.Redis/RedisSentinel.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,13 @@ public IRedisClientsManager Start()
177177
.ToArray();
178178

179179
var sentinelWorker = GetValidSentinelWorker();
180+
#if NETSTANDARD
181+
if (this.RedisManager == null || sentinelWorker == null)
182+
throw new Exception("Unable to resolve sentinels!");
183+
#else
180184
if (this.RedisManager == null || sentinelWorker == null)
181185
throw new ApplicationException("Unable to resolve sentinels!");
182-
186+
#endif
183187
return this.RedisManager;
184188
}
185189
}

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
#if !NETSTANDARD
2+
using System;
23
using System.Collections.Generic;
34
using System.Diagnostics;
45
using System.Linq;
@@ -31,12 +32,8 @@ public TrackingRedisClientsManager(IRedisClientsManager redisClientsManager)
3132
this.redisClientsManager = redisClientsManager;
3233
Logger.DebugFormat("Constructed");
3334

34-
#if NETSTANDARD
35-
var timer = new Timer(state => this.DumpState(), null, TimeSpan.FromSeconds(30), TimeSpan.FromMinutes(1));
36-
#else
3735
var timer = new Timer(state => this.DumpState());
3836
timer.Change(TimeSpan.FromSeconds(30), TimeSpan.FromMinutes(1));
39-
#endif
4037
}
4138

4239
public void Dispose()
@@ -138,3 +135,4 @@ private void DumpState()
138135
}
139136
}
140137
}
138+
#endif

src/ServiceStack.Redis/Support/OptimizedObjectSerializer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.IO;
33
using System.Text;
44
using ServiceStack.Text;
5+
using ServiceStack;
56

67
namespace ServiceStack.Redis.Support
78
{
@@ -60,7 +61,7 @@ SerializedObjectWrapper SerializeToWrapper(object value)
6061
return new SerializedObjectWrapper(RawDataFlag, new ArraySegment<byte>(tmpByteArray));
6162
}
6263

63-
TypeCode code = value == null ? TypeCode.DBNull : Type.GetTypeCode(value.GetType());
64+
TypeCode code = value == null ? TypeCode.DBNull : value.GetType().GetTypeCode();
6465

6566
byte[] data;
6667
int length = -1;

0 commit comments

Comments
 (0)