Skip to content

Commit 36d81a9

Browse files
committed
Preparations for targeting .NET Standard 2.0
Inversed all #ifdefs to consider netstandard13 and netcoreapp11 as exceptions since netstandard20 covers almost all of the .NET Framework API surface. In some rare cases it's still necessary to check specifically for net45/net451 (e.g. performance counters, directory services). It's enough to add netstandard20 and netcoreapp20 to Npgsql and to Npgsql.Tests respectively to support .NET Standard 2.0 (this is currently not done because not supported at Appveyor/Travis). Relates to #1575
1 parent 70f9376 commit 36d81a9

41 files changed

Lines changed: 113 additions & 116 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ before_build:
2323
- appveyor-retry nuget restore Npgsql.sln # For VSIX
2424
build_script:
2525
- dotnet build "src\Npgsql" -c ReleaseOptimizedCryptography
26-
- dotnet build "test\Npgsql.Tests" -c Debug
26+
- dotnet build "test\Npgsql.Tests" -c Debug -f net451
2727
- msbuild src\VSIX\VSIX.csproj /p:Configuration=Release
2828
- msbuild src\MSI\MSI.wixproj /p:Configuration=Release
2929
after_build:

src/Npgsql/BackendMessages/ErrorOrNoticeMessage.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@
2323

2424
using System;
2525
using Npgsql.Logging;
26-
#if NET45 || NET451
26+
#if !NETSTANDARD1_3
2727
using System.Runtime.Serialization;
2828
#endif
2929

3030
namespace Npgsql.BackendMessages
3131
{
32-
#if NET45 || NET451
32+
#if !NETSTANDARD1_3
3333
[Serializable]
3434
#endif
3535
class ErrorOrNoticeMessage

src/Npgsql/BackendMessages/RowDescriptionMessage.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,10 @@ public bool Equals([NotNull] string x, [NotNull] string y)
117117
=> CompareInfo.Compare(x, y, CompareOptions.IgnoreWidth) == 0;
118118
public int GetHashCode([NotNull] string o)
119119
{
120-
#if NET45 || NET451
121-
return CompareInfo.GetSortKey(o, CompareOptions.IgnoreWidth).GetHashCode();
122-
#else
120+
#if NETSTANDARD1_3
123121
return CompareInfo.GetHashCode(o, CompareOptions.IgnoreWidth);
122+
#else
123+
return CompareInfo.GetSortKey(o, CompareOptions.IgnoreWidth).GetHashCode();
124124
#endif
125125
}
126126
}
@@ -133,10 +133,10 @@ public bool Equals([NotNull] string x, [NotNull] string y)
133133
=> CompareInfo.Compare(x, y, CompareOptions.IgnoreWidth | CompareOptions.IgnoreCase) == 0;
134134
public int GetHashCode([NotNull] string o)
135135
{
136-
#if NET45 || NET451
137-
return CompareInfo.GetSortKey(o, CompareOptions.IgnoreWidth | CompareOptions.IgnoreCase).GetHashCode();
138-
#else
136+
#if NETSTANDARD1_3
139137
return CompareInfo.GetHashCode(o, CompareOptions.IgnoreWidth | CompareOptions.IgnoreCase);
138+
#else
139+
return CompareInfo.GetSortKey(o, CompareOptions.IgnoreWidth | CompareOptions.IgnoreCase).GetHashCode();
140140
#endif
141141
}
142142
}

src/Npgsql/Logging/NpgsqlLogManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ internal static NpgsqlLogger GetCurrentClassLogger()
7676
// Copied from NLog
7777
static string GetClassFullName()
7878
{
79-
#if NET45 || NET451
79+
#if !NETSTANDARD1_3
8080
string className;
8181
Type declaringType;
8282
int framesToSkip = 2;

src/Npgsql/NpgsqlCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1124,7 +1124,7 @@ void LogCommand()
11241124
Log.Debug(sb.ToString(), Connection.Connector.Id);
11251125
}
11261126

1127-
#if NET45 || NET451
1127+
#if !NETSTANDARD1_3
11281128
/// <summary>
11291129
/// Create a new command based on this one.
11301130
/// </summary>

src/Npgsql/NpgsqlCommandBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
// TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
2222
#endregion
2323

