I'm having issues with Autofac's (version 3.0.2) resolution of Funcs. Why is Autofac able to return Funcs for types which it cannot resolve? It seems Autofac is doing the dependency resolution when the func is executed which seems incorrect and should be done when the Func is created (not creating a Foo
type but ensuring its constructor may be called with the known registered types).
using System;
using Autofac;
using NUnit.Framework;
namespace AutofacTest
{
class Program
{
static void Main(string[] args)
{
var builder = new ContainerBuilder();
builder.RegisterType<Foo>().AsSelf().AsImplementedInterfaces();
var container = builder.Build();
//var foo = container.Resolve<IFoo>(); //Throws because the int arg can't be resolved (as it should)
Assert.True(container.IsRegistered<Func<int, IFoo>>()); //This is valid and makes sense
var fooFunc = container.Resolve<Func<int, IFoo>>();
var foo = fooFunc(9);
//Assert.False(container.IsRegistered<Func<string, IFoo>>()); //Why is this true?
var badFooFunc = container.Resolve<Func<string, IFoo>>(); // Why doesn't Autofac throw here?
var badFoo = badFooFunc(string.Empty); // Autofac throws here
}
}
interface IFoo { }
public class Foo : IFoo
{
public string ArgStr { get; set; }
public Foo(int arg)
{
this.ArgStr = arg.ToString();
}
}
}