Skip to content

Commit

Permalink
Merge pull request autofac#785 from NickRusinov/fix-728
Browse files Browse the repository at this point in the history
Optional string parameter causing resolution exception when AnyConcreteTypeNotAlreadyRegisteredSource added
  • Loading branch information
tillig authored Sep 13, 2016
2 parents 7a3b97a + 31439a1 commit 9ab6ba4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public IEnumerable<IComponentRegistration> RegistrationsFor(

var ts = service as TypedService;
if (ts == null ||
ts.ServiceType == typeof(string) ||
!ts.ServiceType.GetTypeInfo().IsClass ||
ts.ServiceType.GetTypeInfo().IsSubclassOf(typeof(Delegate)) ||
ts.ServiceType.GetTypeInfo().IsAbstract ||
Expand Down
37 changes: 37 additions & 0 deletions test/Autofac.Test/Features/ResolveAnything/ResolveAnythingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,43 @@ public void LifetimeCanBeSingleInstance()
}
}

public class RegiserTypeWithCtorParam
{
public RegiserTypeWithCtorParam(string stringParam = "MyString")
{
StringParam = stringParam;
}

public string StringParam { get; }
}

[Fact]
public void AllConcreteTypesSourceResolvesOptionalParams()
{
var cb = new ContainerBuilder();
cb.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource());
var container = cb.Build();

var resolved = container.Resolve<RegiserTypeWithCtorParam>();

Assert.Equal("MyString", resolved.StringParam);
}

[Fact]
public void AllConcreteTypesSourceAlreadyRegisteredResolvesOptionalParams()
{
var cb = new ContainerBuilder();

// Concrete type is already registered, but still errors
cb.RegisterType<RegiserTypeWithCtorParam>();
cb.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource());
var container = cb.Build();

var resolved = container.Resolve<RegiserTypeWithCtorParam>();

Assert.Equal("MyString", resolved.StringParam);
}

private static IContainer CreateResolveAnythingContainer()
{
var cb = new ContainerBuilder();
Expand Down

0 comments on commit 9ab6ba4

Please sign in to comment.