Autofac空服务解析异常

本文关键字:异常 服务 Autofac | 更新日期: 2023-09-27 18:01:42

我有一个3项目在我的解决方案:便携式图书馆(适用于ios, android和winphones),图书馆项目(适用于windows phone)和windows phone测试项目。我的图书馆项目参考了便携式图书馆项目,我的windows phone测试项目参考了便携式和winphone图书馆项目。在我的便携式图书馆项目中,我得到了一些服务接口,并在图书馆winphone项目中实现。在我的便携式项目,我想有一些类,有字段与服务,我在windows phone库实现,并在windows phone测试项目中使用这个类。要做到这一点,我尝试使用Autofac库,在我的windows phone库中注册类型(要做到这一点,我用静态actor制作了类),并尝试在我的便携式库类中解决服务,我在我的windows phone库中注册,但我得到了空异常,我不知道如何修复,并且有可能做我实际尝试做的事情?

我的类来自可移植库项目,在调用Resolve方法后我去异常。

    public IDeviceInformation DeviceInformation { get; }
    public Logger()
    {
        DeviceInformation = AutofacContainer.Instance.Resolve<IDeviceInformation>();
    }

类从库项目

public class AutoFacRegister
{
    static AutoFacRegister()
    {
        var builder = new ContainerBuilder();
        builder.RegisterType<WindowsDeviceInformation>()
            .As<IDeviceInformation>();
        AutofacContainer.Instance = builder.Build();
    }
}

Autofac空服务解析异常

我建议不要使用static构造函数来创建容器。我怀疑构造函数从未被调用,因为AutoFacRegister类没有在任何地方使用,因此AutofacContainer.Instance将始终为null。

我建议使用模块来实现你的目标。