Skip to content

Commit

Permalink
Upgraded to .netstandard 2.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
hikalkan committed Aug 15, 2017
1 parent 7d91632 commit d616a4b
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<VersionPrefix>1.3.0</VersionPrefix>
<TargetFramework>netcoreapp1.0</TargetFramework>
<TargetFramework>netcoreapp2.0</TargetFramework>
<AssemblyName>Castle.Windsor.MsDependencyInjection.Tests</AssemblyName>
<PackageId>Castle.Windsor.MsDependencyInjection.Tests</PackageId>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
Expand All @@ -17,17 +17,12 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Include="xunit" Version="2.2.0-beta4-build3444" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
<PackageReference Include="xunit.extensibility.execution" Version="2.2.0-beta4-build3444" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Specification.Tests" Version="1.1.0" />
<PackageReference Include="Shouldly" Version="2.8.2" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net461' ">
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
<PackageReference Include="xunit.extensibility.execution" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Specification.Tests" Version="2.0.0" />
<PackageReference Include="Shouldly" Version="2.8.3" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,6 @@ public void Resolving_On_Same_Scope_Should_Be_Thread_Safe()
});
}

//TODO: TEST - Globally resolved objects should be disposed when the container is disposed?

public void Dispose()
{
Assert.Null(MsLifetimeScope.Current);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<Version>2.1.1</Version>
<TargetFrameworks>net452;net46;netstandard1.6</TargetFrameworks>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<AssemblyName>Castle.Windsor.MsDependencyInjection</AssemblyName>
<PackageId>Castle.Windsor.MsDependencyInjection</PackageId>
<PackageTags>Windsor;Castle;AspNetCore;DI;IoC;DNX;DependencyInjection;Extensions</PackageTags>
Expand All @@ -16,37 +16,17 @@
</PropertyGroup>

<ItemGroup>
<None Include="bin\Release\net452\Castle.Windsor.MsDependencyInjection.pdb">
<PackagePath>lib/net452/</PackagePath>
<Pack>true</Pack>
</None>
<None Include="bin\Release\net46\Castle.Windsor.MsDependencyInjection.pdb">
<PackagePath>lib/net46/</PackagePath>
<Pack>true</Pack>
</None>
<None Include="bin\Release\netstandard1.6\Castle.Windsor.MsDependencyInjection.pdb">
<PackagePath>lib/netstandard1.6/</PackagePath>
<None Include="bin\Release\netstandard2.0\Castle.Windsor.MsDependencyInjection.pdb">
<PackagePath>lib/netstandard2.0/</PackagePath>
<Pack>true</Pack>
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="1.1.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="2.0.0" />
<PackageReference Include="Castle.Windsor" Version="4.0.0" />
<PackageReference Include="SourceLink.Create.CommandLine" Version="2.1.2" PrivateAssets="All" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net452' ">
<Reference Include="System.Runtime" />
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net46' ">
<Reference Include="System.Runtime" />
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,11 @@ public GlobalMsLifetimeScope(IWindsorContainer container)
: base(container)
{
}

protected override void DisposeInternal()
{
base.DisposeInternal();
Container.Dispose();
}
}
}
23 changes: 18 additions & 5 deletions src/Castle.Windsor.MsDependencyInjection/MsLifeTimeScope.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Castle.Core.Internal;
using Castle.MicroKernel.Lifestyle.Scoped;
Expand Down Expand Up @@ -33,21 +34,22 @@ public static IMsLifetimeScope Current

public ILifetimeScope WindsorLifeTimeScope { get; }

protected IWindsorContainer Container { get; }

private readonly List<MsLifetimeScope> _children;

private readonly HashSet<object> _resolvedInstances;
private readonly IWindsorContainer _container;
private readonly List<object> _resolvedInstances;

private ThreadSafeFlag _disposed;

public MsLifetimeScope(IWindsorContainer container)
{
_container = container;
Container = container;

WindsorLifeTimeScope = new DefaultLifetimeScope();

_children = new List<MsLifetimeScope>();
_resolvedInstances = new HashSet<object>();
_resolvedInstances = new List<object>();
_disposed = new ThreadSafeFlag();
}

Expand Down Expand Up @@ -82,8 +84,15 @@ public void Dispose()
return;
}

DisposeInternal();
}

protected virtual void DisposeInternal()
{
lock (_children)
{
_children.Reverse();

foreach (var child in _children)
{
child.Dispose();
Expand All @@ -94,10 +103,14 @@ public void Dispose()

lock (_resolvedInstances)
{
_resolvedInstances.Reverse();

foreach (var instance in _resolvedInstances)
{
_container.Release(instance);
Container.Release(instance);
}

_resolvedInstances.Clear();
}

WindsorLifeTimeScope.Dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,18 @@ namespace Castle.Windsor.MsDependencyInjection
/// <summary>
/// Implements <see cref="IServiceProvider"/>.
/// </summary>
public class ScopedWindsorServiceProvider : IServiceProvider, ISupportRequiredService
public class ScopedWindsorServiceProvider : IServiceProvider, ISupportRequiredService, IDisposable
{
private readonly IWindsorContainer _container;
private readonly IMsLifetimeScope _ownMsLifetimeScope;


#if NET452
public static bool IsInResolving
{
get { return _isInResolving; }
set { _isInResolving = value; }
}

[ThreadStatic]
private static bool _isInResolving;
#else
public static bool IsInResolving
{
get { return _isInResolving.Value; }
set { _isInResolving.Value = value; }
get => _isInResolving.Value;
set => _isInResolving.Value = value;
}

private static readonly AsyncLocal<bool> _isInResolving = new AsyncLocal<bool>();
#endif

public ScopedWindsorServiceProvider(IWindsorContainer container, MsLifetimeScopeProvider msLifetimeScopeProvider)
{
Expand Down Expand Up @@ -109,5 +97,13 @@ private object ResolveInstanceOrNull(Type serviceType, bool isOptional)
//Let Castle Windsor throws exception since the service is not registered!
return _container.Resolve(serviceType);
}

public void Dispose()
{
if (_ownMsLifetimeScope is GlobalMsLifetimeScope)
{
_ownMsLifetimeScope.Dispose();
}
}
}
}

0 comments on commit d616a4b

Please sign in to comment.