2010-08-24 106 views
1

有人可以告訴我爲什麼這不起作用。我創建了一個WCF服務,它返回Northwind數據庫中的客戶列表。如何在Silverlight中調用WCF服務?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.Text; 
using System.ServiceModel.Activation; 

namespace WCFSilverlight.Web 
{ 
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Customers" in code, svc and config file together. 
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 

    public class Customers : ICustomers 
    { 

     IEnumerable<Customer> ICustomers.GetAllCustomers() 
     { 
      NorthwindEntities objNorthwindEntities = new NorthwindEntities(); 
      var query = from cust in objNorthwindEntities.Customers 
         select cust; 
      return query.ToList(); 
     } 
    } 
} 

這是我App.xaml.cs的代碼片段: -

private void Application_Startup(object sender, StartupEventArgs e) 
{ 
    this.RootVisual = new MainPage(); 
    CustomersClient objCustomersClient = new CustomersClient(); 
    objCustomersClient.GetAllCustomersCompleted += new EventHandler<GetAllCustomersCompletedEventArgs>(client_GetNameCompleted); 
    objCustomersClient.GetAllCustomersAsync(); 
} 

void client_GetNameCompleted(object sender, GetAllCustomersCompletedEventArgs e) 
{ 
    MessageBox.Show(e.Result.ToString());  
} 

如果我沒看錯的Silverlight中的方法異步調用。所以我添加了一個事件處理程序來處理它,然後調用該方法來檢索客戶。但是我在Messagebox中沒有任何東西。此外,當我嘗試在client_GetNameCompleted上保留一個斷點時,它從不執行。但如果我保留它在Application_Startup它確實執行。可能是什麼問題?

也給我解釋一下我在做它正確嗎?我見過一個例子,其中一個人使用一些奇怪的符號直接定義函數,如=>

編輯1: -也請解釋我什麼是e.UserState e。它包含什麼,我可以用它做什麼?

編輯2: -: - 我得到這個錯誤http://img178.imageshack.us/img178/9070/53923202.jpg

的WCF服務可以正常使用我已經測試鏈接查詢。因此,Sql Server連接或WCF沒有問題。只有我的客戶有問題。

這是我的ServiceReference.ClientConfig: -

<configuration> 
    <system.serviceModel> 
     <bindings> 
      <basicHttpBinding> 
       <binding name="BasicHttpBinding_ICustomers" maxBufferSize="2147483647" 
        maxReceivedMessageSize="2147483647"> 
        <security mode="None" /> 
       </binding> 
      </basicHttpBinding> 
     </bindings> 
     <client> 
      <endpoint address="http://localhost:50622/Customers.svc" binding="basicHttpBinding" 
       bindingConfiguration="BasicHttpBinding_ICustomers" contract="CustomerServ.ICustomers" 
       name="BasicHttpBinding_ICustomers" /> 
     </client> 
    </system.serviceModel> 
</configuration> 

你現在可以告訴我什麼是錯?

感謝提前:)

更新: -我在谷歌閱讀你需要設置序列化模式爲單向。但我在哪裏設置?我寫什麼在哪裏?

+0

的e.Result應該是IEnumerable的的類型,你可能需要將它轉換回。 – Jirapong 2010-08-24 05:52:41

+0

最後我做到在設計師的延遲加載和它的工作:) – TCM 2010-08-24 10:13:05

回答

2
  1. 你是對的,Silverlight中的所有網絡調用都是異步完成的。
  2. =>語法,你提的是速記用於定義委託方法,其所謂的拉姆達。 (見下文)
  3. 你應該能夠在完成事件處理程序設置斷點,如果不嘗試重新啓動Visual Studio中(我已經看到它之前採取行動strangly)。
  4. e.UserState則要你把在異步調用的UserState變量任何對象(注意額外的過載)的參考。

代碼:

objCustomersClient.GetAllCustomersCompleted = delegate(object Sender, GetAllCustomersCompletedEventArgs e) 
{ 
    MessageBox.Show(e.Result.ToString());   
}; 

// is the same as 

objCustomersClient.GetAllCustomersCompleted += new EventHandler<GetAllCustomersCompletedEventArgs>(client_GetNameCompleted); 
void client_GetNameCompleted(object sender, GetAllCustomersCompletedEventArgs e) 
{ 
    MessageBox.Show(e.Result.ToString());  
} 

// which is same as 

objCustomersClient.GetAllCustomersCompleted += (sender, e) => { MessageBox.Show(e.Result.ToString()); }; 
+0

請閱讀我的編輯職位。 – TCM 2010-08-24 06:27:52

+0

如果您正在使用LINQ到SQL,你將需要設置Seralization模式上通過LINQ到SQL設計的實體。選擇表並查看其屬性。序列化將是其中之一。 – Nate 2010-08-24 13:59:21

相關問題