我在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演练使用silverlight slsvc工具创建了一个参考文件http://developer.xamarin.com/guides/cross-platform/application_fundamentals/web_services/walkthrough_working_with_WCF/我尽我所能尝试了一切。请帮忙。
答案 0 :(得分:0)
调用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
}