-
Notifications
You must be signed in to change notification settings - Fork 458
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
Support for .NET Core 3.x #517
Changes from 5 commits
de1621e
f723d87
b6655d5
90f31a2
864e3c6
fb8365c
e8a766a
d2e2c0a
0c1ca73
858c4b7
de7e469
d620c7f
92891b7
70743f1
0c94508
fea6656
4f0d338
043fcdb
9557c87
e1b13b8
240c9f9
e5537c7
70586b5
e79a539
5699f61
598740a
be676fa
21da7e0
6b86d13
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
namespace Castle.Windsor.Extensions.MsDependencyInjection.Tests | ||
{ | ||
using System; | ||
|
||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Specification; | ||
|
||
public class WindsorScopedServiceProviderTests : DependencyInjectionSpecificationTests | ||
{ | ||
protected override IServiceProvider CreateServiceProvider(IServiceCollection serviceCollection) | ||
{ | ||
var factory = new WindsorServiceProviderFactory(); | ||
var container = factory.CreateBuilder(serviceCollection); | ||
return factory.CreateServiceProvider(container); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
|
||
<IsPackable>false</IsPackable> | ||
|
||
<AssemblyName>Castle.Windsor.Extensions.MsDependencyInjection.Tests</AssemblyName> | ||
|
||
<RootNamespace>Castle.Windsor.Extensions.MsDependencyInjection.Tests</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Specification.Tests" Version="3.1.3" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" /> | ||
<PackageReference Include="xunit" Version="2.4.1" /> | ||
<PackageReference Include="xunit.assert" Version="2.4.1" /> | ||
<PackageReference Include="xunit.extensibility.core" Version="2.4.1" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" /> | ||
<PackageReference Include="coverlet.collector" Version="1.2.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Castle.Windsor.Extensions.MsDependencyInjection\Castle.Windsor.Extensions.MsDependencyInjection.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<Import Project="..\..\buildscripts\common.props"></Import> | ||
|
||
<PropertyGroup> | ||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> | ||
<PackageId>Castle.Windsor.Extensions.MsDependencyInjection</PackageId> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought we agreed that Since There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wasn't aware of that. Besides for .NET Core 3.x it still in separate repo. I wouldn't worry about .NET 5 - knowing how Core teams move, it might still change until they come to release builds Happy to rename to |
||
<Title>Castle Windsor extension for .NET Core Dependency Injection </Title> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
<Description>Allows to use Castle Windsor as a container using IServiceProvider</Description> | ||
<PackageTags>castle, windsor, inversionOfControl, DependencyInjection, aspnet, core</PackageTags> | ||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors> | ||
<NoWarn>$(NoWarn);NU5125</NoWarn> <!-- remove once tools are truly ready for NuGet's new 'license' element --> | ||
<AssemblyName>Castle.Windsor.Extensions.MsDependencyInjection</AssemblyName> | ||
<RootNamespace>Castle.Windsor.Extensions.MsDependencyInjection</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.3" /> | ||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.3" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Castle.Windsor\Castle.Windsor.csproj" /> | ||
</ItemGroup> | ||
</Project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// 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 Microsoft.Extensions.Hosting | ||
jonorossi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
using Castle.Windsor; | ||
using Castle.Windsor.Extensions.MsDependencyInjection; | ||
|
||
public static class HostBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Uses <see name="IWindsorContainer" /> as the DI container for the host | ||
/// </summary> | ||
/// <param name="hostBuilder">Host builder</param> | ||
/// <returns>Host builder</returns> | ||
public static IHostBuilder UseWindsorContainerServiceProvider(this IHostBuilder hostBuilder) | ||
{ | ||
return hostBuilder.UseServiceProviderFactory<IWindsorContainer>(new WindsorServiceProviderFactory()); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// 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.MsDependencyInjection | ||
{ | ||
using Castle.Core; | ||
using Castle.MicroKernel; | ||
using Castle.MicroKernel.Context; | ||
|
||
using Microsoft.Extensions.Logging; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comes with Microsoft.Extensions.Hosting but added as explicit as well. What's the policy on transient dependencies? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
We don't really have a policy, but I know Microsoft recently removed some transient dependencies between ASP.NET packages that weren't needed, which breaks situations like this. I think if the Windsor code uses the public API of the Logging package it should define an explicit dependency rather than just relying on it being there because something else brings it in. |
||
|
||
public class LoggerDependencyResolver : ISubDependencyResolver | ||
{ | ||
private readonly IKernel kernel; | ||
|
||
public LoggerDependencyResolver(IKernel kernel) | ||
{ | ||
this.kernel = kernel; | ||
} | ||
public bool CanResolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, DependencyModel dependency) | ||
{ | ||
return dependency.TargetType == typeof(ILogger); | ||
} | ||
|
||
public object Resolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, DependencyModel dependency) | ||
{ | ||
var factory = kernel.Resolve<ILoggerFactory>(); | ||
return factory.CreateLogger(RegistrationAdapter.OriginalComponentName(model.Name)); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// 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.MsDependencyInjection | ||
{ | ||
using System; | ||
|
||
using Castle.Core; | ||
using Castle.MicroKernel; | ||
using Castle.MicroKernel.Context; | ||
using Castle.MicroKernel.Resolvers.SpecializedResolvers; | ||
|
||
public class NetCoreCollectionResolver : CollectionResolver | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does this custom There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Has a check |
||
{ | ||
public NetCoreCollectionResolver(IKernel kernel, bool allowEmptyCollections = true) : base(kernel, allowEmptyCollections) | ||
{ | ||
} | ||
|
||
public override bool CanResolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, | ||
DependencyModel dependency) | ||
{ | ||
if (kernel.HasComponent(dependency.TargetItemType)) | ||
{ | ||
return false; | ||
} | ||
|
||
return base.CanResolve(context, contextHandlerResolver, model, dependency); | ||
} | ||
|
||
public override object Resolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, | ||
DependencyModel dependency) | ||
{ | ||
Array items = base.Resolve(context, contextHandlerResolver, model, dependency) as Array; | ||
return items; | ||
jonorossi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// 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.MsDependencyInjection | ||
{ | ||
internal class NetCoreRootScope : NetCoreScope | ||
jonorossi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
private NetCoreRootScope() : base(null) | ||
{ | ||
|
||
} | ||
public static NetCoreRootScope BeginRootScope() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should probably throw if |
||
{ | ||
var scope = new NetCoreRootScope(); | ||
NetCoreScope.current.Value = scope; | ||
return scope; | ||
} | ||
|
||
public override NetCoreRootScope RootScope => this; | ||
public override int Nesting => 0; | ||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// 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.MsDependencyInjection | ||
{ | ||
using System; | ||
|
||
using Castle.MicroKernel.Context; | ||
using Castle.MicroKernel.Lifestyle.Scoped; | ||
|
||
internal class NetCoreRootScopeAccessor : IScopeAccessor | ||
{ | ||
public ILifetimeScope GetScope(CreationContext context) | ||
{ | ||
if(NetCoreScope.Current == null) | ||
{ | ||
throw new InvalidOperationException("No scope"); | ||
} | ||
|
||
if(NetCoreRootScope.Current.RootScope == null) | ||
{ | ||
throw new InvalidOperationException("No root scope"); | ||
} | ||
|
||
return NetCoreRootScope.Current.RootScope; | ||
} | ||
|
||
public void Dispose() | ||
jonorossi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This obviously runs the .NET DependencyInjection provided unit tests, so I think the project should probably be named
Castle.Windsor.Extensions.DependencyInjection.Specification.Tests
. This project is also not being compiled or run in the build.And have a separate
Castle.Windsor.Extensions.DependencyInjection.Tests
project using NUnit for extra tests. e.g. support for Options and Logging libraries, and anything else specific to Windsor and for any defects that get found like has already been reported in the pull request but not picked up by this set.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can rename.
As for other unit tests - will add though i don't see reason having tests for Options and Logging - those are already tests by specification tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great. Even if there are no extra unit tests we will at least have somewhere people can add unit tests before fixing any bugs.
Yep, it is definitely unusual, the build hacked around in the DNX days to make it work for the buggy tooling of the day, it probably needs to be refreshed to match what you'd do today if you were starting a clean project.
It looks like it actually is being built now, but the unit tests aren't being run. We are running the unit tests from
buildscripts/build.cmd
:Windsor/buildscripts/build.cmd
Lines 55 to 64 in b12304f