【问题标题】:WCF service working on debug but not working when calling from androidWCF 服务正在调试,但在从 android 调用时不工作
【发布时间】:2015-07-02 08:10:25
【问题描述】:

我在 Visual Studio 2012 中创建了一个 WCF 服务,我打算在 Xamarin 中使用它,并让它将用户注册信息保存到 SQL Server 中的表中。该服务在 Visual Studio 中调试时工作正常,数据条目实际上保存到我的 SQL 表中。我想在 Xamarin 中使用该服务,这是我的代码:

     using System;
     using System.Collections.Generic;
     using System.Data;
     using System.Data.SqlClient;
     using System.Linq;
     using System.Runtime.Serialization;
     using System.ServiceModel;
     using System.ServiceModel.Web;
     using System.Text;
     using ItusService;


     using Android.App;
     using Android.Locations;
     using Android.OS;
     using Android.Util;
     using Android.Widget;

     //some code here...

     public static readonly EndpointAddress EndPoint = new EndpointAddress("http://192.168.1.155:12100/Service1.svc");

     private Service1Client service;

     protected override void OnCreate (Bundle bundle)
     {
         //some code here...
         InitializeService1Client();
     }
     private void InitializeService1Client()
    {
        BasicHttpBinding binding = new BasicHttpBinding ();
        service = new Service1Client(binding, EndPoint);
    }
    async void AddressButton_OnClick(object sender, EventArgs eventArgs)
    {
        //some code here...
        UserLocation useraddress = new UserLocation ();
        useraddress.ClientID = _client.Text = "ddsdfs";
        useraddress.FullName = _name.Text = "dsffdf";
        useraddress.LocationCoordinates = _locationText.Text;
        useraddress.LocationAddress = _addressText.Text;
        useraddress.DistressTime = _time.Text = "djdsk";
        string result = service.ToString ();
        _resultTxt.Text = result;
    }

我不知道我是否遗漏了什么,因为没有任何内容从我的 Android 设备保存到我的 SQL 表中。我使用模拟器和我的设备进行调试,使用两者都没有保存任何内容。我已经打开了监听端口,我已经根据 Xamarin Walkthrough http://developer.xamarin.com/guides/cross-platform/application_fundamentals/web_services/walkthrough_working_with_WCF/ 使用 silverlight slsvc 工具创建了一个参考文件我已经尽我所能。请帮忙。

【问题讨论】:

  • 您没有向您的服务发送任何内容?我唯一看到的是你初始化服务,在本地创建一个UserLocation,然后你做一个service.ToString?这可能会输出类似[Service1Client] 的内容。你的意思不是像service.MethodOnService(useraddress); 这样的事情吗?现在你没有在你的 WCF 服务上调用任何方法。
  • 字符串值例如useraddress.FullName = _name.Text = "dsffdf";只是测试值。它们不会成为最终项目的一部分。
  • 谢谢@Gerald。在教程中我已经去了虽然我没有遇到service.MethodOnService(useraddress);我会尝试它
  • 注意;这是一个示例MethodOnService 不是一个实际的方法。它是您自己的 WCF 服务端的一种方法,用于保存您的数据。
  • @GeraldVersluis 我看到 service.MethodOnService(); 需要在我的 WCF 服务代码中定义,并且在我的 namespace 中不存在。我确定 WCF 服务很好。当我在 Xamarin 中消费时,我只是错过了一些东西,我只是看不到什么。没有教程有帮助。

标签: android wcf xamarin


【解决方案1】:

调用 WCF 方法的一般模式是这样的(这是一般的 WCF,与 Xamarin 或 Android 无关)

// create the client and bind it
client = new ServiceClient(binding, endpoint);

// specify the callback
client.GetLocationCompleted += OnGetLocationCompleted;

// make the request to your method, passing any required parameters
client.GetLocationAsync(useraddress);

因为WCF调用是异步的,所以需要提供回调方法(EventArgs类的命名取决于你的服务是如何定义的)

public void OnGetLocationCompleted(object sender, GetLocationCompletedEventArgs args) {

  // if something bad happend, args.Error will contain an exception
  // otherwise args.Result will contain any return value from you method

}

【讨论】:

  • 谢谢@Jason。让我现在试试
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多