Skip to content

Commit

Permalink
Add Entra (AAD) authentication support
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartpa committed Oct 12, 2023
1 parent ece7e40 commit 3447fc1
Show file tree
Hide file tree
Showing 30 changed files with 336 additions and 387 deletions.
9 changes: 4 additions & 5 deletions Samples/Dapper/ElasticDapper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
</PropertyGroup>
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), Build.props))\Build.props" />
<ItemGroup>
<PackageReference Include="EnterpriseLibrary.TransientFaultHandling.Data.NetCore" Version="6.0.1312" />
<PackageReference Include="EnterpriseLibrary.TransientFaultHandling.NetCore" Version="6.0.1312" />
<PackageReference Include="Microsoft.Azure.SqlDatabase.ElasticScale.Client" version="2.3.0" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="5.0.0" />
<PackageReference Include="Dapper" Version="2.0.90" />
<PackageReference Include="Dapper" Version="2.1.4" />
<PackageReference Include="DapperExtensions" Version="1.7.0" />
</ItemGroup>
<ItemGroup>
<None Include="LICENSE" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Src\ElasticScale.Client\Microsoft.Azure.SqlDatabase.ElasticScale.Client.csproj" />
</ItemGroup>
</Project>
108 changes: 47 additions & 61 deletions Samples/Dapper/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using Microsoft.Data.SqlClient;
using Dapper;
using DapperExtensions;
using Microsoft.Azure.SqlDatabase.ElasticScale.ShardManagement;
Expand Down Expand Up @@ -67,89 +67,74 @@ public static void Main()
Console.Write("Enter a name for a new Blog: ");
var name = Console.ReadLine();

SqlDatabaseUtils.SqlRetryPolicy.ExecuteAction(() =>
using (SqlConnection sqlconn = shardingLayer.ShardMap.OpenConnectionForKey(
key: s_tenantId1,
connectionString: connStrBldr.ConnectionString,
options: ConnectionOptions.Validate))
{
using (SqlConnection sqlconn = shardingLayer.ShardMap.OpenConnectionForKey(
key: s_tenantId1,
connectionString: connStrBldr.ConnectionString,
options: ConnectionOptions.Validate))
{
var blog = new Blog { Name = name };
sqlconn.Insert(blog);
}
});
var blog = new Blog { Name = name };
sqlconn.Insert(blog);
}

SqlDatabaseUtils.SqlRetryPolicy.ExecuteAction(() =>
using (SqlConnection sqlconn = shardingLayer.ShardMap.OpenConnectionForKey(
key: s_tenantId1,
connectionString: connStrBldr.ConnectionString,
options: ConnectionOptions.Validate))
{
using (SqlConnection sqlconn = shardingLayer.ShardMap.OpenConnectionForKey(
key: s_tenantId1,
connectionString: connStrBldr.ConnectionString,
options: ConnectionOptions.Validate))
// Display all Blogs for tenant 1
IEnumerable<Blog> result = sqlconn.Query<Blog>(@"
SELECT *
FROM Blog
ORDER BY Name");

Console.WriteLine("All blogs for tenant id {0}:", s_tenantId1);
foreach (var item in result)
{
// Display all Blogs for tenant 1
IEnumerable<Blog> result = sqlconn.Query<Blog>(@"
SELECT *
FROM Blog
ORDER BY Name");

Console.WriteLine("All blogs for tenant id {0}:", s_tenantId1);
foreach (var item in result)
{
Console.WriteLine(item.Name);
}
Console.WriteLine(item.Name);
}
});
}

// Do work for tenant 2 :-)
// Here I am going to illustrate how to integrate
// with DapperExtensions which saves us the T-SQL
//
SqlDatabaseUtils.SqlRetryPolicy.ExecuteAction(() =>
using (SqlConnection sqlconn = shardingLayer.ShardMap.OpenConnectionForKey(
key: s_tenantId2,
connectionString: connStrBldr.ConnectionString,
options: ConnectionOptions.Validate))
{
using (SqlConnection sqlconn = shardingLayer.ShardMap.OpenConnectionForKey(
key: s_tenantId2,
connectionString: connStrBldr.ConnectionString,
options: ConnectionOptions.Validate))
// Display all Blogs for tenant 2
IEnumerable<Blog> result = sqlconn.GetList<Blog>();
Console.WriteLine("All blogs for tenant id {0}:", s_tenantId2);
foreach (var item in result)
{
// Display all Blogs for tenant 2
IEnumerable<Blog> result = sqlconn.GetList<Blog>();
Console.WriteLine("All blogs for tenant id {0}:", s_tenantId2);
foreach (var item in result)
{
Console.WriteLine(item.Name);
}
Console.WriteLine(item.Name);
}
});
}

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

