2017-09-06 5 views
0

Autofac은 다음과 같이 Web Form 응용 프로그램에서 구성되었습니다.Web Form 응용 프로그램의 정적 클래스에서 Autofac 유형을 해결하십시오.

var builder = new ContainerBuilder(); 
BootStrapper.InitializeBizTypes(builder); 
builder.RegisterType<Component>().**InstancePerRequest()**; 
_containerProvider = new ContainerProvider(builder.Build()); 

이 페이지에서 ObjectDataSource를 함께 바인딩의 GridView있다, ObjectDataSource를 들어, 경우 SelectMethod는 정적 클래스의 방법이다. 정적 클래스에서

내 코드 : 인터페이스를 해결할 때

private static IComponentContext GetContainer() 
    { 
     var cpa = (IContainerProviderAccessor)HttpContext.Current.ApplicationInstance; 
     return cpa.ContainerProvider.ApplicationContainer; 
    } 

    public static IList<RoleDto> GetRoles() 
    { 
     ***var biz = GetContainer().Resolve<IRoleBiz>();*** 
     return biz.GetRoles(); 
    } 

나는 아래에 오류가 발생했습니다.

'AutofacWebRequest'와 일치하는 태그가있는 범위는 인스턴스가 요청 된 범위에서 볼 수 없습니다. 웹 응용 프로그램의 실행 중에이 표시되는 경우

, 그것은 일반적으로 것을 나타냅니다 당 HTTP 요청이 SingleInstance() 구성 요소 (또는 유사한 시나리오)에 의해 요청 되는대로 등록 된 구성 요소입니다. 웹 통합에서 항상 종속성 분석 프로그램 또는 요청 수명 범위의 종속성을 요청하지만 결코 컨테이너 자체에서 의존성을 요청하지 마십시오.

그래서 내 질문은 평생 범위가 "InstancePerRequest"일 때 asp.net 웹 양식 응용 프로그램 내의 정적 클래스에서 등록 된 유형을 해결하는 방법입니까?

답변

0

요청 범위에서 뭔가를 해결하기 위해, 대신 ApplicatonContainer

public static IList<RoleDto> GetRoles() 
{ 
    var cpa = ((IContainerProviderAccessor)HttpContext.Current.ApplicationInstance); 
    var scope = cpa.RequestLifetime; 

    var biz = scope.Resolve<IRoleBiz>(); 
    return biz.GetRoles(); 
} 
IContainerProviderAccessorRequestLifetime 속성을 사용한다