InRequestScope로 설정된 바인딩을 사용하여 Ninject를 사용하도록 변환 한 기존 웹 프로젝트가 있습니다.3 단계 Ninject Console App
// binding in AppStart
kernel.Bind<IDbContext>().To<DbContext>().InRequestScope();
...
// business layer
public class BusinessService {
public BusinessService(IDbContext context) {
this.Context = context;
}
private IDbContext Context { get; set; }
public void UpdateUser(int userId) {
User user = this.Context.Users.Single(u => u.UserId == userId);
user.LastUpdated = DateTime.Now;
this.Context.SaveChanges();
}
}
이제 비즈니스 계층의 코드를 사용하는 몇 가지 콘솔 앱을 변환하려고합니다.
// ... Console App ...
foreach(int userId in usersNeedingToBeUpdated) {
// do stuff to each user
BusinessService businessService = kernel.Get<BusinessService>();
businessLayer.UpdateUser(userId);
}
그러나 범위를 이해하는 데 문제가 있습니다. 나는 kernel.Get()을 할 때마다 Ninject가 새로운 IDbContext를주고 이전 IDbContext를 처리하도록하고 싶다. 나는
kernel.Bind<IDbContext>().To<DbContext>().InParentScope();
을하고 노력 그리고 사람이 직접 IDbContext를 요청할 때까지이 작동 : 부모 범위 없기 때문에
kernel.Get<IDbContext>();
가 그럼 난 널 포인터 예외를 얻을. 대신 TransientScope를 사용하면 Ninject는 IDbContext를 처리하지 않습니다.
1. 어떻게하면 모든 커널에서 얻을 수 있을까요? <>() 새로운 서비스와 새로운 의존성을 얻었고 이전 종속성이 삭제 되었습니까?
나는 this post을 읽었지만 아직해야할 일이 확실하지 않습니다.
참조 [여기] (http://stackoverflow.com/questions/18655915/ninject-dbcontext-windows-service-new- 참조 '.InScope()'
와 범위를 소유하고 구현할 수 'InRequestScope'가 어떻게 작동하는지에 대한 설명은 // instance-each-time-thread-runs/18656636 # 18656636을 참조하십시오). – qujck