Skip to content

Instantly share code, notes, and snippets.

@joncloud
Created March 23, 2018 21:34
Show Gist options
  • Select an option

  • Save joncloud/ce2b759ffe7aad0f16466725ddd09713 to your computer and use it in GitHub Desktop.

Select an option

Save joncloud/ce2b759ffe7aad0f16466725ddd09713 to your computer and use it in GitHub Desktop.
ASP.NET MVC Service Disposal
Using launch settings from C:\Users\jberube\source\repos\WebApplication2\WebApplication2\Properties\launchSettings.json...
Hosting environment: Development
Content root path: C:\Users\jberube\source\repos\WebApplication2\WebApplication2
Now listening on: http://localhost:22449
Application started. Press Ctrl+C to shut down.
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
Request starting HTTP/1.1 GET http://localhost:22449/singleton
info: WebApplication2.Program.Test[0]
new Singleton()
info: WebApplication2.Program.Test[0]
Singleton.GetId() = e215122b-ff3d-4601-bafb-ac53a79b832c
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
Request finished in 14.3441ms 200
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
Request starting HTTP/1.1 GET http://localhost:22449/singleton
info: WebApplication2.Program.Test[0]
Singleton.GetId() = 733cbac6-7bb3-4d0a-863e-bf42d51b5d86
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
Request finished in 0.2627ms 200
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
Request starting HTTP/1.1 GET http://localhost:22449/scoped
info: WebApplication2.Program.Test[0]
new Scoped()
info: WebApplication2.Program.Test[0]
Scoped.GetId() = bc4906d9-82db-4cc2-a1ae-d245c610c1f9
info: WebApplication2.Program.Test[0]
Scoped.Dispose()
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
Request finished in 1.5986ms 200
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
Request starting HTTP/1.1 GET http://localhost:22449/scoped
info: WebApplication2.Program.Test[0]
new Scoped()
info: WebApplication2.Program.Test[0]
Scoped.GetId() = 0b68d19e-db83-4efc-b963-a8b9e6bc666f
info: WebApplication2.Program.Test[0]
Scoped.Dispose()
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
Request finished in 0.1761ms 200
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
Request starting HTTP/1.1 GET http://localhost:22449/scoped
info: WebApplication2.Program.Test[0]
new Scoped()
info: WebApplication2.Program.Test[0]
Scoped.GetId() = 4bf85c93-3926-4d02-a644-219186e66113
info: WebApplication2.Program.Test[0]
Scoped.Dispose()
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
Request finished in 0.1576ms 200
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
Request starting HTTP/1.1 GET http://localhost:22449/transient/4
info: WebApplication2.Program.Test[0]
new Transient()
info: WebApplication2.Program.Test[0]
Transient.GetId() = 02d9f6bf-0aa8-4aaa-b158-5575f477c532
info: WebApplication2.Program.Test[0]
new Transient()
info: WebApplication2.Program.Test[0]
Transient.GetId() = c6a1fd4c-b471-416f-af64-e2810818033f
info: WebApplication2.Program.Test[0]
new Transient()
info: WebApplication2.Program.Test[0]
Transient.GetId() = 118d0262-8bd7-4e3b-82c6-43763d1920c4
info: WebApplication2.Program.Test[0]
new Transient()
info: WebApplication2.Program.Test[0]
Transient.GetId() = ca8f67df-724e-40c4-88ac-48a92d1bf4bc
info: WebApplication2.Program.Test[0]
Transient.Dispose()
info: WebApplication2.Program.Test[0]
Transient.Dispose()
info: WebApplication2.Program.Test[0]
Transient.Dispose()
info: WebApplication2.Program.Test[0]
Transient.Dispose()
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
Request finished in 1.8306ms 200
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Text;
namespace WebApplication2
{
public class Program
{
public static void Main(string[] args) =>
BuildWebHost(args).Run();
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Program>()
.Build();
public void ConfigureServices(IServiceCollection services) =>
services.AddScoped(svc =>
{
var logger = svc.GetRequiredService<ILogger<Test<Scoped>>>();
return new Test<Scoped>(logger);
})
.AddSingleton(svc =>
{
var logger = svc.GetRequiredService<ILogger<Test<Singleton>>>();
return new Test<Singleton>(logger);
})
.AddTransient(svc =>
{
var logger = svc.GetRequiredService<ILogger<Test<Transient>>>();
return new Test<Transient>(logger);
});
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.Run(async (context) =>
{
var req = context.Request.Path;
var id = req.StartsWithSegments("/scoped")
? GetIdFromScoped(context.RequestServices)
: req.StartsWithSegments("/singleton")
? GetIdFromSingleton(context.RequestServices)
: req.StartsWithSegments("/transient", out var remaining)
? GetIdFromTransient(context.RequestServices, GetCountFromPath(remaining))
: GetIdForDefaultPath(context.RequestServices);
await context.Response.WriteAsync(id);
string GetIdForDefaultPath(IServiceProvider services) =>
GetIdFromScoped(services);
string GetIdFromScoped(IServiceProvider services) =>
services.GetRequiredService<Test<Scoped>>().GetId().ToString();
string GetIdFromSingleton(IServiceProvider services) =>
services.GetRequiredService<Test<Singleton>>().GetId().ToString();
string GetIdFromTransient(IServiceProvider services, int count)
{
var sb = new StringBuilder(36 * count);
for (int i = 0; i < count; i++)
{
sb.AppendLine(services.GetRequiredService<Test<Transient>>().GetId().ToString());
}
return sb.ToString();
}
int GetCountFromPath(PathString path) =>
int.TryParse(path.Value.Length > 0 ? path.Value.Substring(1) : "", out var i) ? i : 1;
});
}
struct Scoped { }
struct Singleton { }
struct Transient { }
class Test<T> : IDisposable
{
readonly ILogger<Test<T>> _logger;
public Test(ILogger<Test<T>> logger)
{
_logger = logger;
_logger.LogInformation("new {typeName}()", typeof(T).Name);
}
public Guid GetId()
{
Guid id = Guid.NewGuid();
_logger.LogInformation("{typeName}.GetId() = {id}", typeof(T).Name, id);
return id;
}
public void Dispose() =>
_logger.LogInformation("{typeName}.Dispose()", typeof(T).Name);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment