简体   繁体   中英

“Connection refused” when xamarin.android is trying to contact WCF

i'm pretty new to xamarin and i'm trying to use this techno with a web service .

At the moment I just have a login page that asks my web service if a user can login. To achieve this, my login button uses a portable class that calls the web service .

I hosted the web service on a web server (IIS) . So I use a remote ip to use my webservice

I have been looking for solutions for several hours, the problem remains the same.

If someone can help me it would be nice, remember to tell me if you need more information!

Facts :
-The connection returns the correct result when I use wcftestclient.exe
-The connection returns me the good result when I use UWP
-When I use my android phone in debug, I have an authorization error.
( You can find below the code of each component and error message)

Error Message


PageLogin.xaml.cs

  [XamlCompilation(XamlCompilationOptions.Compile)] public partial class PageLogin : ContentPage { public PageLogin () { InitializeComponent (); this.btnLogin.Clicked += BtnLogin_Clicked; } private void BtnLogin_Clicked(object sender, EventArgs e) { BOWCF.Classes.WebService service = new BOWCF.Classes.WebService(); service.Logged += Service_Logged; service.Login(this.txtId.Text, this.txtPassword.Text); } private void Service_Logged(object sender, BOWCF.Models.User e) { if (e.Enabled) { Classes.Security.CurrentUser = e; Application.Current.MainPage = new Pages.PageMain(); } else { DisplayAlert("TPTFS03", "Connexion Failed", "Ok"); } } } 

BOWCF.Classes.WebService

  public event EventHandler<Models.User> Logged; public void Login(string username, string password) { try { BasicHttpBinding basicHttpBinding = new BasicHttpBinding(); EndpointAddress endpointAddress = new EndpointAddress("http://XXXX.XXXX.XXXX.XXXX:XXXX/ServiceTPTFS03.svc"); SvcTPTFS03.ServiceTPTFS03Client client = new SvcTPTFS03.ServiceTPTFS03Client(); client.LoginCompleted += Client_LoginCompleted; client.LoginAsync(username, password); } catch (Exception ex) { Logged?.Invoke(null, new Models.User()); } } private void Client_LoginCompleted(object sender, SvcTPTFS03.LoginCompletedEventArgs e) { Models.User user = Models.User.Empty; try { if (e.Result != null) { user = new Models.User() { Uid = e.Result.Uid, NameLast = e.Result.NameLast, NameFirst = e.Result.NameFirst, Login = e.Result.Login, Password = e.Result.Password, Enabled = e.Result.Enabled }; Logged?.Invoke(null, user); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } } } 


IServiceTPTFS03.CS

  [ServiceContract] public interface IServiceTPTFS03 { [OperationContract] User Login(string login, string password); } [DataContract] public class User { [DataMember] public Guid Uid { get; set; } = Guid.Empty; [DataMember] public string NameLast { get; set; } = string.Empty; [DataMember] public string NameFirst { get; set; } = string.Empty; [DataMember] public string Login { get; set; } = string.Empty; [DataMember] public string Password { get; set; } = string.Empty; [DataMember] public bool Enabled { get; set; } = false; } 


ServiceTPTFS03.svc.cs

 public class ServiceTPTFS03 : IServiceTPTFS03 { public User Login(string login, string password) { User user = new User(); if (login == "dreau.valerie@swiss-bourdin.com" && password == "1234") { user.Uid = Guid.Parse("AECB05C7-8003-4685-98CD-658761DC7C53"); user.NameLast = "Dreau"; user.NameFirst = "Valerie"; user.Login = "dreau.valerie@swiss-bourdin.com"; user.Password = "1234"; user.Enabled = true; } return user; } } 

Finally I find the solution.
1- You must make sure that the android.permission.INTERNET manifest is active.
2- Do not forget to REBUILD the solution.

In your "BOWCF.Classes.WebService" you must pass the httpbinding and endpointadress.

Try this : new SvcTPTFS03.ServiceTPTFS03Client(basicHttpBinding, endpointAddress); I explain to you, the "basicHttpBinding" it's a variable and same "endpointAddress", if you don't pass the parameter it's not possible for the external network, because the client initialize with the default parameters.

But it's a Good Job.

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