简体   繁体   English

xamarin.android尝试联系WCF时,“连接被拒绝”

[英]“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 . 我对xamarin还是很陌生 ,我正在尝试将此技术与Web服务一起使用

At the moment I just have a login page that asks my web service if a user can login. 目前,我只有一个登录页面 ,询问我的Web服务用户是否可以登录。 To achieve this, my login button uses a portable class that calls the web service . 为此,我的登录按钮使用了可移植类,该类调用了Web服务

I hosted the web service on a web server (IIS) . 我将Web服务托管在Web服务器(IIS)上 So I use a remote ip to use my webservice 所以我使用一个远程IP来使用我的Web服务

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 -当我使用wcftestclient.exe时,连接返回正确的结果
-The connection returns me the good result when I use UWP -当我使用UWP时,连接返回了良好的结果
-When I use my android phone in debug, I have an authorization error. -当我在调试中使用Android手机时,出现授权错误。
( You can find below the code of each component and error message) (您可以在下面找到每个组件的代码和错误消息)

Error Message 错误信息


PageLogin.xaml.cs 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 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 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 服务TPTFS03.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. 1-您必须确保android.permission.INTERNET清单处于活动状态。
2- Do not forget to REBUILD the solution. 2-不要忘记重建解决方案。

In your "BOWCF.Classes.WebService" you must pass the httpbinding and endpointadress. 在“ BOWCF.Classes.WebService”中,您必须传递httpbinding和endpointadress。

Try this : new SvcTPTFS03.ServiceTPTFS03Client(basicHttpBinding, endpointAddress); 试试这个: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. 我向您解释,“ basicHttpBinding”是一个变量,并且是相同的“ endpointAddress”,如果不传递参数,则对于外部网络是不可能的,因为客户端使用默认参数进行了初始化。

But it's a Good Job. 但这是一项好工作。

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

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