Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add Entra (AAD) authentication support #237

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions Samples/Dapper/ElasticDapper.csproj
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
<OutputType>Exe</OutputType>
</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.21" />
<PackageReference Include="DapperExtensions" Version="1.7.0" />
<PackageReference Include="Oracle.ManagedDataAccess.Core" Version="3.21.120" />
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" />
</ItemGroup>
<ItemGroup>
<None Include="LICENSE" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Src\ElasticScale.Client\Microsoft.Azure.SqlDatabase.ElasticScale.Client.csproj" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion Samples/Dapper/LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2015 Microsoft
Copyright (c) 2015-2023 Microsoft

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
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
4 changes: 4 additions & 0 deletions Samples/Dapper/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

For documentation see:

https://learn.microsoft.com/azure/azure-sql/database/elastic-scale-working-with-dapper
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
12 changes: 6 additions & 6 deletions Samples/EFCodeFirst/EntityFrameworkCodeFirst.csproj
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
<OutputType>Exe</OutputType>
</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" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.5" />

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see System.Data.SqlClient referenced here, but throughout the code I see Microsoft.Data.SqlClient. Is that correct?

Furthermore, I see Release 2.4.0 still is referencing Microsoft.Data.SqlClient 3.0.0 which is not the latest package which is 5.1.2. I believe the higher version package for Microsoft.Data.SqlClient will include a higher version of Azure.Identity.

https://www.nuget.org/packages/Microsoft.Data.SqlClient

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tps-pcaldwell are you looking to use EF (Classic). The EF (Classic) sample hasn't been updated, because there has been no demand for it to be updated. Are you looking to use EF (Classic), or are you looking to use EF Core?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I'm looking to use EF Core. In addition, the sample code is using the nuget reference, so it is still reference 2.3.0 instead of a project reference. I would recommend updating the nuget reference to 2.4.0 or removing the EF sample code which would be a shame.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I don't think we can update the EF Classic sample to 2.4 (which is .Net Core). We'll need to add another sample alongside the EF Classic sample for EF Core, which would use 2.4.

Are you interested in an EF Core sample using 2.4?

</ItemGroup>
<ItemGroup>
<None Include="LICENSE" />
</ItemGroup>
<ItemGroup>
<Content Include="appsettings.json" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Src\ElasticScale.Client\Microsoft.Azure.SqlDatabase.ElasticScale.Client.csproj" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion Samples/EFCodeFirst/LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2015 Microsoft
Copyright (c) 2015-2023 Microsoft

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
Loading