1

我只是跟着“演练 - 使用 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);
            }
        }
    }
}

我应该怎么做才能从服务器获取数据......?我做错了什么...?或者演练有什么问题吗?

4

1 回答 1

2

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

  • 如果您在模拟器上,LocalHost将无法工作
  • 如果您在EmulatorDevice上,IISExpress将不允许连接

有很多补救措施,但是我发现测试这个的最简单方法如下(不是我不建议关闭防火墙)

  • 关闭防火墙
  • 找到您的 WCF applicationhost.config 文件,即位于 (.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>" />

注意:您必须更改43420为您的WCF服务IP 地址并更改<put your IP address here>为您的本地 PC IP 地址

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

此外,LocalHost在您的模拟器上访问将无法正常工作。在Emulator Virtual machine 上LocalHost是指运行代码的设备(模拟器)。

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

于 2018-02-27T01:28:57.170 回答