简体   繁体   中英

Error in WCF Cannot convert method group 'getAllEmpName' to non-delegate type 'object'. Did you intend to invoke the method?

I am new to WCF. I have done an application which is as follows

I am having Service as follows

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);
  }

My interface is as follows

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

In my web page I am doing as follows

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

I am getting error as Cannot convert method group 'getAllEmpName' to non-delegate type 'object'. Did you intend to invoke the method? Cannot convert method group 'getAllEmpName' to non-delegate type 'object'. Did you intend to invoke the method? can any one help

The first problem I see is that your getAllEmpName() method is void . It returns nothing. It will send no data back to the client.

Passing a DataSet through WCF isn't always the best idea either. A single DataTable would be slightly better, but returning a List<> or array would be best. However, try something like:

// 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();
}

I also came back and re-read this, and noticed that you aren't disposing your WCF client. That is bad ! When WCF clients aren't properly aborted or closed they can continue to consume resources, and will hold open the connection until it gets garbage collected. There are plenty of other discussions out there on the topic that you can search for.

Since ClientBase implements IDisposable , you should explicitly dispose of it. Something like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM