Skip to content

Commit 76c6454

Browse files
committed
MongoDB.Driver.Core compile with dotnet core
1 parent 540bc4b commit 76c6454

14 files changed

+238
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,4 @@ Tools/FAKE
6161
Tools/xunit.runner.console
6262

6363
src/MongoDB.Bson/project.lock.json
64+
src/MongoDB.Driver.Core/project.lock.json

mongo-csharp-driver.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1212
EndProject
1313
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "MongoDB.Bson", "src\MongoDB.Bson\MongoDB.Bson.xproj", "{E6B739E6-DDD8-4BA5-8595-FD59A0783153}"
1414
EndProject
15+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "MongoDB.Driver.Core", "src\MongoDB.Driver.Core\MongoDB.Driver.Core.xproj", "{B86DE81A-6CAB-458D-852C-358114C146DB}"
16+
EndProject
1517
Global
1618
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1719
Debug|Any CPU = Debug|Any CPU
@@ -22,11 +24,16 @@ Global
2224
{E6B739E6-DDD8-4BA5-8595-FD59A0783153}.Debug|Any CPU.Build.0 = Debug|Any CPU
2325
{E6B739E6-DDD8-4BA5-8595-FD59A0783153}.Release|Any CPU.ActiveCfg = Release|Any CPU
2426
{E6B739E6-DDD8-4BA5-8595-FD59A0783153}.Release|Any CPU.Build.0 = Release|Any CPU
27+
{B86DE81A-6CAB-458D-852C-358114C146DB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
28+
{B86DE81A-6CAB-458D-852C-358114C146DB}.Debug|Any CPU.Build.0 = Debug|Any CPU
29+
{B86DE81A-6CAB-458D-852C-358114C146DB}.Release|Any CPU.ActiveCfg = Release|Any CPU
30+
{B86DE81A-6CAB-458D-852C-358114C146DB}.Release|Any CPU.Build.0 = Release|Any CPU
2531
EndGlobalSection
2632
GlobalSection(SolutionProperties) = preSolution
2733
HideSolutionNode = FALSE
2834
EndGlobalSection
2935
GlobalSection(NestedProjects) = preSolution
3036
{E6B739E6-DDD8-4BA5-8595-FD59A0783153} = {B4E459B4-5B8C-4933-BECC-AEB36CEC84F6}
37+
{B86DE81A-6CAB-458D-852C-358114C146DB} = {B4E459B4-5B8C-4933-BECC-AEB36CEC84F6}
3138
EndGlobalSection
3239
EndGlobal

src/MongoDB.Bson/project.json

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "1.0.0-*",
2+
"version": "2.2.4-*",
33
"dependencies": {
44
"NETStandard.Library": "1.5.0-rc2-24027",
55
"System.Dynamic.Runtime": "4.0.11-rc2-24027",
@@ -10,15 +10,12 @@
1010
"System.Reflection.Emit.Lightweight": "4.0.1-rc2-24027",
1111
"System.Reflection.Extensions": "4.0.1-rc2-24027",
1212
"System.Runtime.InteropServices": "4.1.0-rc2-24027",
13-
"System.Runtime.Serialization.Formatters": "4.0.1-beta-24225-02",
14-
"System.Reflection.TypeExtensions": "4.1.0-rc2-24027"
13+
"System.Reflection.TypeExtensions": "4.1.0-rc2-24027",
14+
"System.Runtime.Serialization.Formatters": "4.0.0-rc4-24214-01"
1515
},
1616
"frameworks": {
1717
"netstandard1.5": {
1818
"imports": [ "dnxcore50", "portable-net451+win8" ]
1919
}
20-
},
21-
"buildOptions": {
22-
"defines": [ ]
2320
}
2421
}

