gfm*_*ore 2 .net autofac runtimeexception .net-maui
所以我正在学习 xamarin 表单课程,但我正在对其进行调整以与 .net Maui 一起使用。到目前为止,它是一个基本的应用程序,仅构建了弹出外壳。
作者使用的是 autofac,我以前从未听说过或使用过。我按照示例进行操作并得到运行时异常:
Autofac.Core.Activators.Reflection.NoConstructorsFoundException:“找不到类型‘MauiApp4.Resource+Xml’的可访问构造函数。”
我不知道这意味着什么,也不知道如何继续:叹息
(我认为它试图说它无法访问某些东西?我也怀疑与 IContainer 有关?)
哦它
当我尝试在 Visual Studio 2022 社区版的 Android Pixel 5.0 API 30 Android 模拟器上构建和运行应用程序时,会发生这种情况。
它似乎在 Container = builder.Build(); 行崩溃
Autofac使用nuget加载,版本为6.3.0
我不知道我使用的是哪个版本的 .net 或 maui,但它是最新的(我如何获取此信息?)
这是当前(适用?)代码:(它也在我的 github 存储库上https://github.com/gfmoore/MauiApp4B.App/tree/master)
应用程序.xaml.cs
using System.Reflection;
using Autofac;
using IContainer = Autofac.IContainer;
namespace MauiApp4;
public partial class App : Application
{
public static IContainer Container;
public App()
{
InitializeComponent();
//class used for registration
var builder = new ContainerBuilder();
//scan and register all classes in the assembly
var dataAccess = Assembly.GetExecutingAssembly();
builder.RegisterAssemblyTypes(dataAccess)
.AsImplementedInterfaces()
.AsSelf();
//TODO - register repositories if you use them
//builder.RegisterType<Repository<User>>().As<IRepository<User>>();
//get container
Container = builder.Build();
//set first page
MainPage = new AppShell();
}
}
Run Code Online (Sandbox Code Playgroud)
AppShell.xaml.cs
using Autofac;
namespace MauiApp4;
public partial class AppShell : Shell
{
public AppShell()
{
InitializeComponent();
Application.Current.UserAppTheme = AppTheme.Dark;
BindingContext = App.Container.Resolve<AppShellViewModel>();
}
}
Run Code Online (Sandbox Code Playgroud)
AppShellViewModel.cs
using System;
using System.Windows.Input;
namespace MauiApp4
{
public class AppShellViewModel
{
public ICommand SignOutCommand { get => new Command(async () => await SignOut()); }
private async Task SignOut()
{
await Shell.Current.DisplayAlert("Todo sign out code", "You have been logged out.", "Ok");
}
}
}
Run Code Online (Sandbox Code Playgroud)
非常感谢任何帮助。
G
欢迎来到依赖注入和本机代码的奇妙世界。这就像跳入泳池的最深处,所以你需要对自己有耐心,扭动指关节,并与 Google/Bing/其他任何东西成为最好的朋友。
我不是 MAUI 用户,但我是 Autofac 的所有者之一。Autofac 是一个依赖注入/控制反转框架,这意味着它将找到所有依赖项并将其连接起来。您无需调用new X(y, z)来创建某些内容,而是从控制反转容器中获取内容。容器将找出构造函数的参数并解析它们。
这里有很多 Autofac 文档。很多。DI 可能是一个复杂的主题,涉及计算对象应该生存多长时间(它们是单例还是每次请求时都想要一个新的?)以及如何连接事物。这个文档将是一个好朋友,就像您最喜欢的搜索引擎一样。
将这些放在一起,我在您的代码中看到了这一点:
builder.RegisterAssemblyTypes(dataAccess)
.AsImplementedInterfaces()
.AsSelf();
Run Code Online (Sandbox Code Playgroud)
这是进行程序集扫描,以将该数据访问程序集中的每种类型注册到容器中。通过这样做,您告诉 Autofac 您还打算在该数据访问程序集中每次都逐字解析,因此当您要求它时,它会尝试找到构造函数并开始新的事情。
你的错误说:
Autofac.Core.Activators.Reflection.NoConstructorsFoundException: 'No accessible constructors were found for the type 'MauiApp4.Resource+Xml'.'
...但是您没有提供堆栈跟踪。我不记得 Autofac 在容器构建时对构造函数进行任何扫描,并且该异常来自激活器(意味着它正在尝试解决问题),所以我猜测实际发生的行是这样的:
BindingContext = App.Container.Resolve<AppShellViewModel>();
Run Code Online (Sandbox Code Playgroud)
也就是说,某件事试图得到解决的路线。我不确定这一点,因为它很可能是毛伊岛内部的东西!就像我说的,我不是 MAUI 用户,所以我猜测,而且你没有包含堆栈跟踪,所以我所能做的就是猜测更多。
不过,将所有这些放在一起,似乎有些东西正在尝试解析MauiApp4.Resource+Xml(Xml嵌套在该类中的一个称为该类的类MauiApp4.Resource?)的实例,并且没有公共构造函数,因此 Autofac 无法为您“新建”它。这可能与程序集中的每种类型实际上都已注册这一事实有关。
我强烈建议仅注册您需要的类型,并且仅注册您使用的接口。AsImplementedInterfaces使用和进行大量注册AsSelf很容易,但它也会使故障排除变得更加困难,因为这可能是在某些解析链中发生的。我不能保证改变程序集扫描就能解决这个问题,它只是有可能让它更容易弄清楚。
第一步是弄清楚正在使用什么MauiApp4.Resource+Xml以及为什么要创建该东西。就像,找到一个带有构造函数参数的类。它可能嵌套在解析链深处!它可能是编译器生成的!完整的异常 - 带有内部异常消息和堆栈跟踪 - 可能会帮助您!
// Some class may need a Resource...
public class SomeClassInTheApp
{
public SomeClassInTheApp(Maui.Resource resource) { ... }
}
// And the resource may need the internal class?
public class Resource
{
public Resource(Xml xml) {...}
// I imagine Xml is nested inside Resource?
public Xml
{
// But it might not have a public constructor
internal Xml(){}
}
}
Run Code Online (Sandbox Code Playgroud)
有很多我只能猜测,但希望这能给你一个看的地方。
我会说,一定要阅读整个异常,包括内部异常消息和堆栈跟踪。它们可能很长,并且可能令人困惑,但 Autofac 已尽力尝试在其中提供足够的信息来指出问题所在。缺点是,由于问题可能深藏在堆栈的某个地方,因此异常可能需要一些阅读才能解决。
Autofac 文档中有一个完整的调试和故障排除提示页面,也可能会有所帮助。
抱歉,我无法确切地告诉您要解决什么问题,但希望这能为您解除障碍并让您找到正确的位置。
| 归档时间: |
|
| 查看次数: |
2159 次 |
| 最近记录: |