Nice programing

IServiceProvider에서 GetRequiredService와 GetService 메서드의 차이점은 무엇입니까?

nicepro 2021. 1. 6. 20:48
반응형

IServiceProvider에서 GetRequiredService와 GetService 메서드의 차이점은 무엇입니까?


의 차이점은 무엇입니까 IServiceProvider.GetRequiredService()와는 IServiceProvider.GetService()?

언제 사용하는 것이 더 좋은 생각 GetRequiredService()입니까?


가능하면 생성자 주입을 사용해야하므로 이러한 메서드를 거의 호출 할 필요가 없습니다.

공장이나 명령 처리기를 동적으로 인스턴스화하는 것과 같은 드문 경우에는 직접 해결할 수 있습니다.

, 서비스가 필요한GetRequiredService 곳에 사용해야 합니다. 서비스가 등록되지 않은 경우 예외가 발생합니다.

GetService다른 쪽은 선택적 종속성을 위한 것으로 null등록 된 서비스가 없을 때만 반환 됩니다.


차이점은 서비스를 찾을 수 없으면 GetService<T>()반환 null된다는 것입니다. 대신 GetRequiredService<T>()던졌습니다 InvalidOperationException.

참조 URL : https://stackoverflow.com/questions/45586897/on-iserviceprovider-what-are-the-differences-between-the-getrequiredservice-and

반응형