This repository has been archived by the owner on Mar 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Code around `CloudTable` is a bit dirty, I might clean it later if I feel like it.
- Loading branch information
1 parent
75f4eec
commit b45dbc7
Showing
13 changed files
with
332 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions
22
samples/SampleConsole/Configuration/ConfigurationRootExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System; | ||
using Microsoft.Extensions.Configuration; | ||
using Serilog.Events; | ||
|
||
namespace TableStorage.UnsupportedTypes.SampleConsole.Configuration | ||
{ | ||
public static class ConfigurationRootExtensions | ||
{ | ||
public static LogEventLevel GetLoggingLevel(this IConfigurationRoot configuration, string keyName, | ||
LogEventLevel defaultLevel = LogEventLevel.Warning) | ||
{ | ||
try | ||
{ | ||
return configuration.GetValue($"Logging:LogLevel:{keyName}", LogEventLevel.Warning); | ||
} | ||
catch (Exception) | ||
{ | ||
return defaultLevel; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Logging; | ||
using Serilog; | ||
|
||
namespace TableStorage.UnsupportedTypes.SampleConsole.Configuration | ||
{ | ||
public static class LoggerConfigurator | ||
{ | ||
public static ILoggerFactory ConfigureSerilog(this IConfigurationRoot configuration) | ||
{ | ||
var serilogLevel = configuration.GetLoggingLevel("Serilog"); | ||
|
||
var loggerConfiguration = new LoggerConfiguration() | ||
.MinimumLevel.Is(serilogLevel) | ||
.Enrich.WithDemystifiedStackTraces() | ||
.WriteTo.Console(serilogLevel); | ||
|
||
var logger = loggerConfiguration.CreateLogger(); | ||
|
||
ILoggerFactory loggerFactory = new LoggerFactory(); | ||
loggerFactory.AddSerilog(logger); | ||
|
||
return loggerFactory; | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
samples/SampleConsole/Configuration/ServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.WindowsAzure.Storage; | ||
using TableStorage.UnsupportedTypes.SampleConsole.Storage; | ||
|
||
namespace TableStorage.UnsupportedTypes.SampleConsole.Configuration | ||
{ | ||
public static class ServiceCollectionExtensions | ||
{ | ||
public static void AddLogic(this IServiceCollection services) | ||
{ | ||
services.AddSingleton<Presenter>(); | ||
} | ||
|
||
public static async Task AddStorageAsync(this IServiceCollection services, IConfigurationRoot configuration) | ||
{ | ||
var tableStorageConnectionString = configuration.GetValue<string>("AzureTableStorage:ConnectionString"); | ||
|
||
var storageAccount = CloudStorageAccount.Parse(tableStorageConnectionString); | ||
var tableClient = storageAccount.CreateCloudTableClient(); | ||
var table = tableClient.GetTableReference(nameof(UnsupportedTypesTestTableEntity)); | ||
|
||
await table.CreateIfNotExistsAsync(); | ||
|
||
services.AddSingleton(table); | ||
} | ||
|
||
public static void AddLogging(this IServiceCollection services, ILoggerFactory loggerFactory) | ||
{ | ||
services.AddSingleton(loggerFactory); | ||
services.AddLogging(); | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
samples/SampleConsole/Configuration/ServiceProviderConfigurator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace TableStorage.UnsupportedTypes.SampleConsole.Configuration | ||
{ | ||
public class ServiceProviderConfigurator | ||
{ | ||
public async Task<IServiceProvider> ConfigureTheWorldAsync() | ||
{ | ||
IServiceCollection services = new ServiceCollection(); | ||
|
||
var configurationBuilder = new ConfigurationBuilder() | ||
.AddJsonFile("appsettings.json") | ||
.AddEnvironmentVariables(); | ||
|
||
var configuration = configurationBuilder.Build(); | ||
|
||
var loggerFactory = configuration.ConfigureSerilog(); | ||
|
||
services.AddLogging(loggerFactory); | ||
services.AddLogic(); | ||
await services.AddStorageAsync(configuration); | ||
|
||
return services.BuildServiceProvider(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.WindowsAzure.Storage.Table; | ||
using TableStorage.UnsupportedTypes.SampleConsole.Storage; | ||
|
||
namespace TableStorage.UnsupportedTypes.SampleConsole | ||
{ | ||
public class Presenter | ||
{ | ||
private readonly CloudTable _table; | ||
private readonly ILogger<Presenter> _logger; | ||
|
||
public Presenter(CloudTable table, ILogger<Presenter> logger) | ||
{ | ||
_table = table; | ||
_logger = logger; | ||
} | ||
|
||
public async Task RunTheShowAsync() | ||
{ | ||
var entity = new UnsupportedTypesTestTableEntity | ||
{ | ||
PartitionKey = "q", | ||
RowKey = "w", | ||
VeryImportant = new Unimportant | ||
{ | ||
FirstName = "Some First Name", | ||
LastName = "Some Last Name" | ||
} | ||
}; | ||
|
||
_logger.LogInformation("Inserting {@InsertEntity}", entity); | ||
|
||
var insertOperation = TableOperation.InsertOrReplace(entity); | ||
var insertResult = await _table.ExecuteAsync(insertOperation); | ||
|
||
_logger.LogInformation("Insert result was {InsertStatusCode}", insertResult.HttpStatusCode); | ||
|
||
var retrieveOperation = TableOperation | ||
.Retrieve<UnsupportedTypesTestTableEntity>(entity.PartitionKey, entity.RowKey); | ||
|
||
var retrieveResult = await _table.ExecuteAsync(retrieveOperation); | ||
|
||
_logger.LogInformation("Retrieve result was {RetrieveStatusCode}", retrieveResult.HttpStatusCode); | ||
|
||
var retrievedEntity = retrieveResult.Result as UnsupportedTypesTestTableEntity; | ||
|
||
_logger.LogInformation("Retrieved {@RetrieveEntity}", retrievedEntity); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using TableStorage.UnsupportedTypes.SampleConsole.Configuration; | ||
|
||
namespace TableStorage.UnsupportedTypes.SampleConsole | ||
{ | ||
class Program | ||
{ | ||
static async Task Main(string[] args) | ||
{ | ||
try | ||
{ | ||
var providerConfigurator = new ServiceProviderConfigurator(); | ||
var serviceProvider = await providerConfigurator.ConfigureTheWorldAsync(); | ||
|
||
using (var applicationScope = serviceProvider.CreateScope()) | ||
{ | ||
var presenter = applicationScope | ||
.ServiceProvider | ||
.GetRequiredService<Presenter>(); | ||
|
||
await presenter.RunTheShowAsync(); | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
DisplayException(e); | ||
} | ||
finally | ||
{ | ||
Console.WriteLine("Press Enter to continue..."); | ||
Console.ReadLine(); | ||
} | ||
} | ||
|
||
private static void DisplayException(Exception e) | ||
{ | ||
if (e == null) return; | ||
|
||
if (_innerExceptionCount > 0) | ||
{ | ||
Console.WriteLine("\tInner exception {0}:", _innerExceptionCount); | ||
Console.WriteLine(); | ||
} | ||
|
||
Console.WriteLine("Exception: {0}", e.GetType()); | ||
Console.WriteLine("Message: {0}", e.Message); | ||
Console.WriteLine("StackTrace:"); | ||
Console.WriteLine(e.Demystify().StackTrace); | ||
Console.WriteLine(); | ||
|
||
_innerExceptionCount++; | ||
|
||
DisplayException(e.InnerException); | ||
} | ||
|
||
private static int _innerExceptionCount = 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp2.0</TargetFramework> | ||
<AssemblyName>TableStorage.UnsupportedTypes.SampleConsole</AssemblyName> | ||
<RootNamespace>TableStorage.UnsupportedTypes.SampleConsole</RootNamespace> | ||
<LangVersion>7.1</LangVersion> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Content Include="appsettings.json"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</Content> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\TableStorage.UnsupportedTypes\TableStorage.UnsupportedTypes.csproj" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Options" Version="2.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.0.0" /> | ||
<PackageReference Include="Serilog" Version="2.6.0" /> | ||
<PackageReference Include="Serilog.Enrichers.Demystify" Version="0.1.0-dev-00011" /> | ||
<PackageReference Include="Serilog.Extensions.Logging" Version="2.0.2" /> | ||
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" /> | ||
<PackageReference Include="WindowsAzure.Storage" Version="8.7.0" /> | ||
</ItemGroup> | ||
</Project> |
14 changes: 14 additions & 0 deletions
14
samples/SampleConsole/Storage/UnsupportedTypesTestTableEntity.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace TableStorage.UnsupportedTypes.SampleConsole.Storage | ||
{ | ||
public class UnsupportedTypesTestTableEntity : UnsupportedTypesTableEntity | ||
{ | ||
[UnsupportedType] | ||
public Unimportant VeryImportant { get; set; } | ||
} | ||
|
||
public class Unimportant | ||
{ | ||
public string FirstName { get; set; } | ||
public string LastName { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"AzureTableStorage": { | ||
"ConnectionString": "UseDevelopmentStorage=true;" | ||
}, | ||
"Logging": { | ||
"IncludeScopes": false, | ||
"LogLevel": { | ||
"Default": "Information", | ||
"System": "Information", | ||
"Microsoft": "Information", | ||
"Serilog": "Debug" | ||
} | ||
} | ||
} |