내 실버 라이트 애플리케이션에서 GetListCallBack
메서드를 비동기 서비스 호출의 완료 이벤트에 이벤트 핸들러로 첨부하는 Repository 클래스의 GetEmployees
대리자 매개 변수에 전달하는 중입니다.이벤트 핸들러가 구독 취소되지 않음
EmpViewModel 등급 :
public class EmpViewModel
{
private IRepository EMPRepository = null;
//constructor
public EmpViewModel
{
this.EMPRepository= new Repository();
}
public void GetList()
{
this.EMPRepository.GetEmployees(xyz, this.GetListCallBack);
}
public void GetAnotherList()
{
this.EMPRepository.GetEmployees(pqr, this.GetAnotherListCallBack);
}
private void GetListCallBack(object sender, GetListCompletedEventArgs args)
{
if (args.Error == null)
{
this.collection1.Clear();
this.collection1 = args.Result;
}
else
{
//do sth
}
}
public void GetAnotherListCallback(object sender, GetListCompletedEventArgs args)
{
//do sth with collection1
}
}
저장소 클래스 : 이제
public class Repository : IRepository
{
private readonly ServiceClient _client=null ;
public Repository()
{
_client = new ServiceClient(Binding, Endpoint);
}
public void GetEmployees(int xyz, EventHandler<GetListCompletedEventArgs> eventHandler)
{
_client.GetListCompleted -= eventHandler;
_client.GetListCompleted += new EventHandler<GetListCompletedEventArgs>(eventHandler);
_client.GetListAsync(xyz);
}
}
때 나는이 같은 클래스 EmpViewModel
에 다른 방법 GetAnotherList()
를 호출하는 경우, 다음 완성 된 GetList()
메소드를 호출 메서드가 호출되기 전에 GetAnotherListCallBack
메서드가 다시 호출됩니다.
두 방법 모두 이벤트에 가입하면 이러한 현상이 발생할 수 있습니다.
보시다시피, 나는 이벤트 핸들러를 콜백 이벤트에서 명시 적으로 구독 취소했으나 여전히 이벤트 핸들러가 호출 중입니다. 누구나 내가 잘못 될 수있는 곳을 제안 할 수 있습니까?
편집 :
내가 대신
을 발사됩니다이Repository
클래스의 다른 인스턴스 만 attched 콜백 메소드에 전달되는 두 콜백 방법으로 잘 작동
Repository
메소드를 호출
this.EMPRepository
를 사용하는 로컬 변수를 사용하는 경우
public class EmpViewModel
{
public void GetList()
{
EMPRepository = new Repository();
EMPRepository.GetEmployees(xyz, this.GetListCallBack);
}
public void GetAnotherList()
{
EMPRepository = new Repository();
EMPRepository.GetEmployees(pqr, this.GetAnotherListCallBack);
}
--------
'ServiceClient' 클래스의 코드를 바꿀 수 있습니까? 그것은 고쳐 져야하는 것입니다. –
@ MD.Unicorn ServiceClient 클래스는 서비스 참조가 추가 될 때 VS에 의해 자동 생성됩니다. – Raj