C# 如何解析接口并使用Autofac在类中使用它

C# 如何解析接口并使用Autofac在类中使用它,c#,autofac,C#,Autofac,我正在尝试解析我在Autofac中注册的接口,但它似乎不起作用。有 nullreferenceexception 类,在其中我注册了inferface: public void RegisterAutofac(HttpConfiguration config) { var builder = new ContainerBuilder(); builder.RegisterApiControllers(Assembly.GetExecutingAs

我正在尝试解析我在Autofac中注册的接口,但它似乎不起作用。有 nullreferenceexception

类,在其中我注册了inferface:

    public void RegisterAutofac(HttpConfiguration config)
    {
        var builder = new ContainerBuilder();

        builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

           (...)
        builder.RegisterType<ApiFileTester>().As<IApiFlTester>().InstancePerRequest();

        var container = builder.Build();
        config.DependencyResolver = new AutofacWebApiDependencyResolver(container);

    }
这里有一个问题,因为我的接口是空的。我试着这样通过:

APIFIESSENDINGCONTROLLER sender=新的APIFIESSENDINGCONTROLLERDTO, 无效

但是它是空的,我通过空值是完全合理的。
是否可以配置可选参数或其他内容?我想让autofac自动解决此接口,而不是手动解决。

您对autofac DI有误解,大多数DI框架都擅长为您创建实例,通过构造函数注入和属性注入,您将自动连接实例及其依赖项。 您的构造函数包含您将在运行时提供的DTO实例,Autofac不会解析该实例,因为您在ConfigureServices循环期间未将其声明给DI容器

在这种情况下,您可能需要放弃使用Autofac,以便在自己的代码中自由创建控制器实例,您将需要从反射中获取具体的类实例。通过这种方法,抽象/实现隔离仍然存在

公共类ApiFileSendingController:ApiClientBase { 专用只读IApiFlTester\u apiFileTester

    public ApiFileSendingController(DTO dto, IApiFlTester tester) : base(dto)
    {
       if (tester is null)
        _tester = GetApiTesterViaReflection(); 
       else
        _tester = tester;
    }

    public ApiFileSendingController(DTO dto) : base(dto)
    {
        _apiFileTester = GetApiTesterViaReflection();
    }


    public void Send(List<AftInvFileDTO> filesToSendRetry = null)
    {
        _apiFileTester.RegisterTestingMethods();
    }

    private IApiFlTester GetApiTesterViaReflection()
    {
        Type type = typeof(IApiFlTester).Assembly.GetTypes().Where(t => t.GetInterfaces().Contains(typeof(IApiFlTester))).FirstOrDefault();
        return Activator.CreateInstance(type) as IApiFlTester;
    }

}

我似乎在解决您的类时没有问题。从技术上讲,不可能真正回答您的问题,因为代码甚至无法编译,而且您似乎缺少大量的autofac注册


如果可选参数不起作用,并且只有DTO作为参数,那么您不能添加其他构造函数,这有什么原因吗?例如:public ApiFileSendingControllerDTO:thisdto,null{}是的,我可以这样添加它,但是我的_apiFileTester仍然为null。你是说在某些情况下,你想调用构造函数,但仍然让Autofac提供第二个依赖项吗?不清楚。如果_apiFileTester永远不应该为null,那么就编写类,使其永远不可以为null。如果参数为null,则引发异常。是的。我想这样做调用构造函数来提供dto,但我不想提供IApiFileTester,因为autofac应该自动解决它。我的问题是,我无法自动解决它-我的_apiFileTester在我尝试像上面介绍的那样编写代码时为空。有没有办法更改代码以调用构造函数并拥有_apiFile包含我在autofac中注册的内容的测试仪?请不要使用中所述的InstancePerRequest。请改用InstancePerLifetimeScope。
DTO dto = new DTO(); //some configuration here

ApiFileSendingController sender = new ApiFileSendingController(dto, null);
sender.Send(); 
    public ApiFileSendingController(DTO dto, IApiFlTester tester) : base(dto)
    {
       if (tester is null)
        _tester = GetApiTesterViaReflection(); 
       else
        _tester = tester;
    }

    public ApiFileSendingController(DTO dto) : base(dto)
    {
        _apiFileTester = GetApiTesterViaReflection();
    }


    public void Send(List<AftInvFileDTO> filesToSendRetry = null)
    {
        _apiFileTester.RegisterTestingMethods();
    }

    private IApiFlTester GetApiTesterViaReflection()
    {
        Type type = typeof(IApiFlTester).Assembly.GetTypes().Where(t => t.GetInterfaces().Contains(typeof(IApiFlTester))).FirstOrDefault();
        return Activator.CreateInstance(type) as IApiFlTester;
    }

}
// @nuget: Autofac
using System;
using Autofac;

public class Program
{
    private static IContainer _container;

    public static void Main()
    {
        RegisterAutofac();
        using (var httpRequestScope = _container.BeginLifetimeScope("AutofacWebRequest"))
        {
            var apiController = httpRequestScope.Resolve<ApiFileSendingController>();
            Console.WriteLine(apiController._apiFileTester);
        }
    }

    public static void RegisterAutofac()
    {
        var builder = new ContainerBuilder();
        //builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
        builder.RegisterType<ApiFileTester>().As<IApiFlTester>().InstancePerLifetimeScope();
        builder.RegisterType<ApiFileSendingController>().AsSelf();
        builder.RegisterType<DTO>().AsSelf();
        _container = builder.Build();
    }

    public class ApiFileSendingController : ApiClientBase
    {
        public readonly IApiFlTester _apiFileTester;
        public ApiFileSendingController(DTO dto, IApiFlTester tester): base (dto)
        {
            _apiFileTester = tester;
        }
    }

    public interface IApiFlTester { }
    public class ApiFileTester : IApiFlTester { }
    public class ApiClientBase 
    {
        public ApiClientBase(DTO dto)
        {
        }
    }
    public class DTO { }
}