Search code examples
wcfxamarintimeout

Xamarin with WCF has a timeout error


My app uses WCF to transfer data from server and app.It works well all along.But recently,if the app does not operate after a period of time,there's always throwing a timeout exception.When the timeout exception was thrown,winform application can call WCF normally。If restart debug,the app can call WCF again;and after a while,it throw timeout exception again.What can I do for this?

My code like this:

 // CreateBinding
  private static BasicHttpBinding CreateBasicHttp()
    {
        BasicHttpBinding binding = new BasicHttpBinding
        {
            Name = "basicHttpBinding",
            MaxBufferSize = 2147483647,
            MaxReceivedMessageSize = 2147483647
        };
        binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
        TimeSpan timeout = new TimeSpan(0, 0, 30);
        binding.SendTimeout = timeout;
        binding.OpenTimeout = timeout;
        binding.ReceiveTimeout = timeout;
        return binding;
    }

 // Create Endpoint

    private static EndpointAddress CreateEndPoint(string serviceName)
    {
        string serviceUrl = App.RemoteUrl + serviceName + ".svc";


        EndpointAddress EndPoint = new EndpointAddress(serviceUrl);
        return EndPoint;
    }   

 // Create binder
 public static WCFBinder GetBinder<T>()
    {
        WCFBinder binder;
        Type t = typeof(T);
        string serviceName = t.Name.Substring(1, t.Name.Length - 1);
        binder.Binding = CreateBasicHttp();
        binder.EndPoint = CreateEndPoint(serviceName);
        return binder;
    }

  // Create a client
  var binder = SDHCommonMethod.GetBinder<IAAA001BLLService>();

  IAAA001BLLService asc = new AAA001BLLServiceClient(binder.Binding, binder.EndPoint);

  // Call WCF
  var obj = await Task.Factory.FromAsync(asc.BeginSelectAAA001, asc.EndSelectAAA001, dto, TaskCreationOptions.None);

Solution

  • The code is right. In vs2017,it can work.Maybe my vs2015 has something wrong.