我使用Autofac已有一段时间了,它已经完美地工作了。
当我最近想在另一个项目中实现它时,我立即遇到一个例外,我不知道为什么会发生它或它可能来自何处。
System.ArgumentNullException:“值不能为空。参数名称:context”
Autofac.ResolutionExtensions.ResolveService(IComponentContext context, Service service, IEnumerable`1 parameters)
Autofac.ResolutionExtensions.Resolve(IComponentContext context, IEnumerable`1 parameters)
Autofac.ResolutionExtensions.Resolve(IComponentContext context)
AssetManagement.App.Test.Main(String[] args) in Test.cs: line: 24
System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
System.Threading.ThreadHelper.ThreadStart_Context(Object state)
System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
System.Threading.ThreadHelper.ThreadStart()
Run Code Online (Sandbox Code Playgroud)
App.cs:
public class Test
{
public static void Main(string[] args)
{
var scheduledJob = Startup.Container.Resolve<IScheduledJob>(); // Exception on this line
}
}
Run Code Online (Sandbox Code Playgroud)
Startup.cs:
internal static class Startup
{
public static IContainer Container { get; private set; }
public static void Run()
{
var builder = new ContainerBuilder();
builder.RegisterType<Test.ScheduledJobTest>().As<IScheduledJob>();
Container = builder.Build();
}
}
Run Code Online (Sandbox Code Playgroud)
奇怪的是,无论是否已注册任何组件,我都会收到此异常Startup。
另一个奇特的事情是,无论我尝试解决哪个类,我都会遇到错误。
以下代码给了我同样的错误:
var scheduledJob = Startup.Container.Resolve<Exception>();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5692 次 |
| 最近记录: |