简体   繁体   English

Visual Studio 2017,Xamarin,WCF无法连接

[英]visual studio 2017, xamarin, wcf not connecting

I am developing a mobile app using xamarin (android). 我正在使用xamarin(android)开发移动应用程序。 This app is supposed to access an mssql database using wcf. 该应用程序应该使用wcf访问mssql数据库。 The wcf successfully connects with the database. wcf成功连接数据库。 The problem is that the app doesnt connect with the wcf. 问题是该应用程序无法与wcf连接。 I'm using vs2017. 我正在使用vs2017。 The wcf is currently in IIS. WCF当前在IIS中。 Then I succeeded in adding a web reference to the wcf. 然后,我成功地添加了对wcf的网络引用。 Its name is JosGum. 它的名字叫JosGum。

My app code 我的应用程式码

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();
    }
}   

The WCF code is as follows and works exactly on its own. WCF代码如下,并且完全可以独立工作。 But it is not listening to the app. 但这不是在听应用程序。

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();

    }

}

The image shows that the wcf is included as a web reference 该图显示将wcf作为Web参考包括在内

Help!!!!!! 救命!!!!!!

As @Jason stated - don't use localhost. 正如@Jason所说-不要使用localhost。

Android emulator is running on virtual machine. Android模拟器正在虚拟机上运行。 So the localhost of emulator and localhost of you host OS are not same. 因此,模拟器的本地主机和主机OS的本地主机是不同的。 So when you try to access WCF service on emulator's localhost you are getting error. 因此,当您尝试在模拟器的localhost上访问WCF服务时,会收到错误消息。

To connect to WCF service running on your host OS, you need to inspect emulator's settings and find right IP address to connect. 要连接到主机操作系统上运行的WCF服务,您需要检查模拟器的设置并找到正确的IP地址进行连接。 Here explained more detailed. 这里解释比较详细。

You also might need to reconfigure WCF service to run on the right network interface. 您可能还需要重新配置WCF服务以在正确的网络接口上运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM