2012-03-10 51 views
0

我是WCF的新手。我做了一個應用程序,它是如下WCF中的錯誤無法將方法組「getAllEmpName」轉換爲非委託類型「對象」。你打算採用這種方法嗎?

我有服務如下

void IService1.getAllEmpName() 
    { 
     SqlConnection con = new SqlConnection("Data Source=SYSTEM19\\SQLEXPRESS;Initial Catalog=Dora;Integrated Security=True"); 
     SqlCommand cmd = new SqlCommand("Select *from Users", con); 
     SqlDataAdapter da = new SqlDataAdapter(cmd); 
     DataSet ds = new DataSet(); 
     da.Fill(ds); 
    } 

我的界面如下

[ServiceContract] 
public interface IService1 
{ 
    [OperationContract] 
    void getAllEmpName(); 
    [OperationContract] 
    void editEmployee(); 
} 

在我的網頁我做如下

private void get_categories() 
    { 
     ServiceReference1.Service1Client ws = new ServiceReference1.Service1Client(); 
     GridView1.DataSource = ws.getAllEmpName(); 
     GridView1.DataBind(); 
} 

我得到的錯誤爲Cannot convert method group 'getAllEmpName' to non-delegate type 'object'. Did you intend to invoke the method?任何一個幫助都可以

回答

2

我看到的第一個問題是您的getAllEmpName()方法是void。它什麼都不返回。它不會將數據發送回客戶端。

通過WCF傳遞DataSet並不總是最好的主意。單個DataTable會稍微好一些,但返回List<>或數組將是最好的。然而,你可以試試:

// Service 

DataTable IService1.getAllEmpName() 
{ 
    SqlConnection con = new SqlConnection("Data Source=SYSTEM19\\SQLEXPRESS;Initial Catalog=Dora;Integrated Security=True"); 
    SqlCommand cmd = new SqlCommand("Select *from Users", con); 
    SqlDataAdapter da = new SqlDataAdapter(cmd); 
    DataTable dt = new DataTable(); 
    dt.Fill(dt); 
    return dt; 
} 

[ServiceContract] 
public interface IService1 
{ 
    [OperationContract] 
    DataTable getAllEmpName(); 
    [OperationContract] 
    void editEmployee(); 
} 

// Client 

private void get_categories() 
{ 
    ServiceReference1.Service1Client ws = new ServiceReference1.Service1Client(); 
    DataTable data = ws.getAllEmpName(); 
    GridView1.DataSource = data; 
    GridView1.DataBind(); 
} 

我也回來了,重新閱讀,並注意到你是不是你配置WCF客戶端。那是不好的!當WCF客戶端沒有正確中止或關閉時,他們可以繼續使用資源,並保持打開連接,直到它被垃圾收集。關於您可以搜索的主題,還有很多其他討論。

由於ClientBase執行IDisposable,您應該明確地處置它。例如:

using(ServiceReference1.Service1Client ws = new ServiceReference1.Service1Client()) 
{ 
    try 
    { 
     // use the "ws" object... 
    } 
    finally 
    { 
     if(ws.State == CommunicationState.Faulted) 
      ws.Abort(); 
     else 
      ws.Close(); 
    } 
} 
相關問題