【问题标题】:WCF functions not viewing in the clientWCF 函数不在客户端中查看
【发布时间】:2014-02-04 10:33:53
【问题描述】:

我在WCF 中创建了函数,并在WCFTestClient.exe 中对其进行了测试。但是在实际客户端(移动设备)上对其进行测试时。它不显示功能,但仅显示以下内容。

示例函数名称为Plus();,它将显示PlusAsync();,并将void 作为返回值。也是一个事件处理程序(我记得是PlusEventHandler 的东西)。

请给我建议。

【问题讨论】:

  • 粘贴您的服务/客户端代码
  • 这对你有用吗???
  • 您是否使用[OperationContract] 注释修饰了所有WCF 方法?

标签: c# wcf asynchronous


【解决方案1】:

您正在获得PlusAsync 操作,因为 WCF 服务以异步方式添加到您的客户端中,您还可以查看下图,它显示了如何异步添加 wcf 服务

但这可能是移动应用程序异步的默认行为,并将回调函数放在该调用中以获取返回值并显示它

调用WCF异步服务示例

    private void MakeAsynchronousCall(int NumberOfStudents)
    {
        BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
        StudentService.StudentServiceClient c =
            new WFCCallExample.StudentService.StudentServiceClient();
        EndpointAddress endpointAddress = c.Endpoint.Address;

        StudentService.IStudentService iStudentService =
            new ChannelFactory<StudentService.IStudentService>
            (basicHttpBinding, endpointAddress).CreateChannel();

        AsyncCallback aSyncCallBack =
            delegate(IAsyncResult result)
        {
            try
            {
                List<StudentService.Student> Students =
                    iStudentService.EndGetStudents(result);

                this.Dispatcher.BeginInvoke((Action)delegate
                { DGStudent.ItemsSource = Students; });
            }
            catch (Exception ex)
            {
                this.Dispatcher.BeginInvoke((Action)delegate
                { MessageBox.Show(ex.Message); });
            }
        };

        try
        {
            iStudentService.BeginGetStudents(NumberOfStudents,
                aSyncCallBack, iStudentService);
        } catch (Exception ex) { MessageBox.Show(ex.Message); }
    }

【讨论】:

  • 感觉移动代理只像 Silverlight 中那样异步
【解决方案2】:

正如How to: Call WCF Service Operations Asynchronously 中解释的那样,首先您创建一个处理结果的回调方法:

// Asynchronous callbacks for displaying results.
static void AddCallback(object sender, AddCompletedEventArgs e)
{
    Console.WriteLine("Add Result: {0}", e.Result);
}

然后您订阅 Completed 事件并发出调用:

client.AddCompleted += new EventHandler<AddCompletedEventArgs>(AddCallback);
client.AddAsync(value1, value2);

只要 WCF 调用返回,您的 AddCallback 就会被调用。

【讨论】:

  • @Pranay 很抱歉,但那确实是一个无用的编辑,所以我把它回滚了。内联代码标签用于内联代码,而不是用于突出显示随机术语。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-27
  • 1970-01-01
  • 2011-11-26
  • 1970-01-01
  • 1970-01-01
  • 2011-07-07
  • 1970-01-01
相关资源
最近更新 更多