Skip to content

Commit 3447fc1

Browse files
committed
Add Entra (AAD) authentication support
1 parent ece7e40 commit 3447fc1

30 files changed

+336
-387
lines changed

Samples/Dapper/ElasticDapper.csproj

+4-5
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55
</PropertyGroup>
66
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), Build.props))\Build.props" />
77
<ItemGroup>
8-
<PackageReference Include="EnterpriseLibrary.TransientFaultHandling.Data.NetCore" Version="6.0.1312" />
9-
<PackageReference Include="EnterpriseLibrary.TransientFaultHandling.NetCore" Version="6.0.1312" />
10-
<PackageReference Include="Microsoft.Azure.SqlDatabase.ElasticScale.Client" version="2.3.0" />
11-
<PackageReference Include="System.Configuration.ConfigurationManager" Version="5.0.0" />
12-
<PackageReference Include="Dapper" Version="2.0.90" />
8+
<PackageReference Include="Dapper" Version="2.1.4" />
139
<PackageReference Include="DapperExtensions" Version="1.7.0" />
1410
</ItemGroup>
1511
<ItemGroup>
1612
<None Include="LICENSE" />
1713
</ItemGroup>
14+
<ItemGroup>
15+
<ProjectReference Include="..\..\Src\ElasticScale.Client\Microsoft.Azure.SqlDatabase.ElasticScale.Client.csproj" />
16+
</ItemGroup>
1817
</Project>

Samples/Dapper/Program.cs

+47-61
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6-
using System.Data.SqlClient;
6+
using Microsoft.Data.SqlClient;
77
using Dapper;
88
using DapperExtensions;
99
using Microsoft.Azure.SqlDatabase.ElasticScale.ShardManagement;
@@ -67,89 +67,74 @@ public static void Main()
6767
Console.Write("Enter a name for a new Blog: ");
6868
var name = Console.ReadLine();
6969

70-
SqlDatabaseUtils.SqlRetryPolicy.ExecuteAction(() =>
70+
using (SqlConnection sqlconn = shardingLayer.ShardMap.OpenConnectionForKey(
71+
key: s_tenantId1,
72+
connectionString: connStrBldr.ConnectionString,
73+
options: ConnectionOptions.Validate))
7174
{
72-
using (SqlConnection sqlconn = shardingLayer.ShardMap.OpenConnectionForKey(
73-
key: s_tenantId1,
74-
connectionString: connStrBldr.ConnectionString,
75-
options: ConnectionOptions.Validate))
76-
{
77-
var blog = new Blog { Name = name };
78-
sqlconn.Insert(blog);
79-
}
80-
});
75+
var blog = new Blog { Name = name };
76+
sqlconn.Insert(blog);
77+
}
8178

82-
SqlDatabaseUtils.SqlRetryPolicy.ExecuteAction(() =>
79+
using (SqlConnection sqlconn = shardingLayer.ShardMap.OpenConnectionForKey(
80+
key: s_tenantId1,
81+
connectionString: connStrBldr.ConnectionString,
82+
options: ConnectionOptions.Validate))
8383
{
84-
using (SqlConnection sqlconn = shardingLayer.ShardMap.OpenConnectionForKey(
85-
key: s_tenantId1,
86-
connectionString: connStrBldr.ConnectionString,
87-
options: ConnectionOptions.Validate))
84+
// Display all Blogs for tenant 1
85+
IEnumerable<Blog> result = sqlconn.Query<Blog>(@"
86+
SELECT *
87+
FROM Blog
88+
ORDER BY Name");
89+
90+
Console.WriteLine("All blogs for tenant id {0}:", s_tenantId1);
91+
foreach (var item in result)
8892
{
89-
// Display all Blogs for tenant 1
90-
IEnumerable<Blog> result = sqlconn.Query<Blog>(@"
91-
SELECT *
92-
FROM Blog
93-
ORDER BY Name");
94-
95-
Console.WriteLine("All blogs for tenant id {0}:", s_tenantId1);
96-
foreach (var item in result)
97-
{
98-
Console.WriteLine(item.Name);
99-
}
93+
Console.WriteLine(item.Name);
10094
}
101-
});
95+
}
10296

10397
// Do work for tenant 2 :-)
10498
// Here I am going to illustrate how to integrate
10599
// with DapperExtensions which saves us the T-SQL
106100
//
107-
SqlDatabaseUtils.SqlRetryPolicy.ExecuteAction(() =>
101+
using (SqlConnection sqlconn = shardingLayer.ShardMap.OpenConnectionForKey(
102+
key: s_tenantId2,
103+
connectionString: connStrBldr.ConnectionString,
104+
options: ConnectionOptions.Validate))
108105
{
109-
using (SqlConnection sqlconn = shardingLayer.ShardMap.OpenConnectionForKey(
110-
key: s_tenantId2,
111-
connectionString: connStrBldr.ConnectionString,
112-
options: ConnectionOptions.Validate))
106+
// Display all Blogs for tenant 2
107+
IEnumerable<Blog> result = sqlconn.GetList<Blog>();
108+
Console.WriteLine("All blogs for tenant id {0}:", s_tenantId2);
109+
foreach (var item in result)
113110
{
114-
// Display all Blogs for tenant 2
115-
IEnumerable<Blog> result = sqlconn.GetList<Blog>();
116-
Console.WriteLine("All blogs for tenant id {0}:", s_tenantId2);
117-
foreach (var item in result)
118-
{
119-
Console.WriteLine(item.Name);
120-
}
111+
Console.WriteLine(item.Name);
121112
}
122-
});
113+
}
123114

