【问题标题】:Error in WCF Cannot convert method group 'getAllEmpName' to non-delegate type 'object'. Did you intend to invoke the method?WCF 中的错误无法将方法组“getAllEmpName”转换为非委托类型“对象”。您是否打算调用该方法?
【发布时间】:2012-03-27 15:15:28
【问题描述】:

我是 WCF 的新手。我做了一个如下的应用程序

我的服务如下

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? 任何人都可以帮忙

【问题讨论】:

    标签: wcf gridview datatable dataset


    【解决方案1】:

    我看到的第一个问题是您的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();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-10-23
      • 2013-02-18
      • 1970-01-01
      • 2017-06-23
      • 1970-01-01
      • 1970-01-01
      • 2015-12-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多