src/MongoDB.Driver.Core/Core/Authentication/AuthenticationHelper.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ public static string MongoPasswordDigest(string username, SecureString password)
6969
IntPtr unmanagedPassword = IntPtr.Zero;
7070
try
7171
{
72+
#if NETCORE50 || NETSTANDARD1_5
73+
unmanagedPassword = SecureStringMarshal.SecureStringToCoTaskMemUnicode(password);
74+
#else
7275
unmanagedPassword = Marshal.SecureStringToBSTR(password);
76+
#endif
7377
var passwordChars = new char[password.Length];
7478
GCHandle passwordCharsHandle = new GCHandle();
7579
try
@@ -115,7 +119,11 @@ public static string MongoPasswordDigest(string username, SecureString password)
115119
{
116120
if (unmanagedPassword != IntPtr.Zero)
117121
{
122+
#if NETCORE50 || NETSTANDARD1_5
123+
SecureStringMarshal.ZeroFreeCoTaskMemUnicode(unmanagedPassword);
124+
#else
118125
Marshal.ZeroFreeBSTR(unmanagedPassword);
126+
#endif
119127
}
120128
}
121129
}

src/MongoDB.Driver.Core/Core/Authentication/ScramSha1Authenticator.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,11 @@ private static byte[] Hi(string password, byte[] salt, int iterations)
209209

210210
private static byte[] HMAC(UTF8Encoding encoding, byte[] data, string key)
211211
{
212+
#if NETCORE50 || NETSTANDARD1_5
213+
using (var hmac = new HMACSHA1(data))
214+
#else
212215
using (var hmac = new HMACSHA1(data, true))
216+
#endif
213217
{
214218
return hmac.ComputeHash(encoding.GetBytes(key));
215219
}

src/MongoDB.Driver.Core/Core/Authentication/UsernamePasswordCredential.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,22 @@ public string GetInsecurePassword()
9999
IntPtr unmanagedPassword = IntPtr.Zero;
100100
try
101101
{
102+
#if NETCORE50 || NETSTANDARD1_5
103+
unmanagedPassword = SecureStringMarshal.SecureStringToCoTaskMemUnicode(_password);
104+
#else
102105
unmanagedPassword = Marshal.SecureStringToGlobalAllocUnicode(_password);
106+
#endif
103107
return Marshal.PtrToStringUni(unmanagedPassword);
104108
}
105109
finally
106110
{
107111
if (unmanagedPassword != IntPtr.Zero)
108112
{
113+
#if NETCORE50 || NETSTANDARD1_5
114+
SecureStringMarshal.ZeroFreeCoTaskMemUnicode(unmanagedPassword);
115+
#else
109116
Marshal.ZeroFreeGlobalAllocUnicode(unmanagedPassword);
117+
#endif
110118
}
111119
}
112120
}

src/MongoDB.Driver.Core/Core/Configuration/SslStreamSettings.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ public SslStreamSettings(
5353
_checkCertificateRevocation = checkCertificateRevocation.WithDefault(true);
5454
_clientCertificates = Ensure.IsNotNull(clientCertificates.WithDefault(Enumerable.Empty<X509Certificate>()), "clientCertificates").ToList();
5555
_clientCertificateSelectionCallback = clientCertificateSelectionCallback.WithDefault(null);
56+
#if NETCORE50 || NETSTANDARD1_5
57+
_enabledSslProtocols = enabledProtocols.WithDefault(SslProtocols.Tls|SslProtocols.Ssl3);
58+
#else
5659
_enabledSslProtocols = enabledProtocols.WithDefault(SslProtocols.Default);
60+
#endif
5761
_serverCertificateValidationCallback = serverCertificateValidationCallback.WithDefault(null);
5862
}
5963

src/MongoDB.Driver.Core/Core/Connections/SslStreamFactory.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ public Stream CreateStream(EndPoint endPoint, CancellationToken cancellationToke
4747
var sslStream = CreateSslStream(stream);
4848
var targetHost = GetTargetHost(endPoint);
4949
var clientCertificates = new X509CertificateCollection(_settings.ClientCertificates.ToArray());
50+
#if NETCORE50 || NETSTANDARD1_5
51+
sslStream.AuthenticateAsClientAsync(targetHost, clientCertificates, _settings.EnabledSslProtocols, _settings.CheckCertificateRevocation).GetAwaiter().GetResult();
52+
#else
5053
sslStream.AuthenticateAsClient(targetHost, clientCertificates, _settings.EnabledSslProtocols, _settings.CheckCertificateRevocation);
54+
#endif
5155
return sslStream;
5256
}
5357
catch