24-
#if NET45 || NET451
24+
#if !NETSTANDARD1_3
2525

2626
using System;
2727
using System.Data;

src/Npgsql/NpgsqlConnection.cs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,14 @@
3737
using JetBrains.Annotations;
3838
using Npgsql.Logging;
3939
using Npgsql.NameTranslation;
40-
#if NET45 || NET451
41-
using System.Transactions;
42-
#endif
4340
using NpgsqlTypes;
4441
using IsolationLevel = System.Data.IsolationLevel;
4542
using ThreadState = System.Threading.ThreadState;
4643

44+
#if !NETSTANDARD1_3
45+
using System.Transactions;
46+
#endif
47+
4748
namespace Npgsql
4849
{
4950
/// <summary>
@@ -90,7 +91,7 @@ public sealed class NpgsqlConnection : DbConnection, ICloneable
9091

9192
bool _wasBroken;
9293

93-
#if NET45 || NET451
94+
#if !NETSTANDARD1_3
9495
[CanBeNull]
9596
internal Transaction EnlistedTransaction { get; set; }
9697
#endif
@@ -128,7 +129,7 @@ public NpgsqlConnection(string connectionString)
128129
GC.SuppressFinalize(this);
129130
ConnectionString = connectionString;
130131

131-
#if NET45 || NET451
132+
#if !NETSTANDARD1_3
132133
// Fix authentication problems. See https://bugzilla.novell.com/show_bug.cgi?id=MONO77559 and
133134
// http://pgfoundry.org/forum/message.php?msg_id=1002377 for more info.
134135
RSACryptoServiceProvider.UseMachineKeyStore = true;
@@ -221,7 +222,7 @@ async Task Open(bool async, CancellationToken cancellationToken)
221222
{
222223
_userFacingConnectionString = _pool.UserFacingConnectionString;
223224

224-
#if NET45 || NET451
225+
#if !NETSTANDARD1_3
225226
if (Settings.Enlist)
226227
{
227228
if (Transaction.Current != null)
@@ -238,7 +239,7 @@ async Task Open(bool async, CancellationToken cancellationToken)
238239
}
239240
else // No enlist
240241
#endif
241-
Connector = await _pool.Allocate(this, timeout, async, cancellationToken);
242+
Connector = await _pool.Allocate(this, timeout, async, cancellationToken);
242243

243244
Counters.SoftConnectsPerSecond.Increment();
244245

@@ -247,7 +248,7 @@ async Task Open(bool async, CancellationToken cancellationToken)
247248
Connector.TypeHandlerRegistry.ActivateGlobalMappings();
248249
}
249250

250-
#if NET45 || NET451
251+
#if !NETSTANDARD1_3
251252
// We may have gotten an already enlisted pending connector above, no need to enlist in that case
252253
if (Settings.Enlist && Transaction.Current != null && EnlistedTransaction == null)
253254
EnlistTransaction(Transaction.Current);
@@ -499,7 +500,7 @@ protected override DbTransaction BeginDbTransaction(IsolationLevel isolationLeve
499500
}
500501
}
501502

502-
#if NET45 || NET451
503+
#if !NETSTANDARD1_3
503504
/// <summary>
504505
/// Enlist transation.
505506
/// </summary>
@@ -553,7 +554,9 @@ internal void Close(bool wasBroken)
553554
Connector.Close();
554555
else
555556
{
556-
#if NET45 || NET451
557+
#if NETSTANDARD1_3
558+
_pool.Release(Connector);
559+
#else
557560
if (EnlistedTransaction == null)
558561
_pool.Release(Connector);
559562
else
@@ -565,8 +568,6 @@ internal void Close(bool wasBroken)
565568
Connector.Connection = null;
566569
EnlistedTransaction = null;
567570
}
568-
#else
569-
_pool.Release(Connector);
570571
#endif
571572
}
572573

@@ -1253,7 +1254,7 @@ internal NpgsqlConnector CheckReadyAndGetConnector()
12531254
#endregion State checks
12541255

