如何使用Xamarin Android访问WCF服务器



我刚刚跟随"演练 - 使用WCF"。但是我无法从服务器中获取任何数据。

我确实做了代理,并将其添加到此处的Android项目

这是我的活动

using Android.App;
using Android.Widget;
using Android.OS;
using System;
using System.ServiceModel;
using HelloWorldServiceProxy;
using System.Runtime.Serialization;
using static Java.Util.Jar.Attributes;
namespace HelloWorld
{
    [Activity(Label = "HelloWorld", MainLauncher = true)]
    public class MainActivity : Activity
    {
        static readonly EndpointAddress Endpoint = new EndpointAddress("http://localhost:8733/Design_Time_Addresses/HelloWorldService/?wsdl");
        HelloWorldServiceClient _client;
        Button _getHelloWorldDataButton;
        TextView _getHelloWorldDataTextView;
        Button _sayHelloWorldButton;
        TextView _sayHelloWorldTextView;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            InitializeHelloWorldServiceClient();
            // This button will invoke the GetHelloWorldData - the method that takes a C# object as a parameter.
            _getHelloWorldDataButton = FindViewById<Button>(Resource.Id.getHelloWorldDataButton);
            _getHelloWorldDataButton.Click += GetHelloWorldDataButtonOnClick;
            _getHelloWorldDataTextView = FindViewById<TextView>(Resource.Id.getHelloWorldDataTextView);
            // This button will invoke SayHelloWorld - this method takes a simple string as a parameter.
            _sayHelloWorldButton = FindViewById<Button>(Resource.Id.sayHelloWorldButton);
            _sayHelloWorldButton.Click += SayHelloWorldButtonOnClick;
            _sayHelloWorldTextView = FindViewById<TextView>(Resource.Id.sayHelloWorldTextView);
        }
        void InitializeHelloWorldServiceClient()
        {
            BasicHttpBinding binding = CreateBasicHttpBinding();
            _client = new HelloWorldServiceClient(binding, Endpoint);
        }
        static BasicHttpBinding CreateBasicHttpBinding()
        {
            BasicHttpBinding binding = new BasicHttpBinding
            {
                Name = "basicHttpBinding",
                MaxBufferSize = 2147483647,
                MaxReceivedMessageSize = 2147483647
            };
            TimeSpan timeout = new TimeSpan(0, 0, 30);
            binding.SendTimeout = timeout;
            binding.OpenTimeout = timeout;
            binding.ReceiveTimeout = timeout;
            return binding;
        }
        async void GetHelloWorldDataButtonOnClick(object sender, EventArgs e)
        {
            var data = new HelloWorldData
            {
                Name = "Mr. Chad",
                SayHello = true
            };
            _getHelloWorldDataTextView.Text = "Waiting for WCF...";
            HelloWorldData result;
            try
            {
                result = await _client.GetHelloDataAsync(data);
                _getHelloWorldDataTextView.Text = result.Name;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        async void SayHelloWorldButtonOnClick(object sender, EventArgs e)
        {
            _sayHelloWorldTextView.Text = "Waiting for WCF...";
            try
            {
                var result = await _client.SayHelloToAsync("Kilroy");
                _sayHelloWorldTextView.Text = result;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

我该怎么做才能从服务器获取数据?我做错了什么...?还是演练有任何错误?

在Xamarin和Andriod上测试WCF的几个问题

  • 如果您在仿真器上
  • 如果您在模拟器上设备 iisexpress 将不允许连接

有很多补救措施,但是我发现的最简单方法是以下方法(不是我不建议将您的防火墙放下(

  • 将防火墙关闭
  • 查找您的WCF ApplicationHost.config文件IE在(.vs config(
    • 或右键单击issexpress -> show all applications,单击服务,然后转到配置路径
    • 找到您的服务并编辑类似于您网站的绑定。

示例更改

<binding protocol="http" bindingInformation="*:43420:localhost" />
<binding protocol="http" bindingInformation="*:43420:127.0.0.1" />
<binding protocol="http" bindingInformation="*:43420:<put your IP address here>" />

NOTE :您必须将43420更改为 WCF 服务 ip 地址,然后将<put your IP address here>更改为您的本地PC IP地址

*对于更永久的解决方案,您需要查找如何配置高级防火墙设置。

此外,在模拟器上访问LocalHost无法工作。在模拟器虚拟机上, localhost 是指代码正在运行的设备(仿真器(。

如果要引用运行 Android Emulator 的计算机,请改用 ip地址 10.0.2.2。您可以从这里阅读更多。

最新更新