2011-12-12 84 views
0

我正在練習在同一解決方案中使用WCF和兩個項目。該服務應該從northwnd db獲取信息,並且客戶端顯示它。WCF服務 - notimplementedexception

一切工作正常,直到我添加一個新的方法存根到我的接口/合同,GetSelectedCustomer(string companyName)

我已經實現了接口(使用智能標記是肯定的)。一切都編譯好。但是,當從客戶端的代碼隱藏中調用該方法時,會拋出NotImplementedException

我發現的唯一看起來很奇怪的是,在intellisense中,GetSelectedCustomer的圖標是內部方法的圖標,而其他圖標是通常的公共方法圖標。我不確定這是爲什麼。

我的界面:

[ServiceContract(Namespace = "http://localhost/NorthWndSrv/")] 
public interface INorthWndSrv 
{ 
    [OperationContract] 
    Customer GetSingleCustomer(string custID); 

    [OperationContract] 
    Customer GetSelectedCustomer(string companyName); 

    [OperationContract] 
    List<Customer> GetAllCustomers(); 

    [OperationContract] 
    List<string> GetCustomerIDs(); 
} 

[DataContract] 
public partial class Customer 
{ 
    [DataMember] 
    public string Company { get; set; } 
    [DataMember] 
    public string Contact { get; set; } 
    [DataMember] 
    public string CityName { get; set; } 
    [DataMember] 
    public string CountryName { get; set; } 
} 

實施:

public class NorthWndSrv: INorthWndSrv 
{ 
    public Customer GetSingleCustomer(string custID) 
    { 
     //stuff that works 
    } 

    public List<Customer> GetAllCustomers() 
    { 
     //stuff that works 
    } 

    public List<string> GetCustomerIDs() 
    { 
     //stuff that works 
    } 

    public Customer GetSelectedCustomer(string companyName) 
    { 
     using (northwndEntities db = new northwndEntities()) 
     { 
      Customer c = (from cust in db.CustomerEntities 
          where cust.CompanyName == companyName 
          select new Customer 
          { 
           Company = cust.CompanyName, 
           Contact = cust.ContactName, 
           CityName = cust.City, 
           CountryName = cust.Country 
          }).FirstOrDefault(); 
      return c; 
     } 
    } 
} 

GridView控件事件在頁面的後臺代碼:

protected void GridViewCustomers_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    NorthWndServiceReference.NorthWndSrvClient Service = new NorthWndServiceReference.NorthWndSrvClient(); 
    string companyName = GridViewCustomers.SelectedRow.Cells[2].Text; //company name 
    NorthWndServiceReference.Customer cust = Service.GetSelectedCustomer(companyName); 
    ArrayList custList = new ArrayList(); 
    custList.Add(cust); 
    DetailsViewCustomers.DataSource = custList; 
    DetailsViewCustomers.DataBind();  
} 
+5

GetSelectedCompany在哪裏?你如何引用服務(也許你只需要更新Web服務) – V4Vendetta

+0

道歉,它應該讀取GetSelectedCustomer。我編輯了這篇文章以反映這一點。我在Web應用程序項目中添加了對服務的引用,並在SelectedIndexChanged事件的第一行中創建了它的一個實例。它出於某種原因在那裏着色綠色 – dbr

+0

行..你嘗試了我的評論的第二部分,更新服務? – V4Vendetta

回答

5

這聽起來像您的服務調用一個出實現程序集的最新版本。嘗試清理項目,然後重建。

特別是,如果您使用的是依賴注入容器,並且在您的項目中沒有明確的引用到實現GetSelectedCompany的程序集,那麼您可能不會看到程序集的更新版本。要麼添加一個明確的引用,要麼創建其他方式(例如後期構建任務)將程序集複製到正確的位置。

+0

再次重建,清理,然後重新添加服務參考。現在工作正常。謝謝! – dbr