Skip to content

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronald Wildenberg committed Apr 17, 2018
1 parent dfc612d commit 99c9cb1
Show file tree
Hide file tree
Showing 13 changed files with 358 additions and 0 deletions.
46 changes: 46 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (web)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/AsyncLocal.SimpleInjector.Web/bin/Debug/netcoreapp2.0/AsyncLocal.SimpleInjector.Web.dll",
"args": [],
"cwd": "${workspaceFolder}/AsyncLocal.SimpleInjector.Web",
"stopAtEntry": false,
"internalConsoleOptions": "openOnSessionStart",
"launchBrowser": {
"enabled": true,
"args": "${auto-detect-url}",
"windows": {
"command": "cmd.exe",
"args": "/C start ${auto-detect-url}"
},
"osx": {
"command": "open"
},
"linux": {
"command": "xdg-open"
}
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"sourceFileMap": {
"/Views": "${workspaceFolder}/Views"
}
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
,]
}
15 changes: 15 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/AsyncLocal.SimpleInjector.Web/AsyncLocal.SimpleInjector.Web.csproj"
],
"problemMatcher": "$msCompile"
}
]
}
25 changes: 25 additions & 0 deletions AsyncLocal.SimpleInjector.Web.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27428.2027
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AsyncLocal.SimpleInjector.Web", "AsyncLocal.SimpleInjector.Web\AsyncLocal.SimpleInjector.Web.csproj", "{1E1D53B4-1CF4-483A-B131-9918CB5A2727}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{1E1D53B4-1CF4-483A-B131-9918CB5A2727}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1E1D53B4-1CF4-483A-B131-9918CB5A2727}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1E1D53B4-1CF4-483A-B131-9918CB5A2727}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1E1D53B4-1CF4-483A-B131-9918CB5A2727}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B1B66517-2176-4EAF-AE97-FF522D8867AF}
EndGlobalSection
EndGlobal
21 changes: 21 additions & 0 deletions AsyncLocal.SimpleInjector.Web/AsyncLocal.SimpleInjector.Web.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.6" />
<PackageReference Include="SimpleInjector" Version="4.1.1" />
<PackageReference Include="SimpleInjector.Integration.AspNetCore.Mvc" Version="4.1.1" />
</ItemGroup>

<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.3" />
</ItemGroup>

</Project>
20 changes: 20 additions & 0 deletions AsyncLocal.SimpleInjector.Web/Controllers/CorrelationContainer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Threading;

namespace AsyncLocal.SimpleInjector.Web.Controllers
{
public class CorrelationContainer
{
private readonly AsyncLocal<Guid> _correlationId = new AsyncLocal<Guid>();

public void SetCorrelationId(Guid correlationId)
{
_correlationId.Value = correlationId;
}

public Guid GetCorrelationId()
{
return _correlationId.Value;
}
}
}
35 changes: 35 additions & 0 deletions AsyncLocal.SimpleInjector.Web/Controllers/CustomerController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace AsyncLocal.SimpleInjector.Web.Controllers
{
[Route("api/[controller]")]
public class CustomerController : Controller
{
private readonly ICustomerService _customerService;
private readonly CorrelationContainer _correlationContainer;

public CustomerController(ICustomerService customerService, CorrelationContainer correlationContainer)
{
_customerService = customerService;
_correlationContainer = correlationContainer;
}

[HttpGet]
[Route("tags")]
public async Task<IEnumerable<string>> Get(int customerId)
{
// Set async local correlation id.
var correlationId = Guid.NewGuid();
_correlationContainer.SetCorrelationId(correlationId);

// Call controller dependency (decorated by LoggingDecorator).
var tags = await _customerService.GetCustomerTags(customerId);

// Return values.
return tags;
}
}
}
21 changes: 21 additions & 0 deletions AsyncLocal.SimpleInjector.Web/Controllers/CustomerService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace AsyncLocal.SimpleInjector.Web.Controllers
{
public class CustomerService : ICustomerService
{
private readonly CorrelationContainer _correlationContainer;

public CustomerService(CorrelationContainer correlationContainer)
{
_correlationContainer = correlationContainer;
}

public Task<IEnumerable<string>> GetCustomerTags(int customerId)
{
return Task.FromResult(new[] { "elm", "is", "cool" }.AsEnumerable());
}
}
}
10 changes: 10 additions & 0 deletions AsyncLocal.SimpleInjector.Web/Controllers/ICustomerService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections.Generic;
using System.Threading.Tasks;

