Skip to content

Commit

Permalink
Releasing 2.1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
ENikS committed Dec 9, 2018
1 parent d950c53 commit 144f184
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 155 deletions.
2 changes: 1 addition & 1 deletion package.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>

<PropertyGroup>
<Version>2.1.2</Version>
<Version>2.1.3</Version>
<PackageReleaseNotes>Bug fixes, dependency updates and minor performance optimizations</PackageReleaseNotes>
</PropertyGroup>

Expand Down
18 changes: 7 additions & 11 deletions src/HostingExtension.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System;

namespace Unity.Microsoft.DependencyInjection
{
public static class HostingExtension
{
public static IWebHostBuilder UseUnityServiceProvider(this IWebHostBuilder hostBuilder, IUnityContainer container = null)
{
return UseUnityServiceProvider(hostBuilder, c => c.UnityContainer = container);
}
private static ServiceProviderFactory _factory;

public static IWebHostBuilder UseUnityServiceProvider(this IWebHostBuilder hostBuilder, Action<UnityConfigurationOptions> config)
public static IWebHostBuilder UseUnityServiceProvider(this IWebHostBuilder hostBuilder, IUnityContainer container = null)
{
var factory = new ServiceProviderFactory(config);
_factory = new ServiceProviderFactory(container);

#if NETCOREAPP1_1
return hostBuilder.ConfigureServices((services) =>
{
services.Replace(ServiceDescriptor.Singleton<IServiceProviderFactory<IUnityContainer>>(factory));
services.Replace(ServiceDescriptor.Singleton<IServiceProviderFactory<IServiceCollection>>(factory));
services.Replace(ServiceDescriptor.Singleton<IServiceProviderFactory<IUnityContainer>>(_factory));
services.Replace(ServiceDescriptor.Singleton<IServiceProviderFactory<IServiceCollection>>(_factory));
});
#else
return hostBuilder.ConfigureServices((context, services) =>
{
services.Replace(ServiceDescriptor.Singleton<IServiceProviderFactory<IUnityContainer>>(factory));
services.Replace(ServiceDescriptor.Singleton<IServiceProviderFactory<IServiceCollection>>(factory));
services.Replace(ServiceDescriptor.Singleton<IServiceProviderFactory<IUnityContainer>>(_factory));
services.Replace(ServiceDescriptor.Singleton<IServiceProviderFactory<IServiceCollection>>(_factory));
});
#endif
}
Expand Down
54 changes: 20 additions & 34 deletions src/ServiceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,31 @@

namespace Unity.Microsoft.DependencyInjection
{
public class ServiceProvider : IServiceProvider,
IServiceScopeFactory,
IServiceScope,
public class ServiceProvider : IServiceProvider,
IServiceScopeFactory,
IServiceScope,
IDisposable
{
private readonly UnityConfigurationOptions _options;
private IUnityContainer _container;

internal ServiceProvider(UnityConfigurationOptions options)

internal ServiceProvider(IUnityContainer container)
{
_options = options;
_options.UnityContainer.RegisterInstance<IServiceScope>(this, new ExternallyControlledLifetimeManager());
_options.UnityContainer.RegisterInstance<IServiceProvider>(this, new ExternallyControlledLifetimeManager());
_options.UnityContainer.RegisterInstance<IServiceScopeFactory>(this, new ExternallyControlledLifetimeManager());
_container = container;
_container.RegisterInstance<IServiceScope>(this, new ExternallyControlledLifetimeManager());
_container.RegisterInstance<IServiceProvider>(this, new ExternallyControlledLifetimeManager());
_container.RegisterInstance<IServiceScopeFactory>(this, new ExternallyControlledLifetimeManager());
}

#region IServiceProvider

public object GetService(Type serviceType)
{
ResolutionParameters parameters = new ResolutionParameters { Type = serviceType };

_options.ResolveConfiguration?.Invoke(parameters);

try
{
return _options.UnityContainer.Resolve(parameters.Type, parameters.Name, parameters.ResolverOverrides ?? Array.Empty<Resolution.ResolverOverride>());
}
catch
{
parameters.ResolutionFailureHanlder?.Invoke(serviceType);
return _container.Resolve(serviceType);
}
catch { /* Ignore */}

return null;
}
Expand All @@ -46,10 +40,7 @@ public object GetService(Type serviceType)

public IServiceScope CreateScope()
{
var childOptions = _options.With(_options.UnityContainer.CreateChildContainer());
childOptions.CreateScope?.Invoke(childOptions);

return new ServiceProvider(childOptions);
return new ServiceProvider(_container.CreateChildContainer());
}

#endregion
Expand All @@ -66,15 +57,13 @@ public IServiceScope CreateScope()

public static IServiceProvider ConfigureServices(IServiceCollection services)
{
var container = new UnityContainer().AddExtension(new MdiExtension())
.AddServices(services);

return new ServiceProvider(new UnityConfigurationOptions { UnityContainer = container });
return new ServiceProvider(new UnityContainer().AddExtension(new MdiExtension())
.AddServices(services));
}

public static explicit operator UnityContainer(ServiceProvider c)
{
return (UnityContainer)c._options.UnityContainer;
return (UnityContainer)c._container;
}

#endregion
Expand All @@ -88,14 +77,11 @@ public void Dispose()
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool _)
{
_options.UnityContainer?.Dispose();
}

~ServiceProvider()
private void Dispose(bool _)
{
Dispose(false);
IDisposable disposable = _container;
_container = null;
disposable?.Dispose();
}

#endregion
Expand Down
18 changes: 6 additions & 12 deletions src/ServiceProviderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ public static class ServiceProviderExtensions
/// <returns>The <see cref="ServiceProvider"/>.</returns>
public static IServiceProvider BuildServiceProvider(this IServiceCollection services, bool validateScopes = false)
{
var container = new UnityContainer().AddExtension(new MdiExtension())
.AddServices(services);

return new ServiceProvider(new UnityConfigurationOptions { UnityContainer = container });
return new ServiceProvider(new UnityContainer().AddExtension(new MdiExtension())
.AddServices(services));
}

/// <summary>
Expand All @@ -32,10 +30,8 @@ public static IServiceProvider BuildServiceProvider(this IServiceCollection serv
/// <returns>Service provider</returns>
public static IServiceProvider BuildServiceProvider(this IServiceCollection services, IUnityContainer container)
{
container.AddExtension(new MdiExtension())
.AddServices(services);

return new ServiceProvider(new UnityConfigurationOptions { UnityContainer = container });
return new ServiceProvider(container.AddExtension(new MdiExtension())
.AddServices(services));
}

/// <summary>
Expand All @@ -47,10 +43,8 @@ public static IServiceProvider BuildServiceProvider(this IServiceCollection serv
/// <returns>Service provider</returns>
public static IServiceProvider BuildServiceProvider(this IUnityContainer container, IServiceCollection services)
{
container.AddExtension(new MdiExtension())
.AddServices(services);

return new ServiceProvider(new UnityConfigurationOptions { UnityContainer = container });
return new ServiceProvider(container.AddExtension(new MdiExtension())
.AddServices(services));
}
}
}
41 changes: 15 additions & 26 deletions src/ServiceProviderFactory.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
using Microsoft.Extensions.DependencyInjection;
using System;
using System;
using Microsoft.Extensions.DependencyInjection;
using Unity.Lifetime;

namespace Unity.Microsoft.DependencyInjection
{
public class ServiceProviderFactory : IServiceProviderFactory<IUnityContainer>,
IServiceProviderFactory<IServiceCollection>
{
private readonly Action<UnityConfigurationOptions> _config;
private readonly IUnityContainer _container;

public ServiceProviderFactory(Action<UnityConfigurationOptions> config)
public ServiceProviderFactory(IUnityContainer container)
{
_config = config;
_container = container ?? new UnityContainer();

_container.RegisterInstance<IServiceProviderFactory<IUnityContainer>>(this, new ContainerControlledLifetimeManager());
_container.RegisterInstance<IServiceProviderFactory<IServiceCollection>>(this, new ExternallyControlledLifetimeManager());
}

public IServiceProvider CreateServiceProvider(IUnityContainer container)
{
return new ServiceProvider(CreateOptions().With(container));
return new ServiceProvider(container);
}

public IServiceProvider CreateServiceProvider(IServiceCollection containerBuilder)
Expand All @@ -26,36 +29,22 @@ public IServiceProvider CreateServiceProvider(IServiceCollection containerBuilde

IUnityContainer IServiceProviderFactory<IUnityContainer>.CreateBuilder(IServiceCollection services)
{
return CreateServiceProviderContainer(services).UnityContainer;
return CreateServiceProviderContainer(services);
}

IServiceCollection IServiceProviderFactory<IServiceCollection>.CreateBuilder(IServiceCollection services)
{
return services;
}

private UnityConfigurationOptions CreateServiceProviderContainer(IServiceCollection services)
{
var options = CreateOptions();
options.UnityContainer.AddServices(services);
return options;
}

private UnityConfigurationOptions CreateOptions()
private IUnityContainer CreateServiceProviderContainer(IServiceCollection services)
{
var options = new UnityConfigurationOptions();
_config(options);
options.UnityContainer = options.UnityContainer ?? new UnityContainer();
ConfigureContainer(options.UnityContainer);
var container = _container.CreateChildContainer();
new ServiceProviderFactory(container);

return options;
}

private void ConfigureContainer(IUnityContainer container)
{
container.AddExtension(new MdiExtension());
container.RegisterInstance<IServiceProviderFactory<IUnityContainer>>(this, new ContainerControlledLifetimeManager());
container.RegisterInstance<IServiceProviderFactory<IServiceCollection>>(this, new ExternallyControlledLifetimeManager());
return container.AddExtension(new MdiExtension())
.AddServices(services);
}
}
}
16 changes: 7 additions & 9 deletions src/Unity.Microsoft.DependencyInjection.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<Import Project="..\package.props" />

<PropertyGroup>
<FileVersion>2.1.2.1</FileVersion>
<AssemblyVersion>2.1.2.1</AssemblyVersion>
<FileVersion>$(Version).0</FileVersion>
<AssemblyVersion>$(Version).0</AssemblyVersion>
<PackageId>Unity.Microsoft.DependencyInjection</PackageId>
<Description>Unity for Microsoft Dependency Injection framework.</Description>
<Copyright>Copyright © Unity Container Project 2018</Copyright>
Expand All @@ -19,13 +19,11 @@
<RootNamespace>Unity.Microsoft.DependencyInjection</RootNamespace>
<UnityAbstractions>..\..\Abstractions\src\Unity.Abstractions.csproj</UnityAbstractions>
<UnityContainer>..\..\Container\src\Unity.Container.csproj</UnityContainer>
<Version>2.1.2.1</Version>
<PackageReleaseNotes>Extended to allow for resolution customization. Updated error language to english.</PackageReleaseNotes>
</PropertyGroup>


<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<TargetFramework>netstandard2.0</TargetFramework>
<DebugType>Full</DebugType>
</PropertyGroup>
Expand All @@ -52,17 +50,17 @@
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' != '' AND '$(TargetFramework)' == 'netstandard2.0' ">
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.2.0" />
</ItemGroup>


<ItemGroup Condition="!Exists('$(UnityAbstractions)')">
<PackageReference Include="Unity.Abstractions" Version="3.3.1" />
<PackageReference Include="Unity.Abstractions" Version="$(UnityAbstractionsVersion)" />
</ItemGroup>

<ItemGroup Condition="!Exists('$(UnityContainer)')">
<PackageReference Include="Unity.Container" Version="5.8.9" />
<PackageReference Include="Unity.Container" Version="$(UnityContainerVersion)" />
</ItemGroup>

</Project>
53 changes: 0 additions & 53 deletions src/UnityConfigurationOptions.cs

This file was deleted.

7 changes: 4 additions & 3 deletions tests/GitHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Unity.Microsoft.DependencyInjection.Tests
{
public class GitHubIssues
public class IssuesOnGitHub
{
public interface IScopedService
{
Expand Down Expand Up @@ -85,7 +85,7 @@ public void Issue_29_Factory()


[Fact]
public void Test2_failing()
public void Issue_28()
{

var serviceCollection = new ServiceCollection();
Expand All @@ -95,10 +95,11 @@ public void Test2_failing()

IUnityContainer container = new UnityContainer().CreateChildContainer();

var factory = new ServiceProviderFactory(c => c.UnityContainer = container);
var factory = new ServiceProviderFactory(container);

var sp = factory.CreateServiceProvider(serviceCollection);
var scopeFactory = sp.GetRequiredService<IServiceScopeFactory>();
var httpFactory0 = sp.GetRequiredService<IHttpClientFactory>();
using (var scope = scopeFactory.CreateScope())
{
var httpFactory = scope.ServiceProvider.GetRequiredService<IHttpClientFactory>();
Expand Down
Loading

0 comments on commit 144f184

Please sign in to comment.