我有一个 Android 应用程序,它通过 IIS 7.5 中托管的 WCF 服务连接到数据库。我从这里开始:Walkthrough - Working with WCF .我只使用我的本地主机,我没有配置远程访问。问题是每次我单击按钮(单击时将传递输入数字的值然后处理 sql 语句然后它将返回结果将出现在按钮旁边的 textview 中),出现一个对话框说 系统错误
。我在 Android 模拟器中运行这个应用程序应该没问题,因为我只通过本地主机连接。
这是我的代码:
主 Activity .cs
btnCheck = FindViewById<ImageButton>(Resource.Id.imageButton1);
btnCheck.Click += GetEmployeeDataOnClick;
private void InitializeEmployeeServiceClient()
{
BasicHttpBinding binding = CreateBasicHttp();
_client = new EmployeeServiceClient(binding, EndPoint);
_client.DisplayEmployeeCompleted += ClientOnDisplayEmployeeCompleted;
}
private static BasicHttpBinding CreateBasicHttp()
{
BasicHttpBinding binding = new BasicHttpBinding
{
Name = "basicHttpBinding",
MaxBufferSize = 2147483647,
MaxReceivedMessageSize = 2147483647
};
TimeSpan timeout = new TimeSpan(0, 0, 30);
binding.SendTimeout = timeout;
binding.OpenTimeout = timeout;
binding.ReceiveTimeout = timeout;
return binding;
}
private void GetEmployeeDataOnClick(object sender, EventArgs eventArgs)
{
empNumber = FindViewById<EditText>(Resource.Id.editText1);
EmployeeNo employee = new EmployeeNo();
employee.EmpNo = empNumber.Text;
_client.DisplayEmployeeAsync(employee);
}
private void ClientOnDisplayEmployeeCompleted(object sender, DisplayEmployeeCompletedEventArgs displayEmployeeCompletedEventArgs)
{
empName = FindViewById<TextView>(Resource.Id.textView9);
string msg = null;
if (displayEmployeeCompletedEventArgs.Error != null)
{
msg = displayEmployeeCompletedEventArgs.Error.Message;
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.SetMessage(msg);
alert.SetPositiveButton("OK", (senderAlert, args) => {});
RunOnUiThread(() => {
alert.Show();});
}
else if (displayEmployeeCompletedEventArgs.Cancelled)
{
msg = "Request was cancelled.";
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.SetMessage(msg);
alert.SetPositiveButton("OK", (senderAlert, args) => {});
RunOnUiThread(() => {
alert.Show();
});
}
else
{
msg = displayEmployeeCompletedEventArgs.Result;
RunOnUiThread(() => empName.Text = msg);
}
}
IService.cs (WCF)
[ServiceContract]
public interface IEmployeeService
{
[OperationContract]
string DisplayEmployee(EmployeeNo empNo);
}
[DataContract]
public class EmployeeNo
{
string empNumber = string.Empty;
[DataMember]
public string EmpNo
{
get { return empNumber; }
set { empNumber = value; }
}
}
服务.svc.cs (WCF)
public string DisplayEmployee(EmployeeNo empNo)
{
// sql connection, statement (inputted empNo which corresponds to an EmpName)
return EmpName;
}
我这样做正确吗?这里有什么问题? 顺便说一句,我在这里完全是个新手。
最佳答案
将连接到 IIS 的 IP 地址更改为服务器的实际 IP 地址(在本例中为模拟器的主机)。
您也可以使用像“http://user-pc:8080/DataService”这样的主机名。
关于c# - 使用 WCF (IIS 7.5) 从数据库获取值时出现系统错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36965433/