简体   繁体   中英

visual studio 2017, xamarin, wcf not connecting

I am developing a mobile app using xamarin (android). This app is supposed to access an mssql database using wcf. The wcf successfully connects with the database. The problem is that the app doesnt connect with the wcf. I'm using vs2017. The wcf is currently in IIS. Then I succeeded in adding a web reference to the wcf. Its name is 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. 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

Help!!!!!!

As @Jason stated - don't use localhost.

Android emulator is running on virtual machine. So the localhost of emulator and localhost of you host OS are not same. So when you try to access WCF service on emulator's localhost you are getting error.

To connect to WCF service running on your host OS, you need to inspect emulator's settings and find right IP address to connect. Here explained more detailed.

You also might need to reconfigure WCF service to run on the right network interface.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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