2011-02-02 60 views
1

我正在使用ASP.NET/C#中的ASMX服務。我的服務返回了我的WebMethods的的一些的適當數據,但不是全部。有趣的部分是所有的WebMethods都非常相似。ASMX服務並不總是返回數據

這裏有一個總是返回數據:

[WebMethod] 
public AccountItem[] GetAllAccounts() 
{ 
    AccountItem[] AccountItems = HttpContext.Current.Cache[AccountItemsCacheKey] as AccountItem[]; 
    if (AccountItems == null) 
    { 
     List<AccountItem> items = new List<AccountItem>(); 
     using (SqlManager sql = new SqlManager(SqlManager.GetSqlDbiConnectionString())) 
     { 
      using (SqlDataReader reader = sql.ExecuteReader("SELECT A.Account_Id, A.Customer_Id, C.Last_Name + ', ' + C.First_Name AS CustomerName, A.[Status], AT.Name AS AcctType, A.Employee_Id, A.Initial_Balance, A.Interest_Rate, '$'+CONVERT(varchar(50), A.Balance, 1) AS Balance FROM Account A JOIN Account_Type AT ON A.Account_Type_Id=AT.Account_Type_Id JOIN Customer C ON A.Customer_Id=C.Customer_Id WHERE [Status]=1")) 
      { 
       while (reader.Read()) 
       { 
        AccountItem item = new AccountItem(); 
        item.AccountId = (int)reader["Account_Id"]; 
        item.CustomerId = (int)reader["Customer_Id"]; 
        item.CustomerName = (string)reader["CustomerName"]; 
        item.AccountStatus = (bool)reader["Status"]; 
        item.AccountType = (string)reader["AcctType"]; 
        item.InitialBalance = (decimal)reader["Initial_Balance"]; 
        item.InterestRate = (decimal)reader["Interest_Rate"]; 
        item.Balance = (string)reader["Balance"]; 

        items.Add(item); 
       } 
       reader.Close(); 
      } 
     } 
     HttpContext.Current.Cache.Add(AccountItemsCacheKey, items.ToArray(), null, DateTime.Now.AddMinutes(CacheDuration), Cache.NoSlidingExpiration, CacheItemPriority.Default, null); 
     return items.ToArray(); 
    } 
    else 
    { 
     return AccountItems; 
    } 
} 

而且這裏有一個從不返回數據:

[WebMethod] 
public TransactionItem[] GetAllTransactions() 
{ 
    TransactionItem[] tranItems = HttpContext.Current.Cache[TransactionItemsCacheKey] as TransactionItem[]; 

    if (tranItems == null) 
    { 
     List<TransactionItem> items = new List<TransactionItem>(); 
     using (SqlManager sql = new SqlManager(SqlManager.GetSqlDbiConnectionString())) 
     { 
      using (SqlDataReader reader = sql.ExecuteReader("SELECT [Transaction_Id],[Account_Id],[Amount],[DateTime],[Comment],TT.[Name] AS [TransType],[Flagged],[Employee_Id],[Status] FROM [Transaction] T JOIN [Trans_Type] TT ON T.Trans_Type_Id=TT.Trans_Type_Id")) 
      { 
       while (reader.Read()) 
       { 
        TransactionItem item = new TransactionItem(); 
        item.TransactionId = (int)reader["Transaction_Id"]; 
        item.AccountId = (int)reader["Account_Id"]; 
        item.Amount = (decimal)reader["Amount"]; 
        item.Timestamp = (DateTime)reader["DateTime"]; 
        item.Comment = (string)reader["Comment"]; 
        item.TransType = (string)reader["TransType"]; 
        item.Flagged = (bool)reader["Flagged"]; 
        item.EmployeeId = (int)reader["Employee_Id"]; 
        item.Status = (bool)reader["Status"]; 

        items.Add(item); 
       } 
       reader.Close(); 
      } 
     } 
     HttpContext.Current.Cache.Add(TransactionItemsCacheKey, items.ToArray(), null, DateTime.Now.AddMinutes(CacheDuration), Cache.NoSlidingExpiration, CacheItemPriority.Default, null); 
     return items.ToArray(); 
    } 
    else 
    { 
     return tranItems; 
    } 
} 

正如你可以看到,他們幾乎是相同的。這兩個SQL查詢都會返回大量記錄,但只有GetAllAccounts() WebMethod實際上將該數據返回。

這是我怎麼顯示通過從GetAllAccounts()回數據,工作正常:

@{ 
    Layout = "~/Shared/_Layout.cshtml"; 
    Page.Title = "Accounts"; 
    Page.Header = "BankSite Mobile - Accounts"; 
    var svc = IntranetService.GetAllAccounts(); 
} 
<div data-role="content"> 
     <ul data-role="listview" data-inset="true" data-theme="c"> 
      @foreach(var item in svc){ 
       <li> 
        <h3><a href="[email protected]">Account #@item.AccountId.ToString() (@item.AccountType)</a></h3> 
        <p>Customer: @item.CustomerName</p> 
        <p>Account Balance: @item.Balance</p> 
       </li> 
      } 
     </ul> 
    </div> 

然而,這並不正常工作,但它的幾乎是完全相同的代碼:

@{ 
    Layout = "~/Shared/_Layout.cshtml"; 
    Page.Title = "Customers"; 
    Page.Header = "BankSite Mobile - Customers"; 
    var svc = IntranetService.GetAllCustomers(); 
} 
<div data-role="content"> 
     <ul data-role="listview" data-inset="true" data-theme="c"> 
      @foreach(var item in svc){ 
       <li> 
        <h3><a href="[email protected]">Account #@item.CustomerId.ToString() (@item.CustomerId)</a></h3> 
        <p>Customer: @item.CustomerId</p> 
        <p>Account Balance: @item.CustomerId</p> 
       </li> 
      } 
     </ul> 
    </div> 

...所以基本上我很困惑。我不明白爲什麼數據沒有按照預期從非工作的WebMethod(GetAllCustomers())返回。我錯過了什麼?

+0

如果你設置斷點通過GetAllTransactions()所有的方式,TransactionItem []填充? – 2011-02-03 03:35:26

回答

0

我發現問題是由讀者檢索到的某些字段爲空,並且您無法創建空字符串。該解決方案基本上是使用像這樣爲每個項目屬性:

item.Amount = (reader["Amount"] != DBNull.value) ? (decimal)reader["Amount"] : 0; 
1

如果你禁用從緩存中加載東西,這兩種方法總會成功返回預期的結果集?我會首先嚐試一下,我的直覺是對緩存有些質疑(即在方法返回之前會過期)。然後從那裏出發。

+0

否定。我禁用了緩存,它仍然返回相同的空集。緩存設置爲60分鐘: public static int CacheDuration { get {return 60; } } – 2011-02-02 16:41:18

0

嘗試通過直接在Web瀏覽器中訪問Web服務來將問題隔離到Web服務。另外,如果可能,請使用SQL Server Profiler確保Web方法正在查詢數據庫。

如果不查詢數據庫,那麼我猜測它已經緩存了一個空數組。

因此,inital「if(tranItems == null)」檢查返回false,但它然後返回一個空數組作爲結果。