src/MongoDB.Driver.Core/Core/Connections/TcpStreamFactory.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,12 @@ private async Task ConnectAsync(Socket socket, EndPoint endPoint, CancellationTo
129129
{
130130
try
131131
{
132+
#if NETCORE50 || NETSTANDARD1_5
133+
await Task.Run(() =>
134+
{
135+
socket.Connect(endPoint);
136+
});
137+
#else
132138
var dnsEndPoint = endPoint as DnsEndPoint;
133139
if (dnsEndPoint != null)
134140
{
@@ -139,6 +145,7 @@ private async Task ConnectAsync(Socket socket, EndPoint endPoint, CancellationTo
139145
{
140146
await Task.Factory.FromAsync(socket.BeginConnect(endPoint, null, null), socket.EndConnect).ConfigureAwait(false);
141147
}
148+
#endif
142149
connected = true;
143150
return;
144151
}

src/MongoDB.Driver.Core/Core/Events/EventContext.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,47 @@
1414
*/
1515

1616
using System;
17+
#if NETCORE50 || NETSTANDARD1_5
18+
using System.Threading;
19+
#else
1720
using System.Runtime.Remoting.Messaging;
21+
#endif
1822
using MongoDB.Driver.Core.Misc;
1923

24+
2025
namespace MongoDB.Driver.Core.Events
2126
{
2227
internal static class EventContext
2328
{
29+
#if NETCORE50 || NETSTANDARD1_5
30+
private static AsyncLocal<int?> findOperationBatchSize = new AsyncLocal<int?>();
31+
public static int? FindOperationBatchSize
32+
{
33+
get { return findOperationBatchSize.Value; }
34+
private set { findOperationBatchSize.Value = value; }
35+
}
36+
37+
private static AsyncLocal<int?> findOperationLimit = new AsyncLocal<int?>();
38+
public static int? FindOperationLimit
39+
{
40+
get { return findOperationLimit.Value; }
41+
private set { findOperationLimit.Value = value; }
42+
}
43+
44+
private static AsyncLocal<CollectionNamespace> killCursorsCollectionNamespace = new AsyncLocal<CollectionNamespace>();
45+
public static CollectionNamespace KillCursorsCollectionNamespace
46+
{
47+
get { return killCursorsCollectionNamespace.Value; }
48+
private set { killCursorsCollectionNamespace.Value = value; }
49+
}
50+
51+
private static AsyncLocal<long?> operationId = new AsyncLocal<long?>();
52+
public static long? OperationId
53+
{
54+
get { return operationId.Value; }
55+
private set { operationId.Value = value; }
56+
}
57+
#else
2458
private static readonly string __findBatchSize = "__MONGODB.FIND_OPERATION_BATCH_SIZE__";
2559
private static readonly string __findLimit = "__MONGODB.FIND_OPERATION_LIMIT__";
2660
private static readonly string __killCursorsNamespace = "__MONGODB.KILL_CURSORS_OPERATION_COLLECTION_NAMESPACE__";
@@ -77,6 +111,8 @@ private set
77111
CallContext.LogicalSetData(__operationId, value);
78112
}
79113
}
114+
#endif
115+
80116

81117
public static IDisposable BeginFind(int? batchSize, int? limit)
82118
{

src/MongoDB.Driver.Core/Core/Misc/RNGCryptoServiceProviderRandomStringGenerator.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,18 @@ internal class RNGCryptoServiceProviderRandomStringGenerator : IRandomStringGene
2323
public string Generate(int length, string legalCharacters)
2424
{
2525
var randomData = new byte[length];
26+
#if NETCORE50 || NETSTANDARD1_5
27+
using (var rng = RandomNumberGenerator.Create())
28+
{
29+
rng.GetBytes(randomData);
30+
}
31+
#else
2632
using (var rng = new RNGCryptoServiceProvider())
2733
{
2834
rng.GetBytes(randomData);
2935
}
36+
#endif
37+
3038

3139
var sb = new StringBuilder(length);
3240
for (int i = 0; i < length; i++)

src/MongoDB.Driver.Core/Hasher.cs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/* Copyright 2010-2014 MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System;
17+
using System.Collections;
18+
using System.Collections.Generic;
19+
20+
namespace MongoDB.Shared
21+
{
22+
internal class Hasher
23+
{
24+
// private fields
25+
private int _hashCode;
26+
27+
// constructors
28+
public Hasher()
29+
{
30+
_hashCode = 17;
31+
}
32+
33+
public Hasher(int seed)
34+
{
35+
_hashCode = seed;
36+
}
37+
38+
// public methods
39+
public override int GetHashCode()
40+
{
41+
return _hashCode;
42+
}
43+
44+
// this overload added to avoid boxing
45+
public Hasher Hash(bool obj)
46+
{
47+
_hashCode = 37 * _hashCode + obj.GetHashCode();
48+
return this;
49+
}
50+
51+
// this overload added to avoid boxing
52+
public Hasher Hash(int obj)
53+
{
54+
_hashCode = 37 * _hashCode + obj.GetHashCode();
55+
return this;
56+
}
57+
58+
// this overload added to avoid boxing
59+
public Hasher Hash(long obj)
60+
{
61+
_hashCode = 37 * _hashCode + obj.GetHashCode();
62+
return this;
63+
}
64+
65+
// this overload added to avoid boxing
66+
public Hasher Hash<T>(Nullable<T> obj) where T : struct
67+
{
68+
_hashCode = 37 * _hashCode + ((obj == null) ? -1 : obj.Value.GetHashCode());
69+
return this;
70+
}
71+
72+
public Hasher Hash(object obj)
73+
{
74+
_hashCode = 37 * _hashCode + ((obj == null) ? -1 : obj.GetHashCode());
75+
return this;
76+
}
77+
78+
public Hasher HashElements(IEnumerable sequence)
79+
{
80+
if (sequence == null)
81+
{
82+
_hashCode = 37 * _hashCode + -1;
83+
}
84+
else
85+
{
86+
foreach (var value in sequence)
87+
{
88+
_hashCode = 37 * _hashCode + ((value == null) ? -1 : value.GetHashCode());
89+
}
90+
}
91+
return this;
92+
}
93+
94+
public Hasher HashStructElements<T>(IEnumerable<T> sequence) where T : struct
95+
{
96+
foreach (var value in sequence)
97+
{
98+
_hashCode = 37 * _hashCode + value.GetHashCode();
99+
}
100+
return this;
101+
}
102+
}
103+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
5+
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
6+
</PropertyGroup>
7+
8+
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
9+
<PropertyGroup Label="Globals">
10+
<ProjectGuid>b86de81a-6cab-458d-852c-358114c146db</ProjectGuid>
11+
<RootNamespace>MongoDB.Driver.Core</RootNamespace>
12+
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
13+
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
14+
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
15+
</PropertyGroup>
16+
17+
<PropertyGroup>
18+
<SchemaVersion>2.0</SchemaVersion>
19+
</PropertyGroup>
20+
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
21+
</Project>

src/MongoDB.Driver.Core/project.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"version": "2.2.4-*",
3+
"dependencies": {
4+
"MongoDB.Bson": "2.2.4-*",
5+
"NETStandard.Library": "1.5.0-rc2-24027",
6+
"System.Collections.Specialized": "4.0.1-rc2-24027",
7+
"System.Diagnostics.TraceSource": "4.0.0-rc2-24027",
8+
"System.Net.Security": "4.0.0-rc2-24027",
9+
"System.Dynamic.Runtime": "4.0.11-rc2-24027",
10+
"Microsoft.AspNetCore.Identity": "1.0.0-rc2-final",
11+
"System.Security.SecureString": "4.0.0-beta-23409",
12+
"System.Xml.XmlSerializer": "4.0.11-rc2-24027",
13+
"System.Collections.NonGeneric": "4.0.1-rc2-24027",
14+
"System.Reflection.Emit.Lightweight": "4.0.1-rc2-24027",
15+
"System.Reflection.Extensions": "4.0.1-rc2-24027",
16+
"System.Runtime.InteropServices": "4.2.0-beta-24225-02",
17+
"System.Runtime.Handles": "4.0.2-beta-24225-02"
18+
},
19+
"frameworks": {
20+
"netstandard1.5": {
21+
"imports": [ "dnxcore50", "portable-net451+win8" ]
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)