Viktor
Viktor

Reputation: 47

Xamarin exception - The contract contains synchronous operations, which are not supported

I'm getting pretty strange exception:

An exception of type 'System.InvalidOperationException' occurred in System.ServiceModel.ni.dll but was not handled in user code

Additional information: The contract 'ICalculatorService' contains synchronous operations, which are not supported in Silverlight. Split the operations into "Begin" and "End" parts and set the AsyncPattern property on the OperationContractAttribute to 'true'. Note that you do not have to make the same change on the server.

I have simplest WCF service in the world. No async code. Just this:

Host:

[ServiceContract]
public interface ICalculatorService
{
    [OperationContract]
    string GetSum(int a, int b);
}

public class CalculatorService : ICalculatorService
{
    public string GetSum(int a, int b)
    {
        return (a + b).ToString();
    }
}

class Program
{
    static void Main(string[] args)
    {
        Uri[] addressBase = new Uri[] { new Uri("http://localhost:9003/CalculatorService") };
        var host = new ServiceHost(typeof(CalculatorService), addressBase);
        host.Open();
        Console.Read();
    }
}

host config:

<system.serviceModel>
  <services>
    <service name="MobileWCF.ServerHost.CalculatorService">
      <endpoint address="net.tcp://localhost:8003/CalculatorService" binding="netTcpBinding"
        contract="MobileWCF.Contracts.ICalculatorService" />
      <endpoint address="http://localhost:9003/CalculatorService" binding="basicHttpBinding"
        contract="MobileWCF.Contracts.ICalculatorService" />
    </service>
  </services>
</system.serviceModel>

then, I've added Xamarin.Forms (portable) project, added View, and in code-behind in onClickEvent I added:

    void OnButtonClicked(object sender, EventArgs e)
    {
        string strAddress = "http://localhost:9003/CalculatorService";
        BasicHttpBinding httpBinding = new BasicHttpBinding();
        EndpointAddress address = new EndpointAddress(strAddress);

        ChannelFactory<ICalculatorService> channel = new ChannelFactory<ICalculatorService>(httpBinding, address);
        var game = channel.CreateChannel(address);
        var num = game.GetSum(3,4);
    }

Why am I getting this exception. Does Xamarin.Forms doesn't allow synchronous services? Everything should be in Begin/End APM model?

Upvotes: 0

Views: 211

Answers (2)

MKMohanty
MKMohanty

Reputation: 966

Xamarin says:

Xamarin.Forms has full support for the Windows Phone 8.1 platform using WinRT. The look and feel of apps using Windows Phone 8.1 support may be different to your earlier Xamarin.Forms Windows Phone apps that were based on Silverlight.

All Service Calls in Silverlight are Asynchronous. For this use:

[OperationContract(AsyncPattern = true)]

For more info see: here

Upvotes: 1

Gusman
Gusman

Reputation: 15151

Ok, let's see. You're using Xamarin.Forms with a PCL project. The PCL project uses the same profile as Silverlight, and Silverlight doesn't support synchronous methods, hence they're not supported in your app.

If you want to use it, or change the methods to follow the asynchronous patter or change your project type to use a Shared project, the Shared project does not use the portable class profile and can use the full framework.

Upvotes: 1

Related Questions