2

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!!!!!!

JosMelka
  • 21
  • 5
  • 1
    are you trying to connect using localhost/loopback, or the FQDN/IP of the server? Have you verified connectivity using the browser on the device? – Jason Jan 23 '18 at 15:28
  • This is what I used http://localhost:2727/Service1.svc and I test the app using vs emulator Thanks – JosMelka Jan 23 '18 at 15:44
  • 1
    don't use localhost, use the IP or FQDN – Jason Jan 23 '18 at 15:51

1 Answers1

0

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.

Gor Rustamyan
  • 902
  • 10
  • 21
  • Thanks guys. I think it is solved because I started receiving different errors. The first was a kind of 'the message with To '' cannot be processed ...' This was solved by adding [ServiceBehavior (AddressFilterMode=AddressFilterMode.Any)] to the Service1.svc.cs file right above public class Service1 : Iservice1. After that I started receiving this error. No action header was found with namespace 'http://www.w3.org/2005/08/addressing' for the given message. Please help again, what's the solution and where to put it. – JosMelka Jan 25 '18 at 06:47