Skip to content

Instantly share code, notes, and snippets.

View alistairjevans's full-sized avatar

Alistair Evans alistairjevans

View GitHub Profile
@alistairjevans
alistairjevans / autofac-changes.md
Last active January 22, 2020 20:27
Autofac Changes for 5.0

Autofac Changes in 5.0

Framework Version Targeting Changes

From Autofac 5, we will no longer provide a target for .NET 4.5. The minimum version of .NET Framework you can use Autofac with is .NET 4.6.1.

The full targeting list is now:

  • netstandard2.1
#if NETCOREAPP3_0
using System;
using System.Collections.Generic;
using System.Text;
using Xunit;
namespace Autofac.Test
{
@alistairjevans
alistairjevans / benchmark.cs
Created October 26, 2019 10:44
TupleDictionary Benchmark
public class GenericTupleDictionaryBenchmark
{
[Params(100, 500, 1000, 2000, 5000, 10000)]
public int DictSize { get; set; }
private (Type open, Type closed)[] allTestTypes = new[]
{
(typeof(Service1<>), typeof(Service1<string>)),
(typeof(Service2<>), typeof(Service2<ContainerBuilder>)),
(typeof(Service3<>), typeof(Service3<int>)),
@alistairjevans
alistairjevans / myproject.csproj
Created October 24, 2019 14:43
Targeting netstandard2.0
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'net461' ">
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="1.1.0-preview1.19504.10" />
</ItemGroup>
@alistairjevans
alistairjevans / MyDisposableObject.cs
Created October 24, 2019 14:30
Call GC.SuppressFinalize
private class MyDisposableObject : IDisposable, IAsyncDisposable
{
private SqlConnection myResource = new SqlConnection();
private bool isDisposed = false;
public void Dispose()
{
if (!isDisposed)
{
isDisposed = true;
@alistairjevans
alistairjevans / MyDisposableObject.cs
Last active October 24, 2019 13:50
Dispose once only
private class MyDisposableObject : IDisposable, IAsyncDisposable
{
private SqlConnection myResource = new SqlConnection();
private bool isDisposed = false;
public void Dispose()
{
if (!isDisposed)
{
isDisposed = true;
class BaseClassThatMightHaveChildrenWithResources : IDisposable, IAsyncDisposable
{
public virtual void Dispose()
{
}
public virtual ValueTask DisposeAsync()
{
Dispose();
return default;
class MyObjectWithALock : IDisposable, IAsyncDisposable
{
private SemaphoreSlim semaphore = new SemaphoreSlim(1, 1);
public void Dispose()
{
semaphore.Dispose();
}
public async ValueTask DisposeAsync()
@alistairjevans
alistairjevans / program.cs
Last active October 24, 2019 14:44
Scope throws an exception
// Get a host builder
var hostBuilder = CreateHostBuilder(args);
// Add our service as scoped.
hostBuilder.ConfigureServices(srv =>
{
srv.AddScoped<MyAsyncOnlyDisposableObject>();
});
// Get our container
@alistairjevans
alistairjevans / NotAsync.cs
Created October 24, 2019 12:37
Non-async methods being forced to dispose sync
private void NotAnAsyncMethod()
{
var disposableObject = new MyDisposableObject();
// Unpleasant
disposableObject.DisposeAsync().GetAwaiter().GetResult();
}