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

Fixed first-chance HandlerException for optional parameters #450

Merged
merged 2 commits into from
Oct 29, 2018
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Castle Windsor Changelog

## Unreleased

Bugfixes:
- Fixed first-chance HandlerException for optional parameters (@jnm2, #450)

## 5.0.0-beta001 (2018-10-26)

Enhancements:
Expand Down
23 changes: 23 additions & 0 deletions src/Castle.Windsor.Tests/DefaultValueTestCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@

namespace CastleTests
{
using System.Collections.Generic;

using Castle.MicroKernel.Registration;
using Castle.Windsor;

using CastleTests.Components;

Expand Down Expand Up @@ -90,5 +93,25 @@ public void Uses_explicit_value_over_default()

Assert.AreEqual("Adam Mickiewicz", value.Name);
}

#if !NETCOREAPP1_0 // FirstChanceException event was added in .NET Core 2.0
[Test]
public void First_chance_exceptions_are_not_thrown()
{
using (var container = new WindsorContainer())
{
container.Register(Component.For<HasCtorWithOptionalInterfaceParameter>());

TestUtils.AssertNoFirstChanceExceptions(() => container.Resolve<HasCtorWithOptionalInterfaceParameter>());
}
}

private sealed class HasCtorWithOptionalInterfaceParameter
{
public HasCtorWithOptionalInterfaceParameter(IEqualityComparer<int> comparer = null)
{
}
}
#endif
}
}
62 changes: 62 additions & 0 deletions src/Castle.Windsor.Tests/TestUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 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.

#if !NETCOREAPP1_0 // FirstChanceException event was added in .NET Core 2.0
namespace CastleTests
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Text;

using NUnit.Framework;

public static class TestUtils
{
public static void AssertNoFirstChanceExceptions(Action action)
{
var firstChanceExceptions = new List<Exception>();

var handler = new EventHandler<FirstChanceExceptionEventArgs>((sender, e) =>
firstChanceExceptions.Add(e.Exception));

AppDomain.CurrentDomain.FirstChanceException += handler;
try
{
action.Invoke();
}
finally
{
AppDomain.CurrentDomain.FirstChanceException -= handler;
}

if (firstChanceExceptions.Any())
{
var message = new StringBuilder();
for (var i = 0; i < firstChanceExceptions.Count; i++)
{
message.Append("First-chance exception ").Append(i + 1).Append(" of ").Append(firstChanceExceptions.Count).AppendLine(":");
message.AppendLine(firstChanceExceptions[i].ToString());
message.AppendLine();
}

message.Append("Expected: no first-chance exceptions.");

Assert.Fail(message.ToString());
}
}
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -361,12 +361,7 @@ private object ResolveFromKernelByName(CreationContext context, ComponentModel m

private object ResolveFromKernelByType(CreationContext context, ComponentModel model, DependencyModel dependency)
{
IHandler handler;
try
{
handler = TryGetHandlerFromKernel(dependency, context);
}
catch (HandlerException exception)
if (!TryGetHandlerFromKernel(dependency, context, out var handler))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return false case looks almost identical to the handler = null; return true case below it.
There's a potentially a better factoring to indicate which of the two messages should be used?

{
if (dependency.HasDefaultValue)
{
Expand All @@ -377,8 +372,7 @@ private object ResolveFromKernelByType(CreationContext context, ComponentModel m
"Missing dependency.{2}Component {0} has a dependency on {1}, which could not be resolved.{2}Make sure the dependency is correctly registered in the container as a service, or provided as inline argument.",
model.Name,
dependency.TargetItemType,
Environment.NewLine),
exception);
Environment.NewLine));
}

if (handler == null)
Expand Down Expand Up @@ -424,23 +418,37 @@ private object ResolveFromParameter(CreationContext context, ComponentModel mode
}
}

private IHandler TryGetHandlerFromKernel(DependencyModel dependency, CreationContext context)
private bool TryGetHandlerFromKernel(DependencyModel dependency, CreationContext context, out IHandler handler)
{
// we are doing it in two stages because it is likely to be faster to a lookup
// by key than a linear search
var handler = kernel.LoadHandlerByType(dependency.DependencyKey, dependency.TargetItemType, context.AdditionalArguments);
if (handler == null)
try
{
handler = kernel.LoadHandlerByType(dependency.DependencyKey, dependency.TargetItemType, context.AdditionalArguments);
}
catch (HandlerException)
{
throw new HandlerException(string.Format("Handler for {0} was not found.", dependency.TargetItemType), null);
handler = null;
}
if (handler == null) return false;

if (handler.IsBeingResolvedInContext(context) == false)
{
return handler;
return true;
}

// make a best effort to find another one that fit

var handlers = kernel.GetHandlers(dependency.TargetItemType);
IHandler[] handlers;
try
{
handlers = kernel.GetHandlers(dependency.TargetItemType);
}
catch (HandlerException)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make this a valid refactoring, we have to be sure that this method can't throw HandlerException indirectly.
If you know that kernel.GetHandlers would not throw HandlerException, we can remove the try. Same for the try above.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DefaultNamingSubSystem doesn't appear to, however a custom INamingSubSystem or IHandlersFilter may. Sounds like a sensible way to write it.

{
return false;
}

foreach (var maybeCorrectHandler in handlers)
{
if (maybeCorrectHandler.IsBeingResolvedInContext(context) == false)
Expand All @@ -449,7 +457,7 @@ private IHandler TryGetHandlerFromKernel(DependencyModel dependency, CreationCon
break;
}
}
return handler;
return true;
}

private static bool IsHandlerInValidState(IHandler handler)
Expand Down