2014-07-21 1 views
1

: Microsoft.Data.Services.Client.Portable 을 윈도우 8은 VS2013액세스하는 동안 오류가 WCF 서비스는 내가 사용하여 표준 중 하나로, OData 클라이언트 건물입니다

이 난에 서비스 참조를 추가 한 프로젝트 (TMALiveData) 권한 부여. 이제 데이터를 검색하고 싶습니다. 다음 코드를 사용하고 있지만, 할 때, 최종 루프에서 널 포인터 참조를 얻습니다.

코드는 다음과 같습니다 내가 잘못이 분명 아무것도

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Data.Services.Client; 

namespace testLSconWithMSv2 
{ 
    public class testLSCon 
    { 
     static string mResult; 

     public static string result { get { return mResult; } } 

     public static void testREADLiveConnection() 
     { 

      Uri tmaLiveDataRoot = new Uri("https://xxx.azurewebsites.net/xxx.svc/"); 
      TMLiveData.TMALiveData mLiveData = new TMLiveData.TMALiveData(tmaLiveDataRoot); 
      mResult = null; 


      DataServiceQuery<TMLiveData.JobType> query = (DataServiceQuery<TMLiveData.JobType>)mLiveData.JobTypes.Where(c => c.IsActive == true); 
      mResult = "Trying to READ the data"; 
      try 
      { 
       query.BeginExecute(OnQueryComplete, query); 
      } 
      catch (Exception ex) 
      { 
       mResult = "Error on beginExecute: " + ex.Message; 
      } 


     } 

     private static void OnQueryComplete(IAsyncResult result) 
     { 

      DataServiceQuery<TMLiveData.JobType> query = result as DataServiceQuery<TMLiveData.JobType>; 
      mResult = "Done!"; 
      try 
      { 
       foreach (TMLiveData.JobType jobType in query.EndExecute(result)) 
       { 
        mResult += jobType.JobType1 + ","; 
       } 
      }catch (Exception ex) 
      { 
       mResult = "Error looping for items: " + ex.Message; 
      } 



     } 

    } 
} 

있습니까? 내가에서 MS의 예에 대한 접근 방식을 기반으로 한 : 당신이 DataServiceQuery<TMLiveData.JobType>IAsyncResult 캐스팅하려고 노력하고 있기 때문에 How to Execute Async...

+0

null 인 객체는 무엇입니까? –

답변

1

당신은 NullReferenceException을 받고 있습니다. 대신 IAsyncResult.AsyncState 캐스팅해야합니다 보조 노트에

private static void OnQueryComplete(IAsyncResult result) 
{ 
     DataServiceQuery<TMLiveData.JobType> query = (DataServiceQuery<TMLiveData.JobType>) result.AsyncState; 

     mResult = "Done!"; 
     try 
     { 
      foreach (TMLiveData.JobType jobType in query.EndExecute(result)) 
      { 
       mResult += jobType.JobType1 + ","; 
      } 
     } 
     catch (Exception ex) 
     { 
      mResult = "Error looping for items: " + ex.Message; 
     } 
} 

, 나는 as 연산자 대신 여기에 명시 적 캐스트를 사용했다. 그렇게했다면 NullReferenceException 대신 InvalidCastException이 표시되어 오류를 쉽게 발견 할 수 있습니다.

+0

네, 항상 캐스팅 시도를 사용하여 Null 체크를 수행하는 것이 좋습니다. null tben 캐스트를 직접 내 보내지 않으면 ... –

+0

"null이 예상됩니다"IMO가 아닙니다. 그것은 당신이 일할 것으로 예상되는 유형의 100 % 확신에 관한 것입니다. 만약 그럴 수 있다면,'as '를 사용하십시오. –

+0

확인 - 지금 테스트 중입니다. 어떻게 진행되는지 알려 드리겠습니다. 감사. 도움을 주신 덕분에 – WaterBoy