我正在練習在同一解決方案中使用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();
}
GetSelectedCompany在哪裏?你如何引用服務(也許你只需要更新Web服務) – V4Vendetta
道歉,它應該讀取GetSelectedCustomer。我編輯了這篇文章以反映這一點。我在Web應用程序項目中添加了對服務的引用,並在SelectedIndexChanged事件的第一行中創建了它的一個實例。它出於某種原因在那裏着色綠色 – dbr
行..你嘗試了我的評論的第二部分,更新服務? – V4Vendetta