SqlDatabaseUtils.SqlRetryPolicy.ExecuteAction(() =>
using (SqlConnection sqlconn = shardingLayer.ShardMap.OpenConnectionForKey(
key: s_tenantId2,
connectionString: connStrBldr.ConnectionString,
options: ConnectionOptions.Validate))
{
using (SqlConnection sqlconn = shardingLayer.ShardMap.OpenConnectionForKey(
key: s_tenantId2,
connectionString: connStrBldr.ConnectionString,
options: ConnectionOptions.Validate))
{
var blog = new Blog { Name = name2 };
sqlconn.Insert(blog);
}
});
var blog = new Blog { Name = name2 };
sqlconn.Insert(blog);
}

SqlDatabaseUtils.SqlRetryPolicy.ExecuteAction(() =>
using (SqlConnection sqlconn = shardingLayer.ShardMap.OpenConnectionForKey(s_tenantId2, connStrBldr.ConnectionString, ConnectionOptions.Validate))
{
using (SqlConnection sqlconn = shardingLayer.ShardMap.OpenConnectionForKey(s_tenantId2, connStrBldr.ConnectionString, ConnectionOptions.Validate))
// Display all Blogs for tenant 2
IEnumerable<Blog> result = sqlconn.GetList<Blog>();
Console.WriteLine("All blogs for tenant id {0}:", s_tenantId2);
foreach (var item in result)
{
// Display all Blogs for tenant 2
IEnumerable<Blog> result = sqlconn.GetList<Blog>();
Console.WriteLine("All blogs for tenant id {0}:", s_tenantId2);
foreach (var item in result)
{
Console.WriteLine(item.Name);
}
Console.WriteLine(item.Name);
}
});
}

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

using (SqlConnection conn = new SqlConnection(connStrBldr.ToString()))
{
conn.RetryLogicProvider = SqlDatabaseUtils.SqlRetryProvider;
conn.Open();
conn.Execute(@"
IF (OBJECT_ID('[dbo].[Blog]', 'U') IS NULL)
Expand Down
2 changes: 1 addition & 1 deletion Samples/Dapper/Sharding.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Data.SqlClient;
using Microsoft.Data.SqlClient;
using Microsoft.Azure.SqlDatabase.ElasticScale.ShardManagement;

namespace ElasticDapper
Expand Down
18 changes: 14 additions & 4 deletions Samples/Dapper/SqlDatabaseUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling;
using Microsoft.Data.SqlClient;

namespace ElasticDapper
{
Expand All @@ -11,12 +11,22 @@ namespace ElasticDapper
/// </summary>
internal static class SqlDatabaseUtils
{
/// <summary>
/// Create a retry logic provider
/// </summary>
public static SqlRetryLogicBaseProvider SqlRetryProvider = SqlConfigurableRetryFactory.CreateExponentialRetryProvider(SqlRetryPolicy);

/// <summary>
/// Gets the retry policy to use for connections to SQL Server.
/// </summary>
public static RetryPolicy SqlRetryPolicy
private static SqlRetryLogicOption SqlRetryPolicy => new()
{
get { return new RetryPolicy<SqlDatabaseTransientErrorDetectionStrategy>(10, TimeSpan.FromSeconds(5)); }
}
// Tries 5 times before throwing an exception
NumberOfTries = 5,
// Preferred gap time to delay before retry
DeltaTime = TimeSpan.FromSeconds(1),
// Maximum gap time for each delay time before retry
MaxTimeInterval = TimeSpan.FromSeconds(20)
};
}
}
2 changes: 1 addition & 1 deletion Samples/EFCodeFirst/ElasticScaleContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using System.Data.Common;
using System.Data.Entity;
using System.Data.SqlClient;
using Microsoft.Data.SqlClient;
using Microsoft.Azure.SqlDatabase.ElasticScale.ShardManagement;

namespace EFCodeFirstElasticScale
Expand Down
2 changes: 1 addition & 1 deletion Samples/EFCodeFirst/ElasticScaleDbConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public ElasticScaleDbConfiguration()
// the SqlAzureExecutionStrategy which would lead to wrong retry behavior
// since it would not use the OpenConnectionForKey call.
// For more details, see http://msdn.microsoft.com/en-us/data/dn456835.aspx.
this.SetExecutionStrategy("System.Data.SqlClient", () => new DefaultExecutionStrategy());
this.SetExecutionStrategy("Microsoft.Data.SqlClient", () => new DefaultExecutionStrategy());

// There are legitimate cases, typically for migrations during development
// using Add-Migration and Update-Datase, where a connection to a
Expand Down
7 changes: 3 additions & 4 deletions Samples/EFCodeFirst/EntityFrameworkCodeFirst.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
</PropertyGroup>
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), Build.props))\Build.props" />
<ItemGroup>
<PackageReference Include="EnterpriseLibrary.TransientFaultHandling.Data.NetCore" Version="6.0.1312" />
<PackageReference Include="EnterpriseLibrary.TransientFaultHandling.NetCore" Version="6.0.1312" />
<PackageReference Include="Microsoft.Azure.SqlDatabase.ElasticScale.Client" Version="2.3.0" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="5.0.0" />
<PackageReference Include="EntityFramework" Version="6.4.4" />
</ItemGroup>
<ItemGroup>
Expand All @@ -17,4 +13,7 @@
<ItemGroup>
<Content Include="appsettings.json" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Src\ElasticScale.Client\Microsoft.Azure.SqlDatabase.ElasticScale.Client.csproj" />
</ItemGroup>
</Project>
97 changes: 41 additions & 56 deletions Samples/EFCodeFirst/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Data.SqlClient;
using Microsoft.Data.SqlClient;
using System.Linq;

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

