簡體   English   中英

如何使用 xamarin android 訪問 WCF 服務器

[英]How can I access WCF server using xamarin android

我只是跟着“演練 - 使用 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 存在幾個問題

  • 如果您在Emulator 上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 您可以從這里閱讀更多內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM