diff --git a/package.props b/package.props
index d7414af..03dfb46 100644
--- a/package.props
+++ b/package.props
@@ -1,7 +1,7 @@
- 2.1.2
+ 2.1.3
Bug fixes, dependency updates and minor performance optimizations
diff --git a/src/HostingExtension.cs b/src/HostingExtension.cs
index 42c6186..cd360cd 100644
--- a/src/HostingExtension.cs
+++ b/src/HostingExtension.cs
@@ -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 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>(factory));
- services.Replace(ServiceDescriptor.Singleton>(factory));
+ services.Replace(ServiceDescriptor.Singleton>(_factory));
+ services.Replace(ServiceDescriptor.Singleton>(_factory));
});
#else
return hostBuilder.ConfigureServices((context, services) =>
{
- services.Replace(ServiceDescriptor.Singleton>(factory));
- services.Replace(ServiceDescriptor.Singleton>(factory));
+ services.Replace(ServiceDescriptor.Singleton>(_factory));
+ services.Replace(ServiceDescriptor.Singleton>(_factory));
});
#endif
}
diff --git a/src/ServiceProvider.cs b/src/ServiceProvider.cs
index 793c70b..ad1f873 100644
--- a/src/ServiceProvider.cs
+++ b/src/ServiceProvider.cs
@@ -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(this, new ExternallyControlledLifetimeManager());
- _options.UnityContainer.RegisterInstance(this, new ExternallyControlledLifetimeManager());
- _options.UnityContainer.RegisterInstance(this, new ExternallyControlledLifetimeManager());
+ _container = container;
+ _container.RegisterInstance(this, new ExternallyControlledLifetimeManager());
+ _container.RegisterInstance(this, new ExternallyControlledLifetimeManager());
+ _container.RegisterInstance(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());
- }
- catch
- {
- parameters.ResolutionFailureHanlder?.Invoke(serviceType);
+ return _container.Resolve(serviceType);
}
+ catch { /* Ignore */}
return null;
}
@@ -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
@@ -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
@@ -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
diff --git a/src/ServiceProviderExtensions.cs b/src/ServiceProviderExtensions.cs
index fc41987..36ea4c4 100644
--- a/src/ServiceProviderExtensions.cs
+++ b/src/ServiceProviderExtensions.cs
@@ -17,10 +17,8 @@ public static class ServiceProviderExtensions
/// The .
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));
}
///
@@ -32,10 +30,8 @@ public static IServiceProvider BuildServiceProvider(this IServiceCollection serv
/// Service provider
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));
}
///
@@ -47,10 +43,8 @@ public static IServiceProvider BuildServiceProvider(this IServiceCollection serv
/// Service provider
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));
}
}
}
diff --git a/src/ServiceProviderFactory.cs b/src/ServiceProviderFactory.cs
index 1e72f4f..030dc4a 100644
--- a/src/ServiceProviderFactory.cs
+++ b/src/ServiceProviderFactory.cs
@@ -1,5 +1,5 @@
-using Microsoft.Extensions.DependencyInjection;
-using System;
+using System;
+using Microsoft.Extensions.DependencyInjection;
using Unity.Lifetime;
namespace Unity.Microsoft.DependencyInjection
@@ -7,16 +7,19 @@ namespace Unity.Microsoft.DependencyInjection
public class ServiceProviderFactory : IServiceProviderFactory,
IServiceProviderFactory
{
- private readonly Action _config;
+ private readonly IUnityContainer _container;
- public ServiceProviderFactory(Action config)
+ public ServiceProviderFactory(IUnityContainer container)
{
- _config = config;
+ _container = container ?? new UnityContainer();
+
+ _container.RegisterInstance>(this, new ContainerControlledLifetimeManager());
+ _container.RegisterInstance>(this, new ExternallyControlledLifetimeManager());
}
public IServiceProvider CreateServiceProvider(IUnityContainer container)
{
- return new ServiceProvider(CreateOptions().With(container));
+ return new ServiceProvider(container);
}
public IServiceProvider CreateServiceProvider(IServiceCollection containerBuilder)
@@ -26,7 +29,7 @@ public IServiceProvider CreateServiceProvider(IServiceCollection containerBuilde
IUnityContainer IServiceProviderFactory.CreateBuilder(IServiceCollection services)
{
- return CreateServiceProviderContainer(services).UnityContainer;
+ return CreateServiceProviderContainer(services);
}
IServiceCollection IServiceProviderFactory.CreateBuilder(IServiceCollection services)
@@ -34,28 +37,14 @@ IServiceCollection IServiceProviderFactory.CreateBuilder(ISe
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>(this, new ContainerControlledLifetimeManager());
- container.RegisterInstance>(this, new ExternallyControlledLifetimeManager());
+ return container.AddExtension(new MdiExtension())
+ .AddServices(services);
}
}
}
diff --git a/src/Unity.Microsoft.DependencyInjection.csproj b/src/Unity.Microsoft.DependencyInjection.csproj
index a7a0e05..f418f75 100644
--- a/src/Unity.Microsoft.DependencyInjection.csproj
+++ b/src/Unity.Microsoft.DependencyInjection.csproj
@@ -3,8 +3,8 @@
- 2.1.2.1
- 2.1.2.1
+ $(Version).0
+ $(Version).0
Unity.Microsoft.DependencyInjection
Unity for Microsoft Dependency Injection framework.
Copyright © Unity Container Project 2018
@@ -19,13 +19,11 @@
Unity.Microsoft.DependencyInjection
..\..\Abstractions\src\Unity.Abstractions.csproj
..\..\Container\src\Unity.Container.csproj
- 2.1.2.1
- Extended to allow for resolution customization. Updated error language to english.
- true
+ false
netstandard2.0
Full
@@ -52,17 +50,17 @@
-
-
+
+
-
+
-
+
diff --git a/src/UnityConfigurationOptions.cs b/src/UnityConfigurationOptions.cs
deleted file mode 100644
index 362444a..0000000
--- a/src/UnityConfigurationOptions.cs
+++ /dev/null
@@ -1,53 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Text;
-using Unity.Resolution;
-
-namespace Unity.Microsoft.DependencyInjection
-{
- public class UnityConfigurationOptions
- {
- ///
- /// The container to use as the root container.
- ///
- public IUnityContainer UnityContainer { get; set; }
- ///
- /// Controls the unity resolution parameters when resolving a type via the ServiceProvider.
- ///
- public Action ResolveConfiguration { get; set; }
- ///
- /// Configures the scope options when a new child scope is created.
- ///
- public Action CreateScope { get; set; }
-
- public UnityConfigurationOptions With(IUnityContainer unityContainer = null, Action resolutionConfiguration = null, Action createScope = null)
- {
- return new UnityConfigurationOptions
- {
- UnityContainer = unityContainer ?? UnityContainer,
- ResolveConfiguration = resolutionConfiguration ?? ResolveConfiguration,
- CreateScope = createScope ?? CreateScope
- };
- }
- }
-
- public class ResolutionParameters
- {
- ///
- /// The type being resolved.
- ///
- public Type Type { get; set; }
- ///
- /// The name to use when resolving the type.
- ///
- public string Name { get; set; }
- ///
- /// The resolver overrides to use when resolving the type.
- ///
- public ResolverOverride[] ResolverOverrides { get; set; }
- ///
- /// Action to call when a type resolution fails.
- ///
- public Action ResolutionFailureHanlder { get; set; }
- }
-}
diff --git a/tests/GitHub.cs b/tests/GitHub.cs
index 2a0ea4b..320e62a 100644
--- a/tests/GitHub.cs
+++ b/tests/GitHub.cs
@@ -6,7 +6,7 @@
namespace Unity.Microsoft.DependencyInjection.Tests
{
- public class GitHubIssues
+ public class IssuesOnGitHub
{
public interface IScopedService
{
@@ -85,7 +85,7 @@ public void Issue_29_Factory()
[Fact]
- public void Test2_failing()
+ public void Issue_28()
{
var serviceCollection = new ServiceCollection();
@@ -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();
+ var httpFactory0 = sp.GetRequiredService();
using (var scope = scopeFactory.CreateScope())
{
var httpFactory = scope.ServiceProvider.GetRequiredService();
diff --git a/tests/Microsoft.DependencyInjection.Tests.csproj b/tests/Microsoft.DependencyInjection.Tests.csproj
index f8340fd..1667a50 100644
--- a/tests/Microsoft.DependencyInjection.Tests.csproj
+++ b/tests/Microsoft.DependencyInjection.Tests.csproj
@@ -1,4 +1,4 @@
-
+
netcoreapp2.0
@@ -7,10 +7,9 @@
-
-
-
-
+
+
+
all
runtime; build; native; contentfiles; analyzers
@@ -25,7 +24,8 @@
-
+
+