[英]visual studio 2017, xamarin, wcf not connecting
我正在使用xamarin(android)開發移動應用程序。 該應用程序應該使用wcf訪問mssql數據庫。 wcf成功連接數據庫。 問題是該應用程序無法與wcf連接。 我正在使用vs2017。 WCF當前在IIS中。 然后,我成功地添加了對wcf的網絡引用。 它的名字叫JosGum。
我的應用程式碼
public class ACategory : Activity
{
Button btnTest;
TextView textView1;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.ACategory);
btnTest = FindViewById<Button>(Resource.Id.btnTest);
textView1 = FindViewById<TextView>(Resource.Id.textView1);
btnTest.Text = "Back";
string temp = Intent.GetStringExtra("ButtonClicked") ?? "Item not available";
textView1.Text = "You are viewing the " + temp + " Category. Choises in here will be populated when connected to database";
this.Title = Intent.GetStringExtra("ButtonClicked") ?? "Motor not available";
ListView list = (ListView)FindViewById(Resource.Id.list);
JosGum.Service1 myService = new JosGum.Service1();
myService.AddTBFCompleted += MyService_AddTBFCompleted;
myService.AddTBFAsync(temp);
btnTest.Click += (o, e) =>
{
SetContentView(Resource.Layout.Main);
StartActivity(typeof(MainActivity));
};
}
private void MyService_AddTBFCompleted(object sender, JosGum.AddTBFCompletedEventArgs e)
{
//throw new NotImplementedException();
textView1.Text = e.Error.ToString();
}
}
WCF代碼如下,並且完全可以獨立工作。 但這不是在聽應用程序。
public class Service1 : IService1
{
private string conStr = "server =.; User Id = sa; Password=; Database=Test; trusted_connection=yes";
public List<ToBeFilled> GetToBeFilled()
{
List<ToBeFilled> tBFList = new List<ToBeFilled>();
SqlConnection connection = new SqlConnection(this.conStr);
connection.Open();
SqlCommand cmd = new SqlCommand("select TBF from TestTable", connection);
cmd.CommandType = CommandType.Text;
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
ToBeFilled tBF = new ToBeFilled();
tBF.TBF = sdr["TBF"].ToString();
tBFList.Add(tBF);
}
return tBFList.ToList();
}
public string AddTBF(string tBF)
{
int status = 0;
SqlConnection connection = new SqlConnection(this.conStr);
SqlCommand cmd = new SqlCommand();
try
{
if (connection.State==ConnectionState.Closed)
{
connection.Open();
}
cmd = new SqlCommand("Insert into TestTable (Id,TBF) values (@Id, @TBF)", connection);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Id", tBF);
cmd.Parameters.AddWithValue("@TBF", tBF);
cmd.ExecuteReader();
status = 1;
}
catch(Exception ex)
{
throw ex;
}
finally
{
connection.Close();
cmd.Dispose();
}
return status.ToString();
}
}
救命!!!!!!
正如@Jason所說-不要使用localhost。
Android模擬器正在虛擬機上運行。 因此,模擬器的本地主機和主機OS的本地主機是不同的。 因此,當您嘗試在模擬器的localhost上訪問WCF服務時,會收到錯誤消息。
要連接到主機操作系統上運行的WCF服務,您需要檢查模擬器的設置並找到正確的IP地址進行連接。 這里解釋比較詳細。
您可能還需要重新配置WCF服務以在正確的網絡接口上運行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.