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

AspNetCore Facility bug fixes #489

Merged
merged 4 commits into from
Sep 17, 2019
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
## Unreleased

Bugfixes:
- ASP.NET Core Facility: FrameworkDependencyResolver must not throw NRE if dependency has no type (eg. depending on a named component)
- ASP.NET Core Facility: Register ViewComponents and TagHelpers correctly
- ASP.NET Core Facility: Allow crosswiring multiple implementations of the same service
- ASP.NET Core Facility: Count TagHelper classes with __Generated__ in the name (eg. TagHelpers generated for ViewComponents) as Framework classes
- Finding the controller should be made in a case insensitive way for Castle.Facilities.AspNet.Mvc facility (@yitzchok, #480)

## 5.0.0 (2019-02-12)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;

namespace Castle.Facilities.AspNetCore.Tests.Fakes
{
public class AuthorisationHandlerOne : IAuthorizationHandler
{
public Task HandleAsync(AuthorizationHandlerContext context)
{
return Task.CompletedTask;
}
}

public class AuthorisationHandlerTwo : IAuthorizationHandler
{
public Task HandleAsync(AuthorizationHandlerContext context)
{
return Task.CompletedTask;
}
}

public class AuthorisationHandlerThree : IAuthorizationHandler
{
public Task HandleAsync(AuthorizationHandlerContext context)
{
return Task.CompletedTask;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ public void TearDown()
testContext.Dispose();
}

[Test]
public void Should_not_match_null()
{
Assert.That(frameworkDependencyResolver.HasMatchingType(null), Is.False);
}

[TestCase(typeof(ServiceProviderOnlyTransient))]
[TestCase(typeof(ServiceProviderOnlyTransientGeneric<OpenOptions>))]
[TestCase(typeof(ServiceProviderOnlyTransientGeneric<ClosedOptions>))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
namespace Castle.Facilities.AspNetCore.Tests
{
using System;
using System.Linq;

using Castle.Core;
using Castle.Facilities.AspNetCore.Tests.Fakes;
Expand Down Expand Up @@ -288,6 +289,22 @@ public void Should_resolve_Composite_Transient_CrossWired_from_ServiceProvider(T
});
}

[Test]
public void Should_resolve_Multiple_Transient_CrossWired_from_ServiceProvider()
{
testContext.WindsorContainer.Register(Types.FromAssemblyContaining<AuthorisationHandlerOne>()
.BasedOn<Microsoft.AspNetCore.Authorization.IAuthorizationHandler>().WithServiceBase()
.LifestyleTransient().Configure(c => c.CrossWired()));

using (var sp = testContext.ServiceCollection.BuildServiceProvider())
{
var services = sp.GetServices<Microsoft.AspNetCore.Authorization.IAuthorizationHandler>();

Assert.That(services, Has.Exactly(3).Items);
Assert.That(services.Select(s => s.GetType()), Is.Unique);
}
}

[TestCase(LifestyleType.Bound)]
[TestCase(LifestyleType.Custom)]
[TestCase(LifestyleType.Pooled)]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright 2004-2018 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.Facilities.AspNetCore.Tests
{
using System;

using Castle.Facilities.AspNetCore.Tests.Framework;
using Castle.MicroKernel.Registration;

using Microsoft.AspNetCore.Mvc;

using NUnit.Framework;

public abstract class WindsorRegistrationOptionsControllerTestCase
{
[SetUp]
public abstract void SetUp();

[TearDown]
public void TearDown()
{
testContext.Dispose();
}

protected Framework.TestContext testContext;

[TestCase(typeof(OverrideController))]
public void Should_resolve_overidden_Controllers_using_WindsorRegistrationOptions(Type optionsResolvableType)
{
Assert.DoesNotThrow(() => { testContext.WindsorContainer.Resolve(optionsResolvableType); });
}

public class OverrideController : Controller
{
}
}

[TestFixture]
public class WindsorRegistrationOptionsForAssembliesControllerTestCase : WindsorRegistrationOptionsControllerTestCase
{
[SetUp]
public override void SetUp()
{
testContext = TestContextFactory.Get(opts => opts
.UseEntryAssembly(typeof(Uri).Assembly)
.RegisterControllers(typeof(OverrideController).Assembly));
}
}

[TestFixture]
public class WindsorRegistrationOptionsForComponentsControllerTestCase : WindsorRegistrationOptionsControllerTestCase
{
[SetUp]
public override void SetUp()
{
testContext = TestContextFactory.Get(opts => opts
.UseEntryAssembly(typeof(Uri).Assembly)
.RegisterControllers(Component.For<OverrideController>().LifestyleScoped().Named("controllers")));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright 2004-2018 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.Facilities.AspNetCore.Tests
{
using System;

using Castle.Facilities.AspNetCore.Tests.Framework;
using Castle.MicroKernel.Registration;

using Microsoft.AspNetCore.Razor.TagHelpers;

using NUnit.Framework;

public abstract class WindsorRegistrationOptionsTagHelperTestCase
{
[SetUp]
public abstract void SetUp();

[TearDown]
public void TearDown()
{
testContext.Dispose();
}

protected Framework.TestContext testContext;

[TestCase(typeof(OverrideTagHelper))]
public void Should_resolve_overidden_TagHelpers_using_WindsorRegistrationOptions(Type optionsResolvableType)
{
Assert.DoesNotThrow(() => { testContext.WindsorContainer.Resolve(optionsResolvableType); });
}

public class OverrideTagHelper : TagHelper
{
}
}

[TestFixture]
public class WindsorRegistrationOptionsForAssembliesTagHelperTestCase : WindsorRegistrationOptionsTagHelperTestCase
{
[SetUp]
public override void SetUp()
{
testContext = TestContextFactory.Get(opts => opts
.UseEntryAssembly(typeof(Uri).Assembly)
.RegisterTagHelpers(typeof(OverrideTagHelper).Assembly));
}
}

[TestFixture]
public class WindsorRegistrationOptionsForComponentsTagHelperTestCase : WindsorRegistrationOptionsTagHelperTestCase
{
[SetUp]
public override void SetUp()
{
testContext = TestContextFactory.Get(opts => opts
.UseEntryAssembly(typeof(Uri).Assembly)
.RegisterTagHelpers(Component.For<OverrideTagHelper>().LifestyleScoped().Named("tag-helpers")));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public class OverrideViewComponent : ViewComponent
}
}

[TestFixture, Order(3)]
[TestFixture]
public class WindsorRegistrationOptionsForAssembliesTestCase : WindsorRegistrationOptionsTestCase
{
[SetUp]
Expand All @@ -68,11 +68,11 @@ public override void SetUp()
.UseEntryAssembly(typeof(WindsorRegistrationOptionsTestCase).Assembly)
.RegisterTagHelpers(typeof(OverrideTagHelper).Assembly)
.RegisterControllers(typeof(OverrideController).Assembly)
.RegisterTagHelpers(typeof(OverrideViewComponent).Assembly));
.RegisterViewComponents(typeof(OverrideViewComponent).Assembly));
}
}

[TestFixture, Order(3)]
[TestFixture]
public class WindsorRegistrationOptionsForComponentsTestCase : WindsorRegistrationOptionsTestCase
{
[SetUp]
Expand All @@ -82,7 +82,7 @@ public override void SetUp()
.UseEntryAssembly(typeof(WindsorRegistrationOptionsTestCase).Assembly)
.RegisterTagHelpers(Component.For<OverrideTagHelper>().LifestyleScoped().Named("tag-helpers"))
.RegisterControllers(Component.For<OverrideController>().LifestyleScoped().Named("controllers"))
.RegisterTagHelpers(Component.For<OverrideViewComponent>().LifestyleScoped().Named("view-components")));
.RegisterViewComponents(Component.For<OverrideViewComponent>().LifestyleScoped().Named("view-components")));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright 2004-2018 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.Facilities.AspNetCore.Tests
{
using System;

using Castle.Facilities.AspNetCore.Tests.Framework;
using Castle.MicroKernel.Registration;

using Microsoft.AspNetCore.Mvc;

using NUnit.Framework;

public abstract class WindsorRegistrationOptionsViewComponentTestCase
{
[SetUp]
public abstract void SetUp();

[TearDown]
public void TearDown()
{
testContext.Dispose();
}

protected Framework.TestContext testContext;

[TestCase(typeof(OverrideViewComponent))]
public void Should_resolve_overidden_ViewComponents_using_WindsorRegistrationOptions(Type optionsResolvableType)
{
Assert.DoesNotThrow(() => { testContext.WindsorContainer.Resolve(optionsResolvableType); });
}

public class OverrideViewComponent : ViewComponent
{
}
}

[TestFixture]
public class WindsorRegistrationOptionsForAssembliesViewComponentTestCase : WindsorRegistrationOptionsViewComponentTestCase
{
[SetUp]
public override void SetUp()
{
testContext = TestContextFactory.Get(opts => opts
.UseEntryAssembly(typeof(Uri).Assembly)
.RegisterViewComponents(typeof(OverrideViewComponent).Assembly));
}
}

[TestFixture]
public class WindsorRegistrationOptionsForComponentsViewComponentTestCase : WindsorRegistrationOptionsViewComponentTestCase
{
[SetUp]
public override void SetUp()
{
testContext = TestContextFactory.Get(opts => opts
.UseEntryAssembly(typeof(Uri).Assembly)
.RegisterViewComponents(Component.For<OverrideViewComponent>().LifestyleScoped().Named("view-components")));
}
}
}
3 changes: 2 additions & 1 deletion src/Castle.Facilities.AspNetCore/AspNetCoreMvcExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public static void AddCustomTagHelperActivation(this IServiceCollection services
if (services == null) throw new ArgumentNullException(nameof(services));
if (activator == null) throw new ArgumentNullException(nameof(activator));

applicationTypeSelector = applicationTypeSelector ?? (type => !type.GetTypeInfo().Namespace.StartsWith("Microsoft"));
applicationTypeSelector = applicationTypeSelector ?? (type => !type.GetTypeInfo().Namespace.StartsWith("Microsoft") &&
!type.GetTypeInfo().Name.Contains("__Generated__"));

services.AddSingleton<ITagHelperActivator>(provider =>
new DelegatingTagHelperActivator(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,25 @@ public void ProcessModel(IKernel kernel, ComponentModel model)
}
}

var key = model.Name;

foreach (var serviceType in model.Services)
{
if (model.LifestyleType == LifestyleType.Transient)
{
services.AddTransient(serviceType, p =>
{
return kernel.Resolve(serviceType);
});
services.AddTransient(serviceType, p => kernel.Resolve(key, serviceType));
}
else if (model.LifestyleType == LifestyleType.Scoped)
{
services.AddScoped(serviceType, p =>
{
kernel.RequireScope();
return kernel.Resolve(serviceType);
return kernel.Resolve(key, serviceType);
});
}
else if (model.LifestyleType == LifestyleType.Singleton)
{
services.AddSingleton(serviceType, p => kernel.Resolve(serviceType));
services.AddSingleton(serviceType, p => kernel.Resolve(key, serviceType));
}
else
{
Expand Down
Loading