Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature-639' of https://github.com/simpleinjector/Simpl…
…eInjector into v4.6.x
- Loading branch information
Showing
11 changed files
with
931 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/SimpleInjector.Integration.GenericHost/SimpleInjector.Integration.GenericHost.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<Description> | ||
Integrates Simple Injector with applications built upon .NET Generic Host. | ||
</Description> | ||
<AssemblyTitle>Simple Injector Generic Host Integration</AssemblyTitle> | ||
<NeutralLanguage>en-US</NeutralLanguage> | ||
<VersionPrefix>4.0.0</VersionPrefix> | ||
<Authors>Simple Injector Contributors</Authors> | ||
<TargetFrameworks>netstandard2.0</TargetFrameworks> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
<AssemblyName>SimpleInjector.Integration.GenericHost</AssemblyName> | ||
<AssemblyOriginatorKeyFile>../SimpleInjector.snk</AssemblyOriginatorKeyFile> | ||
<SignAssembly>true</SignAssembly> | ||
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign> | ||
<PackageId>SimpleInjector.Integration.GenericHost</PackageId> | ||
<PackageTags>simpleinjector;dependency-injection;DI;.NET;ASP.NET Core;Generic Host;GenericHost;IHost;HostBuilder</PackageTags> | ||
<PackageIconUrl>https://simpleinjector.org/simpleinjector.ico</PackageIconUrl> | ||
<PackageProjectUrl>https://simpleinjector.org/generichost</PackageProjectUrl> | ||
<PackageLicenseExpression>MIT</PackageLicenseExpression> | ||
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute> | ||
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute> | ||
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute> | ||
<GenerateAssemblyCopyrightAttribute>false</GenerateAssemblyCopyrightAttribute> | ||
<GenerateNeutralResourcesLanguageAttribute>false</GenerateNeutralResourcesLanguageAttribute> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="2.1.0" /> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.1.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\SimpleInjector\SimpleInjector.csproj" /> | ||
<ProjectReference Include="..\SimpleInjector.Integration.ServiceCollection\SimpleInjector.Integration.ServiceCollection.csproj" /> | ||
<PackageReference Include="NuSpec.ReferenceGenerator" Version="2.0.0-beta-bld14"> | ||
<PrivateAssets>All</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<Target Name="PostcompileScript" AfterTargets="Build" Condition=" '$(IsCrossTargetingBuild)' != 'true' "> | ||
<Exec Command="dotnet pack --no-build --configuration $(Configuration)" /> | ||
</Target> | ||
|
||
</Project> | ||
|
96 changes: 96 additions & 0 deletions
96
src/SimpleInjector.Integration.GenericHost/SimpleInjectorGenericHostExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
#region Copyright Simple Injector Contributors | ||
/* The Simple Injector is an easy-to-use Inversion of Control library for .NET | ||
* | ||
* Copyright (c) 2019 Simple Injector Contributors | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and | ||
* associated documentation files (the "Software"), to deal in the Software without restriction, including | ||
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the | ||
* following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all copies or substantial | ||
* portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT | ||
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO | ||
* EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | ||
* USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
#endregion | ||
|
||
namespace SimpleInjector | ||
{ | ||
using System; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using SimpleInjector.Integration.ServiceCollection; | ||
|
||
/// <summary> | ||
/// Extension methods for integrating Simple Injector with Generic Hosts. | ||
/// </summary> | ||
public static class SimpleInjectorGenericHostExtensions | ||
{ | ||
/// <summary> | ||
/// Registers the given <typeparamref name="THostedService"/> in the Container as Singleton and | ||
/// adds it to the host's pipeline of hosted services. | ||
/// </summary> | ||
/// <typeparam name="THostedService">An <see cref="IHostedService"/> to register.</typeparam> | ||
/// <param name="options">The options.</param> | ||
/// <returns>The <paramref name="options"/>.</returns> | ||
public static SimpleInjectorAddOptions AddHostedService<THostedService>( | ||
this SimpleInjectorAddOptions options) | ||
where THostedService : class, IHostedService | ||
{ | ||
if (options is null) | ||
{ | ||
throw new ArgumentNullException(nameof(options)); | ||
} | ||
|
||
var registration = Lifestyle.Singleton.CreateRegistration<THostedService>(options.Container); | ||
|
||
// Let the built-in configuration system dispose this instance. | ||
registration.SuppressDisposal = true; | ||
|
||
options.Container.AddRegistration<THostedService>(registration); | ||
|
||
options.Services.AddSingleton<IHostedService>( | ||
_ => options.Container.GetInstance<THostedService>()); | ||
|
||
return options; | ||
} | ||
|
||
/// <summary> | ||
/// Finalizes the configuration of Simple Injector on top of <see cref="IHost"/>. | ||
/// Ensures framework components can be injected into Simple Injector-resolved components, unless | ||
/// <see cref="SimpleInjectorUseOptions.AutoCrossWireFrameworkComponents"/> is set to <c>false</c> | ||
/// using the <paramref name="setupAction"/>. | ||
/// </summary> | ||
/// <param name="host">The application's <see cref="IHost"/>.</param> | ||
/// <param name="container">The application's <see cref="Container"/> instance.</param> | ||
/// <param name="setupAction">An optional setup action.</param> | ||
/// <returns>The supplied <paramref name="host"/>.</returns> | ||
/// <exception cref="ArgumentNullException">Thrown when <paramref name="host"/> or | ||
/// <paramref name="container"/> are null references.</exception> | ||
public static IHost UseSimpleInjector( | ||
this IHost host, | ||
Container container, | ||
Action<SimpleInjectorUseOptions> setupAction = null) | ||
{ | ||
if (host is null) | ||
{ | ||
throw new ArgumentNullException(nameof(host)); | ||
} | ||
|
||
if (container is null) | ||
{ | ||
throw new ArgumentNullException(nameof(container)); | ||
} | ||
|
||
host.Services.UseSimpleInjector(container, setupAction); | ||
|
||
return host; | ||
} | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
src/SimpleInjector.Integration.ServiceCollection/DefaultServiceProviderAccessor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#region Copyright Simple Injector Contributors | ||
/* The Simple Injector is an easy-to-use Inversion of Control library for .NET | ||
* | ||
* Copyright (c) 2019 Simple Injector Contributors | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and | ||
* associated documentation files (the "Software"), to deal in the Software without restriction, including | ||
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the | ||
* following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all copies or substantial | ||
* portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT | ||
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO | ||
* EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | ||
* USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
#endregion | ||
|
||
namespace SimpleInjector.Integration.ServiceCollection | ||
{ | ||
using System; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
internal class DefaultServiceProviderAccessor : IServiceProviderAccessor | ||
{ | ||
private readonly Container container; | ||
private InstanceProducer serviceScopeProducer; | ||
|
||
internal DefaultServiceProviderAccessor(Container container) | ||
{ | ||
this.container = container; | ||
} | ||
|
||
public IServiceProvider Current => this.CurentServiceScope.ServiceProvider; | ||
|
||
private IServiceScope CurentServiceScope | ||
{ | ||
get | ||
{ | ||
if (this.serviceScopeProducer == null) | ||
{ | ||
this.serviceScopeProducer = this.container.GetRegistration(typeof(IServiceScope), true); | ||
} | ||
|
||
try | ||
{ | ||
// This resolved IServiceScope will be cached inside a Simple Injector Scope and will be | ||
// disposed of when the scope is disposed of. | ||
return (IServiceScope)this.serviceScopeProducer.GetInstance(); | ||
} | ||
catch (Exception) | ||
{ | ||
var lifestyle = this.container.Options.DefaultScopedLifestyle; | ||
|
||
// PERF: Since GetIstance() checks the availability of the scope itself internally, we | ||
// would be duplicating the check (and duplicate the local storage access call). Doing | ||
// this inside the catch, therefore, prevents having to do the check on every resolve. | ||
if (Lifestyle.Scoped.GetCurrentScope(this.container) is null) | ||
{ | ||
throw new ActivationException( | ||
"You are trying to resolve a cross-wired service, but are doing so outside " + | ||
$"the context of an active ({lifestyle.Name}) scope. To be able to resolve " + | ||
"this service the operation must run in the context of such scope. " + | ||
"Please see https://simpleinjector.org/scoped for more information about how " + | ||
"to manage scopes."); | ||
} | ||
else | ||
{ | ||
throw; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/SimpleInjector.Integration.ServiceCollection/IServiceProviderAccessor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#region Copyright Simple Injector Contributors | ||
/* The Simple Injector is an easy-to-use Inversion of Control library for .NET | ||
* | ||
* Copyright (c) 2019 Simple Injector Contributors | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and | ||
* associated documentation files (the "Software"), to deal in the Software without restriction, including | ||
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the | ||
* following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all copies or substantial | ||
* portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT | ||
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO | ||
* EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | ||
* USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
#endregion | ||
|
||
namespace SimpleInjector.Integration.ServiceCollection | ||
{ | ||
using System; | ||
|
||
/// <summary> | ||
/// Allows access to the request's <see cref="IServiceProvider"/> instance. | ||
/// This interface is used by Simple Injector and allow it to resolve transient and scoped services from the | ||
/// framework's <see cref="IServiceProvider"/> through cross wiring. | ||
/// </summary> | ||
public interface IServiceProviderAccessor | ||
{ | ||
/// <summary> | ||
/// Gets the current <see cref="IServiceProvider"/> for the current scope or request. | ||
/// This operation will never return null. | ||
/// </summary> | ||
/// <value>An <see cref="IServiceProvider"/> instance.</value> | ||
IServiceProvider Current { get; } | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...njector.Integration.ServiceCollection/SimpleInjector.Integration.ServiceCollection.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<Description> | ||
Integrates Simple Injector with applications that require the use of IServiceCollection for registration of framework components. | ||
</Description> | ||
<AssemblyTitle>Simple Injector IServiceCollection Integration</AssemblyTitle> | ||
<NeutralLanguage>en-US</NeutralLanguage> | ||
<VersionPrefix>4.0.0</VersionPrefix> | ||
<Authors>Simple Injector Contributors</Authors> | ||
<TargetFrameworks>netstandard2.0</TargetFrameworks> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
<AssemblyName>SimpleInjector.Integration.ServiceCollection</AssemblyName> | ||
<AssemblyOriginatorKeyFile>../SimpleInjector.snk</AssemblyOriginatorKeyFile> | ||
<SignAssembly>true</SignAssembly> | ||
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign> | ||
<PackageId>SimpleInjector.Integration.ServiceCollection</PackageId> | ||
<PackageTags>simpleinjector;dependency-injection;DI;ASP.NET Core;IServiceCollection</PackageTags> | ||
<PackageIconUrl>https://simpleinjector.org/simpleinjector.ico</PackageIconUrl> | ||
<PackageProjectUrl>https://simpleinjector.org/netcoreconsole</PackageProjectUrl> | ||
<PackageLicenseExpression>MIT</PackageLicenseExpression> | ||
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute> | ||
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute> | ||
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute> | ||
<GenerateAssemblyCopyrightAttribute>false</GenerateAssemblyCopyrightAttribute> | ||
<GenerateNeutralResourcesLanguageAttribute>false</GenerateNeutralResourcesLanguageAttribute> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.0.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\SimpleInjector\SimpleInjector.csproj" /> | ||
<PackageReference Include="NuSpec.ReferenceGenerator" Version="2.0.0-beta-bld14"> | ||
<PrivateAssets>All</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<Target Name="PostcompileScript" AfterTargets="Build" Condition=" '$(IsCrossTargetingBuild)' != 'true' "> | ||
<Exec Command="dotnet pack --no-build --configuration $(Configuration)" /> | ||
</Target> | ||
|
||
</Project> | ||
|
Oops, something went wrong.