WCF 服务正在调试但在从 android 调用时不工作
WCF service working on debug but not working when calling from android
我在 Visual Studio 2012 年创建了一个 WCF 服务,我打算在 Xamarin 中使用它,并将用户注册信息保存到 table 服务器中的 table。在 Visual Studio 中调试时服务工作正常,数据条目实际上保存到 SQL 中我的 table。我想在 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 table。我同时使用模拟器和我的设备进行调试,两者都没有保存任何内容。我已经打开了用于监听的端口,我已经根据 Xamarin 演练使用 silverlight slsvc 工具创建了一个参考文件 http://developer.xamarin.com/guides/cross-platform/application_fundamentals/web_services/walkthrough_working_with_WCF/ 我已经尽我所能。请帮忙。
调用 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 的命名 class 将取决于您的服务定义方式)
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
}
我在 Visual Studio 2012 年创建了一个 WCF 服务,我打算在 Xamarin 中使用它,并将用户注册信息保存到 table 服务器中的 table。在 Visual Studio 中调试时服务工作正常,数据条目实际上保存到 SQL 中我的 table。我想在 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 table。我同时使用模拟器和我的设备进行调试,两者都没有保存任何内容。我已经打开了用于监听的端口,我已经根据 Xamarin 演练使用 silverlight slsvc 工具创建了一个参考文件 http://developer.xamarin.com/guides/cross-platform/application_fundamentals/web_services/walkthrough_working_with_WCF/ 我已经尽我所能。请帮忙。
调用 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 的命名 class 将取决于您的服务定义方式)
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
}