namespace AsyncLocal.SimpleInjector.Web.Controllers
{
public interface ICustomerService
{
Task<IEnumerable<string>> GetCustomerTags(int customerId);
}
}
38 changes: 38 additions & 0 deletions AsyncLocal.SimpleInjector.Web/Controllers/LoggingDecorator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;

namespace AsyncLocal.SimpleInjector.Web.Controllers
{
public class LoggingDecorator : ICustomerService
{
private readonly Func<ICustomerService> _decorateeFunc;
private readonly ILogger<CustomerService> _logger;
private readonly CorrelationContainer _correlationContainer;

public LoggingDecorator(
Func<ICustomerService> decorateeFunc,
ILogger<CustomerService> logger,
CorrelationContainer correlationContainer)
{
_decorateeFunc = decorateeFunc;
_logger = logger;
_correlationContainer = correlationContainer;
}

public async Task<IEnumerable<string>> GetCustomerTags(int customerId)
{
// Get async local correlation id.
var correlationId = _correlationContainer.GetCorrelationId();
_logger.LogWarning($"Getting customer tags for {customerId} ({correlationId})");

// Call decoratee.
var decoratee = _decorateeFunc.Invoke();
var values = await decoratee.GetCustomerTags(customerId);

// Return values.
return values;
}
}
}
18 changes: 18 additions & 0 deletions AsyncLocal.SimpleInjector.Web/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;

namespace AsyncLocal.SimpleInjector.Web
{
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}

public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
}
77 changes: 77 additions & 0 deletions AsyncLocal.SimpleInjector.Web/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using AsyncLocal.SimpleInjector.Web.Controllers;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.ViewComponents;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SimpleInjector;
using SimpleInjector.Integration.AspNetCore.Mvc;
using SimpleInjector.Lifestyles;

namespace AsyncLocal.SimpleInjector.Web
{
public class Startup
{
private readonly Container _container = new Container();

public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();

IntegrateSimpleInjector(services);
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
InitializeContainer(app);

if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseMvc();
}

private void IntegrateSimpleInjector(IServiceCollection services)
{
_container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();

services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

services.AddSingleton<IControllerActivator>(
new SimpleInjectorControllerActivator(_container));
services.AddSingleton<IViewComponentActivator>(
new SimpleInjectorViewComponentActivator(_container));

services.EnableSimpleInjectorCrossWiring(_container);
services.UseSimpleInjectorAspNetRequestScoping(_container);
}

private void InitializeContainer(IApplicationBuilder app)
{
// Add application presentation components:
_container.RegisterMvcControllers(app);
_container.RegisterMvcViewComponents(app);

// Register dependency that will be used in controller.
_container.RegisterSingleton<ICustomerService, CustomerService>();
_container.RegisterDecorator<ICustomerService, LoggingDecorator>();
_container.RegisterSingleton<CorrelationContainer>();

// Allow Simple Injector to resolve services from ASP.NET Core.
_container.AutoCrossWireAspNetComponents(app);
}
}
}
17 changes: 17 additions & 0 deletions AsyncLocal.SimpleInjector.Web/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
},
"Console": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
}
15 changes: 15 additions & 0 deletions AsyncLocal.SimpleInjector.Web/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"Logging": {
"IncludeScopes": false,
"Debug": {
"LogLevel": {
"Default": "Warning"
}
},
"Console": {
"LogLevel": {
"Default": "Information"
}
}
}
}

0 comments on commit 99c9cb1

Please sign in to comment.