124115
// Create and save a new Blog
125116
Console.Write("Enter a name for a new Blog: ");
126117
var name2 = Console.ReadLine();
127118

128-
SqlDatabaseUtils.SqlRetryPolicy.ExecuteAction(() =>
119+
using (SqlConnection sqlconn = shardingLayer.ShardMap.OpenConnectionForKey(
120+
key: s_tenantId2,
121+
connectionString: connStrBldr.ConnectionString,
122+
options: ConnectionOptions.Validate))
129123
{
130-
using (SqlConnection sqlconn = shardingLayer.ShardMap.OpenConnectionForKey(
131-
key: s_tenantId2,
132-
connectionString: connStrBldr.ConnectionString,
133-
options: ConnectionOptions.Validate))
134-
{
135-
var blog = new Blog { Name = name2 };
136-
sqlconn.Insert(blog);
137-
}
138-
});
124+
var blog = new Blog { Name = name2 };
125+
sqlconn.Insert(blog);
126+
}
139127

140-
SqlDatabaseUtils.SqlRetryPolicy.ExecuteAction(() =>
128+
using (SqlConnection sqlconn = shardingLayer.ShardMap.OpenConnectionForKey(s_tenantId2, connStrBldr.ConnectionString, ConnectionOptions.Validate))
141129
{
142-
using (SqlConnection sqlconn = shardingLayer.ShardMap.OpenConnectionForKey(s_tenantId2, connStrBldr.ConnectionString, ConnectionOptions.Validate))
130+
// Display all Blogs for tenant 2
131+
IEnumerable<Blog> result = sqlconn.GetList<Blog>();
132+
Console.WriteLine("All blogs for tenant id {0}:", s_tenantId2);
133+
foreach (var item in result)
143134
{
144-
// Display all Blogs for tenant 2
145-
IEnumerable<Blog> result = sqlconn.GetList<Blog>();
146-
Console.WriteLine("All blogs for tenant id {0}:", s_tenantId2);
147-
foreach (var item in result)
148-
{
149-
Console.WriteLine(item.Name);
150-
}
135+
Console.WriteLine(item.Name);
151136
}
152-
});
137+
}
153138

154139
Console.WriteLine("Press any key to exit...");
155140
Console.ReadKey();
@@ -168,6 +153,7 @@ private static void CreateSchema(string shardName)
168153

