【发布时间】: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 中消费时,我只是错过了一些东西,我只是看不到什么。没有教程有帮助。