Unable to use WCF within Android app

ScottNimrodScottNimrod USMember ✭✭
edited August 2015 in Xamarin.Forms

I receive the following exception when attempting to invoke a WCF service within my Droid project:

"Operation is not valid due to the current state of the object"

I am not sure what I am doing wrong.
1. I wait on my Open event before I perform a RPC (remote procedure call).
2. I want until my RPC is completed before I attempt to close my connection.

public class EmailClient : IEmailClient
{
    FasadeClient _client = null;

    public void Send(string title, string fromAddress, string toAddress, string content)
    {
        Activate();

        var message = new Messaging.Message()
        {
            Subject = title,
            From = fromAddress,
            To = toAddress,
            Body = content
        };

        _client.OpenCompleted += (se, ev) => _client.SendEmailAsync(message, true);
        _client.SendEmailCompleted += (se, ev) => Deactivate();
    }

    void Activate()
    {
        var binding = CreateBasicHttp();
        var endpoint = new EndpointAddress(@"http://www.some_domain.com/some_service.svc");

        _client = new FasadeClient(binding, endpoint);
        _client.OpenAsync();
    }

    void Deactivate() => _client.Close();

    BasicHttpBinding CreateBasicHttp()
    {
        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;
    }
}
Tagged:

Answers

Sign In or Register to comment.