169154
using (SqlConnection conn = new SqlConnection(connStrBldr.ToString()))
170155
{
156+
conn.RetryLogicProvider = SqlDatabaseUtils.SqlRetryProvider;
171157
conn.Open();
172158
conn.Execute(@"
173159
IF (OBJECT_ID('[dbo].[Blog]', 'U') IS NULL)

Samples/Dapper/Sharding.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Microsoft. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4-
using System.Data.SqlClient;
4+
using Microsoft.Data.SqlClient;
55
using Microsoft.Azure.SqlDatabase.ElasticScale.ShardManagement;
66

77
namespace ElasticDapper

Samples/Dapper/SqlDatabaseUtils.cs

+14-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using System;
5-
using Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling;
5+
using Microsoft.Data.SqlClient;
66

77
namespace ElasticDapper
88
{
@@ -11,12 +11,22 @@ namespace ElasticDapper
1111
/// </summary>
1212
internal static class SqlDatabaseUtils
1313
{
14+
/// <summary>
15+
/// Create a retry logic provider
16+
/// </summary>
17+
public static SqlRetryLogicBaseProvider SqlRetryProvider = SqlConfigurableRetryFactory.CreateExponentialRetryProvider(SqlRetryPolicy);
18+
1419
/// <summary>
1520
/// Gets the retry policy to use for connections to SQL Server.
1621
/// </summary>
17-
public static RetryPolicy SqlRetryPolicy
22+
private static SqlRetryLogicOption SqlRetryPolicy => new()
1823
{
19-
get { return new RetryPolicy<SqlDatabaseTransientErrorDetectionStrategy>(10, TimeSpan.FromSeconds(5)); }
20-
}
24+
// Tries 5 times before throwing an exception
25+
NumberOfTries = 5,
26+
// Preferred gap time to delay before retry
27+
DeltaTime = TimeSpan.FromSeconds(1),
28+
// Maximum gap time for each delay time before retry
29+
MaxTimeInterval = TimeSpan.FromSeconds(20)
30+
};
2131
}
2232
}

Samples/EFCodeFirst/ElasticScaleContext.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
using System.Data.Common;
55
using System.Data.Entity;
6-
using System.Data.SqlClient;
6+
using Microsoft.Data.SqlClient;
77
using Microsoft.Azure.SqlDatabase.ElasticScale.ShardManagement;
88

99
namespace EFCodeFirstElasticScale

Samples/EFCodeFirst/ElasticScaleDbConfiguration.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public ElasticScaleDbConfiguration()
1717
// the SqlAzureExecutionStrategy which would lead to wrong retry behavior
1818
// since it would not use the OpenConnectionForKey call.
1919
// For more details, see http://msdn.microsoft.com/en-us/data/dn456835.aspx.
20-
this.SetExecutionStrategy("System.Data.SqlClient", () => new DefaultExecutionStrategy());
20+
this.SetExecutionStrategy("Microsoft.Data.SqlClient", () => new DefaultExecutionStrategy());
2121

2222
// There are legitimate cases, typically for migrations during development
2323
// using Add-Migration and Update-Datase, where a connection to a

Samples/EFCodeFirst/EntityFrameworkCodeFirst.csproj

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
</PropertyGroup>
66
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), Build.props))\Build.props" />
77
<ItemGroup>
8-
<PackageReference Include="EnterpriseLibrary.TransientFaultHandling.Data.NetCore" Version="6.0.1312" />
9-
<PackageReference Include="EnterpriseLibrary.TransientFaultHandling.NetCore" Version="6.0.1312" />
10-
<PackageReference Include="Microsoft.Azure.SqlDatabase.ElasticScale.Client" Version="2.3.0" />
11-
<PackageReference Include="System.Configuration.ConfigurationManager" Version="5.0.0" />
128
<PackageReference Include="EntityFramework" Version="6.4.4" />
139
</ItemGroup>
1410
<ItemGroup>
@@ -17,4 +13,7 @@
1713
<ItemGroup>
1814
<Content Include="appsettings.json" />
1915
</ItemGroup>
16+
<ItemGroup>
17+
<ProjectReference Include="..\..\Src\ElasticScale.Client\Microsoft.Azure.SqlDatabase.ElasticScale.Client.csproj" />
18+
</ItemGroup>
2019
</Project>

Samples/EFCodeFirst/Program.cs

+41-56
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using System;
5-
using System.Data.SqlClient;
5+
using Microsoft.Data.SqlClient;
66
using System.Linq;
77

88
////////////////////////////////////////////////////////////////////////////////////////
@@ -58,81 +58,66 @@ public static void Main()
5858
Console.Write("Enter a name for a new Blog: ");
5959
var name = Console.ReadLine();
6060

61-
SqlDatabaseUtils.SqlRetryPolicy.ExecuteAction(() =>
61+
using (var db = new ElasticScaleContext<int>(sharding.ShardMap, s_tenantId1, connStrBldr.ConnectionString))
6262
{
63-
using (var db = new ElasticScaleContext<int>(sharding.ShardMap, s_tenantId1, connStrBldr.ConnectionString))
64-
{
65-
var blog = new Blog { Name = name };
66-
db.Blogs.Add(blog);
67-
db.SaveChanges();
68-
}
69-
});
63+
var blog = new Blog { Name = name };
64+
db.Blogs.Add(blog);
65+
db.SaveChanges();
66+
}
7067