SqlDatabaseUtils.SqlRetryPolicy.ExecuteAction(() =>
using (var db = new ElasticScaleContext<int>(sharding.ShardMap, s_tenantId1, connStrBldr.ConnectionString))
{
using (var db = new ElasticScaleContext<int>(sharding.ShardMap, s_tenantId1, connStrBldr.ConnectionString))
{
var blog = new Blog { Name = name };
db.Blogs.Add(blog);
db.SaveChanges();
}
});
var blog = new Blog { Name = name };
db.Blogs.Add(blog);
db.SaveChanges();
}

SqlDatabaseUtils.SqlRetryPolicy.ExecuteAction(() =>
using (var db = new ElasticScaleContext<int>(sharding.ShardMap, s_tenantId1, connStrBldr.ConnectionString))
{
using (var db = new ElasticScaleContext<int>(sharding.ShardMap, s_tenantId1, connStrBldr.ConnectionString))
// Display all Blogs for tenant 1
var query = from b in db.Blogs
orderby b.Name
select b;

Console.WriteLine("All blogs for tenant id {0}:", s_tenantId1);
foreach (var item in query)
{
// Display all Blogs for tenant 1
var query = from b in db.Blogs
orderby b.Name
select b;

Console.WriteLine("All blogs for tenant id {0}:", s_tenantId1);
foreach (var item in query)
{
Console.WriteLine(item.Name);
}
Console.WriteLine(item.Name);
}
});
}

// Do work for tenant 2 :-)
SqlDatabaseUtils.SqlRetryPolicy.ExecuteAction(() =>
using (var db = new ElasticScaleContext<int>(sharding.ShardMap, s_tenantId2, connStrBldr.ConnectionString))
{
using (var db = new ElasticScaleContext<int>(sharding.ShardMap, s_tenantId2, connStrBldr.ConnectionString))
// Display all Blogs from the database
var query = from b in db.Blogs
orderby b.Name
select b;

Console.WriteLine("All blogs for tenant id {0}:", s_tenantId2);
foreach (var item in query)
{
// Display all Blogs from the database
var query = from b in db.Blogs
orderby b.Name
select b;

Console.WriteLine("All blogs for tenant id {0}:", s_tenantId2);
foreach (var item in query)
{
Console.WriteLine(item.Name);
}
Console.WriteLine(item.Name);
}
});
}

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

SqlDatabaseUtils.SqlRetryPolicy.ExecuteAction(() =>
using (var db = new ElasticScaleContext<int>(sharding.ShardMap, s_tenantId2, connStrBldr.ConnectionString))
{
using (var db = new ElasticScaleContext<int>(sharding.ShardMap, s_tenantId2, connStrBldr.ConnectionString))
{
var blog = new Blog { Name = name2 };
db.Blogs.Add(blog);
db.SaveChanges();
}
});
var blog = new Blog { Name = name2 };
db.Blogs.Add(blog);
db.SaveChanges();
}

SqlDatabaseUtils.SqlRetryPolicy.ExecuteAction(() =>
using (var db = new ElasticScaleContext<int>(sharding.ShardMap, s_tenantId2, connStrBldr.ConnectionString))
{
using (var db = new ElasticScaleContext<int>(sharding.ShardMap, s_tenantId2, connStrBldr.ConnectionString))
// Display all Blogs from the database
var query = from b in db.Blogs
orderby b.Name
select b;

Console.WriteLine("All blogs for tenant id {0}:", s_tenantId2);
foreach (var item in query)
{
// Display all Blogs from the database
var query = from b in db.Blogs
orderby b.Name
select b;

Console.WriteLine("All blogs for tenant id {0}:", s_tenantId2);
foreach (var item in query)
{
Console.WriteLine(item.Name);
}
Console.WriteLine(item.Name);
}
});
}

Console.WriteLine("Press any key to exit...");
Console.ReadKey();
Expand Down
2 changes: 1 addition & 1 deletion Samples/EFCodeFirst/Sharding.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Data.SqlClient;
using Microsoft.Data.SqlClient;
using System.Linq;
using Microsoft.Azure.SqlDatabase.ElasticScale.ShardManagement;

Expand Down
Loading

0 comments on commit 3447fc1

Please sign in to comment.