Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix ms di no available scope #662

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
_ReSharper*/
*.csproj.ReSharper
.cr
.idea

# Build output
build/
Expand Down
7 changes: 7 additions & 0 deletions Castle.Windsor.sln
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "BuildScripts", "BuildScript
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Castle.Windsor.Extensions.Hosting", "src\Castle.Windsor.Extensions.Hosting\Castle.Windsor.Extensions.Hosting.csproj", "{4F123B9A-F9B3-4F81-A0EC-8C8DEF03298A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Castle.Windsor.Extensions.DependencyInjection.Tests.AspNetCoreApp", "src\Castle.Windsor.Extensions.DependencyInjection.Tests.AspNetCoreApp\Castle.Windsor.Extensions.DependencyInjection.Tests.AspNetCoreApp.csproj", "{418B8D35-3225-45B6-AAE0-B1C4B3303CFE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -142,6 +144,10 @@ Global
{4F123B9A-F9B3-4F81-A0EC-8C8DEF03298A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4F123B9A-F9B3-4F81-A0EC-8C8DEF03298A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4F123B9A-F9B3-4F81-A0EC-8C8DEF03298A}.Release|Any CPU.Build.0 = Release|Any CPU
{418B8D35-3225-45B6-AAE0-B1C4B3303CFE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{418B8D35-3225-45B6-AAE0-B1C4B3303CFE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{418B8D35-3225-45B6-AAE0-B1C4B3303CFE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{418B8D35-3225-45B6-AAE0-B1C4B3303CFE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -164,6 +170,7 @@ Global
{7C2F5733-0E06-4201-A15A-1ED1F3308DB4} = {7935AFF5-BF6D-4D59-8D66-058B6557F70F}
{DD7F7887-F27C-4C52-AA41-0F386D4D9996} = {7E507A42-984B-470D-8A0C-648B9AF8E1DC}
{4F123B9A-F9B3-4F81-A0EC-8C8DEF03298A} = {7935AFF5-BF6D-4D59-8D66-058B6557F70F}
{418B8D35-3225-45B6-AAE0-B1C4B3303CFE} = {7935AFF5-BF6D-4D59-8D66-058B6557F70F}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {CC2A1EB6-49EC-4064-AD8B-29439E8A45E4}
Expand Down
1 change: 1 addition & 0 deletions Castle.Windsor.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ limitations under the License.
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpKeepExistingMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpPlaceEmbeddedOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpRenamePlacementToArrangementMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpUseContinuousIndentInsideBracesMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EAddAccessorOwnerDeclarationBracesMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002ECSharpPlaceAttributeOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateBlankLinesAroundFieldToBlankLinesAroundProperty/@EntryIndexedValue">True</s:Boolean>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net6.0</TargetFrameworks>

<IsPackable>false</IsPackable>

<AssemblyName>Castle.Windsor.Extensions.DependencyInjection.Tests.AspNetCoreApp</AssemblyName>
<RootNamespace>Castle.Windsor.Extensions.DependencyInjection.Tests.AspNetCoreApp</RootNamespace>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Castle.Windsor.Extensions.DependencyInjection\Castle.Windsor.Extensions.DependencyInjection.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2004-2020 Castle Project - http://www.castleproject.org/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Castle.Windsor.Extensions.DependencyInjection.Tests.AspNetCoreApp.Components
{
public class DecoratedUserService : UserService
{
public IUserService UserService { get; }

public DecoratedUserService(IUserService userService)
{
UserService = userService;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2004-2020 Castle Project - http://www.castleproject.org/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Castle.Windsor.Extensions.DependencyInjection.Tests.AspNetCoreApp.Components
{
public interface IUserService
{

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2004-2020 Castle Project - http://www.castleproject.org/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Castle.Windsor.Extensions.DependencyInjection.Tests.AspNetCoreApp.Components
{
public class UserService : IUserService
{

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright 2004-2023 Castle Project - http://www.castleproject.org/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Castle.Windsor.Extensions.DependencyInjection.Tests.AspNetCoreApp
{
using Microsoft.AspNetCore.Builder;
#if NETCOREAPP3_1
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
#endif
using Microsoft.Extensions.Hosting;

public class Program
{
#if NET6_0
public static void Main(string[] args)
{
// create web application builder
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

// configure container
WindsorContainer container = new WindsorContainer();
WindsorServiceProviderFactory factory = new WindsorServiceProviderFactory(container);
IHostBuilder _ = builder.Host.UseServiceProviderFactory(factory);

// configure pipeline
WebApplication app = builder.Build();
app.MapGet("/", () => "Hello world");

// start
app.Run();
}
#elif NETCOREAPP3_1
public static void Main(string[] args)
{
// create host builder
IHost app =
CreateHostBuilder(args)
.Build();

// start
app.Run();
}

private static IHostBuilder CreateHostBuilder(string[] args)
{
WindsorContainer container = new WindsorContainer();
WindsorServiceProviderFactory factory = new WindsorServiceProviderFactory(container);
return Host
.CreateDefaultBuilder(args)
.UseServiceProviderFactory(factory)
.ConfigureWebHostDefaults(hostBuilder => hostBuilder.UseStartup<Startup>());
}

public class Startup
{
public void ConfigureServices(IServiceCollection serviceCollection)
{
}

public void Configure(IApplicationBuilder app)
{
app.Map(
string.Empty,
appBuilder =>
{
appBuilder.Run(async (context) => await context.Response.WriteAsync("Hello world"));
});
}
}
#endif
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2004-2023 Castle Project - http://www.castleproject.org/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Castle.Windsor.Extensions.DependencyInjection.Tests
{
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;

using Castle.Windsor.Extensions.DependencyInjection.Tests.AspNetCoreApp;

using Microsoft.AspNetCore.Mvc.Testing;

using Xunit;

public class AspNetCoreScopeTests
{
[Fact]
public async Task TestScopeIsAvailable()
{
WebApplicationFactory<Program> factory = new WebApplicationFactory<Program>();
HttpClient client = factory.CreateClient();
HttpResponseMessage response = await client.GetAsync("/");
Assert.NotNull(response);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,22 @@
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)'=='netcoreapp3.1'">
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.3" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Specification.Tests" Version="3.1.3" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="3.1.32" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.32" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Specification.Tests" Version="3.1.32" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)'=='net6.0'">
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.20" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Specification.Tests" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Castle.Windsor.Extensions.DependencyInjection\Castle.Windsor.Extensions.DependencyInjection.csproj" />
<ProjectReference Include="..\Castle.Windsor.Extensions.DependencyInjection.Tests.AspNetCoreApp\Castle.Windsor.Extensions.DependencyInjection.Tests.AspNetCoreApp.csproj" />
</ItemGroup>



</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using Castle.MicroKernel.Registration;
using Castle.Windsor.Extensions.DependencyInjection.Tests.Components;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Threading;
using System.Threading.Tasks;
using Xunit;

namespace Castle.Windsor.Extensions.DependencyInjection.Tests {
public class ResolveFromThreadpoolUnsafe {
[Fact]
public async Task Can_Resolve_From_ServiceProvider() {

var serviceProvider = new ServiceCollection();
var container = new WindsorContainer();
var f = new WindsorServiceProviderFactory(container);
f.CreateBuilder(serviceProvider);

container.Register(
Component.For<IUserService>().ImplementedBy<UserService>()
);

IServiceProvider sp = f.CreateServiceProvider(container);

var actualUserService = sp.GetService<IUserService>();
Assert.NotNull(actualUserService);

/*
ThreadPool.UnsafeQueueUserWorkItem(state => {
// resolving using castle (without scopes) works
var actualUserService = container.Resolve<IUserService>(nameof(UserService));
Assert.NotNull(actualUserService);
}, null);
*/

TaskCompletionSource<IUserService> tcs = new TaskCompletionSource<IUserService>();

ThreadPool.UnsafeQueueUserWorkItem(state => {
try {
var actualUserService = sp.GetService<IUserService>();
Assert.NotNull(actualUserService);
}
catch (Exception ex) {
tcs.SetException(ex);
return;
}
tcs.SetResult(actualUserService);
}, null);

// Wait for the work item to complete.
var task = tcs.Task;
IUserService result = await task;
Assert.NotNull(result);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics;
using System.Linq;

// ReSharper disable once CheckNamespace
namespace Microsoft.Extensions.DependencyInjection.Specification
{
using System;
using System.Linq;

using StackTrace = System.Diagnostics.StackTrace;

public abstract class SkippableDependencyInjectionSpecificationTests : DependencyInjectionSpecificationTests
{
public string[] SkippedTests => new[] { "SingletonServiceCanBeResolvedFromScope" };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static ComponentRegistration<TService> ScopedToNetServiceScope<TService>(
public static ComponentRegistration<TService> LifestyleNetTransient<TService>(this ComponentRegistration<TService> registration) where TService : class
{
return registration
.Attribute(ExtensionContainerScopeBase.TransientMarker).Eq(Boolean.TrueString)
.Attribute(ExtensionContainerScope.TransientMarker).Eq(Boolean.TrueString)
.LifeStyle.ScopedToNetServiceScope(); //.NET core expects new instances but release on scope dispose
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@

namespace Castle.Windsor.Extensions.DependencyInjection.Scope
{
internal class ExtensionContainerRootScope : ExtensionContainerScopeBase
internal class ExtensionContainerRootScope : ExtensionContainerScope
{

private ExtensionContainerRootScope() : base(null, null)
{
}

public static ExtensionContainerRootScope BeginRootScope()
{
var scope = new ExtensionContainerRootScope();
ExtensionContainerScopeCache.Current = scope;
current.Value = scope;
return scope;
}

internal override ExtensionContainerScopeBase RootScope => this;
}
}
Loading