71-
SqlDatabaseUtils.SqlRetryPolicy.ExecuteAction(() =>
68+
using (var db = new ElasticScaleContext<int>(sharding.ShardMap, s_tenantId1, connStrBldr.ConnectionString))
7269
{
73-
using (var db = new ElasticScaleContext<int>(sharding.ShardMap, s_tenantId1, connStrBldr.ConnectionString))
70+
// Display all Blogs for tenant 1
71+
var query = from b in db.Blogs
72+
orderby b.Name
73+
select b;
74+
75+
Console.WriteLine("All blogs for tenant id {0}:", s_tenantId1);
76+
foreach (var item in query)
7477
{
75-
// Display all Blogs for tenant 1
76-
var query = from b in db.Blogs
77-
orderby b.Name
78-
select b;
79-
80-
Console.WriteLine("All blogs for tenant id {0}:", s_tenantId1);
81-
foreach (var item in query)
82-
{
83-
Console.WriteLine(item.Name);
84-
}
78+
Console.WriteLine(item.Name);
8579
}
86-
});
80+
}
8781

8882
// Do work for tenant 2 :-)
89-
SqlDatabaseUtils.SqlRetryPolicy.ExecuteAction(() =>
83+
using (var db = new ElasticScaleContext<int>(sharding.ShardMap, s_tenantId2, connStrBldr.ConnectionString))
9084
{
91-
using (var db = new ElasticScaleContext<int>(sharding.ShardMap, s_tenantId2, connStrBldr.ConnectionString))
85+
// Display all Blogs from the database
86+
var query = from b in db.Blogs
87+
orderby b.Name
88+
select b;
89+
90+
Console.WriteLine("All blogs for tenant id {0}:", s_tenantId2);
91+
foreach (var item in query)
9292
{
93-
// Display all Blogs from the database
94-
var query = from b in db.Blogs
95-
orderby b.Name
96-
select b;
97-
98-
Console.WriteLine("All blogs for tenant id {0}:", s_tenantId2);
99-
foreach (var item in query)
100-
{
101-
Console.WriteLine(item.Name);
102-
}
93+
Console.WriteLine(item.Name);
10394
}
104-
});
95+
}
10596

10697
// Create and save a new Blog
10798
Console.Write("Enter a name for a new Blog: ");
10899
var name2 = Console.ReadLine();
109100

110-
SqlDatabaseUtils.SqlRetryPolicy.ExecuteAction(() =>
101+
using (var db = new ElasticScaleContext<int>(sharding.ShardMap, s_tenantId2, connStrBldr.ConnectionString))
111102
{
112-
using (var db = new ElasticScaleContext<int>(sharding.ShardMap, s_tenantId2, connStrBldr.ConnectionString))
113-
{
114-
var blog = new Blog { Name = name2 };
115-
db.Blogs.Add(blog);
116-
db.SaveChanges();
117-
}
118-
});
103+
var blog = new Blog { Name = name2 };
104+
db.Blogs.Add(blog);
105+
db.SaveChanges();
106+
}
119107

120-
SqlDatabaseUtils.SqlRetryPolicy.ExecuteAction(() =>
108+
using (var db = new ElasticScaleContext<int>(sharding.ShardMap, s_tenantId2, connStrBldr.ConnectionString))
121109
{
122-
using (var db = new ElasticScaleContext<int>(sharding.ShardMap, s_tenantId2, connStrBldr.ConnectionString))
110+
// Display all Blogs from the database
111+
var query = from b in db.Blogs
112+
orderby b.Name
113+
select b;
114+
115+
Console.WriteLine("All blogs for tenant id {0}:", s_tenantId2);
116+
foreach (var item in query)
123117
{
124-
// Display all Blogs from the database
125-
var query = from b in db.Blogs
126-
orderby b.Name
127-
select b;
128-
129-
Console.WriteLine("All blogs for tenant id {0}:", s_tenantId2);
130-
foreach (var item in query)
131-
{
132-
Console.WriteLine(item.Name);
133-
}
118+
Console.WriteLine(item.Name);
134119
}
135-
});
120+
}
136121

137122
Console.WriteLine("Press any key to exit...");
138123
Console.ReadKey();

Samples/EFCodeFirst/Sharding.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Microsoft. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4-
using System.Data.SqlClient;
4+
using Microsoft.Data.SqlClient;
55
using System.Linq;
66
using Microsoft.Azure.SqlDatabase.ElasticScale.ShardManagement;
77

0 commit comments

Comments
 (0)