2016-09-17 2 views
2

저는 한동안 Autofac을 사용해 왔지만 완벽하게 작동했습니다.값은 null 일 수 없습니다. 매개 변수 이름 : 클래스를 해결하려고 할 때의 컨텍스트

최근에 다른 프로젝트에서이 프로젝트를 구현하고자 할 때 나는 왜 그것이 발생했는지 또는 그것이 어디에서 생길 수 있는지 전혀 알 수 없다는 예외가 발생했습니다.

System.ArgumentNullException : ". 값은 null 일 수 없습니다 매개 변수 이름 : 상황"

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() 

App.cs :

public class Test 
{ 
     public static void Main(string[] args) 
     { 
      var scheduledJob = Startup.Container.Resolve<IScheduledJob>(); // Exception on this line 
     } 
} 

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(); 
     } 
    } 

Startup에 구성 요소를 등록했는지 여부에 관계없이 이상한 점은 아무 문제가 없습니다.

또 다른 독특한 점은 내가 해결하려고하는 클래스에 관계없이 오류가 발생한다는 것입니다.

같은 오류는 다음과 같은 코드로 나에게 주어집니다 :

var scheduledJob = Startup.Container.Resolve<Exception>(); 
+1

어디에서'Startup.Run'을 호출합니까? –

답변

2

스택 추적의 Resolve 방법은 IComponentContext 인터페이스의 확장 방법이다. ResolveService 메서드에서 확장 메서드의 대상이 실제로 null이 아닌지 확인하기 위해 검사가 수행됩니다. 귀하의 경우 Container 속성은 null이어야하며 고정 Run 메서드에 대한 호출이 이루어지지 않았기 때문에 초기화되지 않았을 수 있습니다.

나는 이것을 테스트하기위한 몇 가지 빠른 스파이크 코드라고 가정합니다. 그러나 이것을 더 받아 들일 경우 Service Locatoranti-pattern을 권장하면서 컨테이너에 대한 정적 참조를 보유하지 않도록해야합니다.

+0

버머! 나는 그것이 매우 간과 한 것일 것이라고 간단히 간과했다. 정말 고마워! – silkfire

+0

그래서 내가 링크 된 기사를 읽었지만 유감스럽게도 Service Locator 패턴에 대한 대안을 제공하지 않습니다. 이것에 대한 당신의 생각은 무엇입니까? – silkfire

관련 문제