【问题标题】:How to catch return value from service method?如何从服务方法中获取返回值?
【发布时间】:2011-05-31 18:49:34
【问题描述】:

我对 silverlight 和 WCF 完全陌生。仍在阅读在线资料并尝试编写一些代码以开始工作。 :)

我的问题是,我想将数据插入数据库并且我的插入方法返回一个布尔值。如何在按钮单击事件中捕获 Silverlight 中的返回值并向用户显示确认消息。

我的服务代码是:

[OperationContract]
    public bool insertData(string Name, string Address, string cType, string postcode, string city, string phone, string email)
    {
        bussAppDataContext dc = new bussAppDataContext();
        TestTable tt = new TestTable();


        tt.CompanyName = Name;
        tt.Address = Address;
        tt.CompanyType = cType;
        tt.Postcode = postcode;
        tt.City = city;
        tt.Telephone = phone;
        tt.Email = email;

        dc.TestTables.InsertOnSubmit(tt);
        dc.SubmitChanges();
        return true;

    }

silverlight 客户端代码是:

private void btnSend_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        FirstServiceReference.FirstServiceClient webServc = new FirstServiceReference.FirstServiceClient();

webServc.insertDataAsync(txtCName.Text.Trim(), txtAddress.Text.Trim(), cmbCType.SelectedValue.ToString(), txtPostcode.Text.Trim(), txtCity.Text.Trim(), txtPhone.Text .Trim(), txtEmail.Text.Trim());

}

【问题讨论】:

    标签: silverlight wcf


    【解决方案1】:

    silverlight 中的所有 Web 服务调用都是异步的,因此您需要向 insertDataCompleted 事件添加处理程序。操作完成时调用此事件。 像这样的:

    webServc.insertDataCompleted += MyHandler;
    webServc.insertDataAsync(txtCName.Text.Trim(), txtAddress.Text.Trim(), cmbCType.SelectedValue.ToString(), txtPostcode.Text.Trim(), txtCity.Text.Trim(), txtPhone.Text.Trim(), txtEmail.Text.Trim());
    
    }
    
    private void MyHandler(object sender, MyEventArgs args) {}
    

    参数的结果是布尔值。 看看这里Calling web services with Silverlight Tim Heuer

    希望这会有所帮助。

    BR,

    TJ

    【讨论】:

    • 我猜你可以通过输入 webServc.insertDataCompleted += 来生成处理程序,然后点击 TAB TAB...
    • 嗨。我的处理程序显示错误。 webServc.insertDataCompleted += new EventHandler(webServc_insertDataCompleted);错误是:错误 1 ​​无法将类型 'System.EventHandler' 隐式转换为 'System.EventHandler' C:\Users\R730\documents\视觉工作室 2010\Projects\BussAppTest\BussAppTest\Views\Home.xaml.cs 55 45 BussAppTest
    • 好的,尝试将处理程序的事件参数更改为 System.ComponentModel.AsyncCompletedEventArgs 类型。然后检查事件参数的真实类型。你让 VisualStudio 生成你完成的处理程序了吗?
    • 您的问题解决了吗?如果你愿意,你可以把你的来源发给我,我会看看。生成存根可能出了点问题...
    • 我的问题还没有解决。我仍然有错误。所以我宁愿使用 System.ComponentModel.AsyncCompletedEventArgs -方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多