12551256
#region Schema operations
1256-
#if NET45 || NET451
1257+
#if !NETSTANDARD1_3
12571258
/// <summary>
12581259
/// Returns the supported collections
12591260
/// </summary>
@@ -1335,7 +1336,7 @@ public override DataTable GetSchema([CanBeNull] string collectionName, [CanBeNul
13351336
/// <summary>
13361337
/// Creates a closed connection with the connection string and authentication details of this message.
13371338
/// </summary>
1338-
#if NET45 || NET451
1339+
#if !NETSTANDARD1_3
13391340
object ICloneable.Clone()
13401341
#else
13411342
public NpgsqlConnection Clone()
@@ -1393,7 +1394,7 @@ public override void ChangeDatabase(string dbName)
13931394
Open();
13941395
}
13951396

1396-
#if NET45 || NET451
1397+
#if !NETSTANDARD1_3
13971398
/// <summary>
13981399
/// DB provider factory.
13991400
/// </summary>

src/Npgsql/NpgsqlConnectionStringBuilder.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public sealed class NpgsqlConnectionStringBuilder : DbConnectionStringBuilder, I
6666
/// </summary>
6767
public NpgsqlConnectionStringBuilder() { Init(); }
6868

69-
#if NET45 || NET451
69+
#if !NETSTANDARD1_3
7070
/// <summary>
7171
/// Initializes a new instance of the NpgsqlConnectionStringBuilder class, optionally using ODBC rules for quoting values.
7272
/// </summary>
@@ -1223,7 +1223,7 @@ public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
12231223
yield return new KeyValuePair<string, object>(k, this[k]);
12241224
}
12251225

1226-
#if !(NET45 || NET451)
1226+
#if NETSTANDARD1_3
12271227
/// <summary>
12281228
/// Gets a value indicating whether the ICollection{T} is read-only.
12291229
/// </summary>
@@ -1233,7 +1233,7 @@ public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
12331233

12341234
#region ICustomTypeDescriptor
12351235

1236-
#if NET45 || NET451
1236+
#if !NETSTANDARD1_3
12371237
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
12381238
protected override void GetProperties(Hashtable propertyDescriptors)
12391239
{

src/Npgsql/NpgsqlConnector.Auth.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,10 @@ async Task AuthenticateGSS(bool async, CancellationToken cancellationToken)
110110
var targetName = $"{KerberosServiceName}/{Host}";
111111
// AuthenticateAsClientAsync doesn't exist in .NET 4.5/4.5.1 (only introduced in 4.6)
112112
// Conversely, no sync in .NET Standard 1.3 :/
113-
#if NET45 || NET451
114-
negotiateStream.AuthenticateAsClient(CredentialCache.DefaultNetworkCredentials, targetName);
115-
#elif NETSTANDARD1_3
113+
#if NETSTANDARD1_3
116114
await negotiateStream.AuthenticateAsClientAsync(CredentialCache.DefaultNetworkCredentials, targetName);
117115
#else
118-
#error Missing platform
116+
negotiateStream.AuthenticateAsClient(CredentialCache.DefaultNetworkCredentials, targetName);
119117
#endif
120118
}
121119
catch (AuthenticationCompleteException)

src/Npgsql/NpgsqlConnector.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ string GetUsername()
472472
return username;
473473
}
474474

475-
#if NET45 || NET451
475+
#if !NETSTANDARD1_3
476476
username = Environment.UserName;
477477
if (!string.IsNullOrEmpty(username))
478478
return username;
@@ -603,13 +603,13 @@ void Connect(NpgsqlTimeout timeout)
603603
}
604604
else
605605
{
606-
#if NET45 || NET451
606+
#if NETSTANDARD1_3
607+
// .NET Standard 1.3 didn't have sync DNS methods
608+
endpoints = Dns.GetHostAddressesAsync(Host).Result.Select(a => new IPEndPoint(a, Port)).ToArray();
609+
#else
607610
// Note that there aren't any timeoutable DNS methods, and we want to use sync-only
608611
// methods (not to rely on any TP threads etc.)
609612
endpoints = Dns.GetHostAddresses(Host).Select(a => new IPEndPoint(a, Port)).ToArray();
610-
#else
611-
// .NET Core doesn't appear to have sync DNS methods (yet?)
612-
endpoints = Dns.GetHostAddressesAsync(Host).Result.Select(a => new IPEndPoint(a, Port)).ToArray();
613613
#endif
614614
timeout.Check();
615615
}

0 commit comments

Comments
 (0)