无法注册服务

问题描述 投票:1回答:1

我正在尝试使用Prism autofac注册服务。但是,我似乎无法找到一种方法来使这项工作。我在互联网上找到的所有东西都是旧版本的棱镜,并没有真正告诉我现在如何使用它。

protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
    containerRegistry.RegisterForNavigation<NavigationPage>();
    containerRegistry.RegisterForNavigation<CashRegister>();
    containerRegistry.RegisterForNavigation<HamburgerMenu>("Index");
    containerRegistry.RegisterForNavigation<Inventory>();
    containerRegistry.RegisterForNavigation<Navigation>();
    containerRegistry.RegisterForNavigation<Start>();
    containerRegistry.RegisterForNavigation<InventoryItemDetail>();
    containerRegistry.RegisterForNavigation<FolderCreate>();
    //containerRegistry.Register<SQLiteService>().As<ISQLiteService>();
    var builder = new ContainerBuilder();
    builder.RegisterType<SQLiteService>().As<ISQLiteService>();
    builder.Build();
}

正如你所看到的那样,我尝试使用注册容器本身,但由于它无法识别.As方法,因此无法正常工作。尝试使用新的构建器也给了我错误。

public class StartViewModel : BindableBase
{
    public StartViewModel(ISQLiteService sqliteService)
    {

    }
}

在这里,我尝试使用注册类型。

谁能告诉我我做错了什么?

我发现你可以使用GetBuilder()方法使用build in builder但仍然出错。

error autofac

c# .net xamarin.forms prism autofac
1个回答
0
投票

堆栈跟踪告诉我们,在NullReferenceException的构造函数中抛出了SQLiteService。在没有看到你的代码的情况下,我猜想有一些必要的构造函数参数,但Autofac正在传递null

请参阅the Autofac documentation,了解如何将参数传递给已注册的依赖项。假设我们必须传递数据库文件的名称,我们按如下方式注册SQLiteService

containerRegistry.RegisterType<SQLiteService>()
    .As<ISQLiteService>()
    .WithParameter("databaseFile", "database.sqlite");
© www.soinside.com 2019 - 2024. All rights reserved.