2012-03-10 52 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(); 
    } 
} 
相关问题