Skip to content

Commit

Permalink
Merge branch 'feature-639' of https://github.com/simpleinjector/Simpl…
Browse files Browse the repository at this point in the history
…eInjector into v4.6.x
  • Loading branch information
dotnetjunkie committed May 10, 2019
2 parents fb979b5 + e0f1f73 commit 0ca698d
Show file tree
Hide file tree
Showing 11 changed files with 931 additions and 1 deletion.
Expand Up @@ -53,6 +53,10 @@
<DocumentationSource sourceFile="..\SimpleInjector.Integration.AspNetCore.Mvc.Core\bin\Release\netstandard2.0\SimpleInjector.Integration.AspNetCore.Mvc.Core.xml" />
<DocumentationSource sourceFile="..\SimpleInjector.Integration.AspNetCore.Mvc\bin\Release\netstandard2.0\SimpleInjector.Integration.AspNetCore.Mvc.dll" />
<DocumentationSource sourceFile="..\SimpleInjector.Integration.AspNetCore.Mvc\bin\Release\netstandard2.0\SimpleInjector.Integration.AspNetCore.Mvc.xml" />
<DocumentationSource sourceFile="..\SimpleInjector.Integration.ServiceCollection\bin\Release\netstandard2.0\SimpleInjector.Integration.ServiceCollection.dll" />
<DocumentationSource sourceFile="..\SimpleInjector.Integration.ServiceCollection\bin\Release\netstandard2.0\SimpleInjector.Integration.ServiceCollection.xml" />
<DocumentationSource sourceFile="..\SimpleInjector.Integration.GenericHost\bin\Release\netstandard2.0\SimpleInjector.Integration.GenericHost.dll" />
<DocumentationSource sourceFile="..\SimpleInjector.Integration.GenericHost\bin\Release\netstandard2.0\SimpleInjector.Integration.GenericHost.xml" />
</DocumentationSources>
<HelpAttributes />
<NamespaceSummaries>
Expand Down
@@ -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>

@@ -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;
}
}
}
@@ -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;
}
}
}
}
}
}
@@ -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; }
}
}
@@ -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>

0 comments on commit 0ca698d

Please sign in to comment.