1

I just followed "Walkthrough - Working with WCF". But I can't get any data from server.

I did make Proxy and add it to Android project here

Here is my Activity

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);
            }
        }
    }
}

What should I do to get data from Server...? What Did I do wrong...? Or Is there any wrong in Walkthrough?

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
Jungeun
  • 9
  • 3
  • "I can't get any data from server." - What does it mean? Did you get an exception? What is retuned from await _client.SayHelloToAsync("Kilroy")? – EvZ Feb 27 '18 at 00:52
  • are you testing this on the emulator or device? – TheGeneral Feb 27 '18 at 00:54
  • @EvZ Yes! I got Exception! System.ServiceModel.EndpointNotFoundException: System error./System.EventArgs I've also tried replace Local Ip addres instead of "localhost:8733" but it doesn't working.. – Jungeun Feb 27 '18 at 01:20
  • @MichaelRandall I tested on the emulator..! – Jungeun Feb 27 '18 at 01:20

1 Answers1

2

There several issues with testing WCF on Xamarin and Andriod

  • If you are on the Emulator, LocalHost will not work
  • If you are on either the Emulator or Device, IISExpress will not allow a connection

There are many remedies for this, however the easiest way i found to test this, is the following (not i don not recommend leaving your firewall off)

  • Turn your firewall off
  • Find your WCF applicationhost.config file ie at (.vs\config)
    • or right click issexpress -> show all applications, click the service, and go to the config path
    • Find your service and edit the bindings similar to the below for your site.

Example changes

<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 : You will have to change 43420 to your WCF Service Ip Address and change <put your IP address here> to your local PC IP Address

*For a more permanent solution you will need to look up how to configure your Advanced Firewall Settings.

Furthermore, accessing LocalHost on your emulator wont work. On the Emulator Virtual machine , the LocalHost refers to the device on which the code is running (the emulator).

If you want to refer to the computer which is running the Android Emulator, use the IP address 10.0.2.2 instead. You can read more from here.

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • I turned my firewall off and tried it but it still not working.. and I didn't use IIS Express...... – Jungeun Feb 27 '18 at 17:51
  • You will still have to use 10.0.2.2 instead of local host in the emulator – TheGeneral Feb 28 '18 at 00:25
  • 1
    This worked for me. I can now access my WCF service through the Android emulator at http://10.0.2.2:55724/MyService.svc – test Sep 21 '18 at 19:27