-1

I have a wcf service published on my windows server.

I have a simple method which makes a simple db query in the service :

  public bool HasActifContract(string accountNumber)
  {
   try
   {
     using (MyEntitites dc = new MyEntitites())
     {
         var client = (from ro in dc.MyClientTable where ro.AccountNumber == accountNumber select ro).FirstOrDefault();
         return client != null && client.ContractExpirationDate > DateTime.Now.Date;
     }
   }
   catch (Exception ex)
   {
     return false;
   }
  }

In my wpf application, I am calling this method like:

  using (var sc = Tools.GenerateServiceReference())
  {
    var result = sc.HasActifContract(client.Vers_AccountNumber);
    if (!result)
    {
      return;
    }
  }

Generation of the service reference :

 public static ServiceClient GenerateServiceReference()
 {
  MyUtilities.Utility utility = new MyUtilities.Utility();
  var serviceURL = ConnectionUrl + "Service.svc";

  
  EndpointAddress ep = new EndpointAddress(new Uri(serviceURL));

  BasicHttpsBinding bind = new BasicHttpsBinding(
           BasicHttpsSecurityMode.TransportWithMessageCredential);

  bind.MaxReceivedMessageSize = int.MaxValue;
  bind.MaxBufferPoolSize = int.MaxValue;
  bind.MaxBufferSize = int.MaxValue;
 
  bind.ReceiveTimeout = new TimeSpan(0, 0, 10);
  bind.SendTimeout = new TimeSpan(0, 0, 5 * 60);
  bind.BypassProxyOnLocal = true;
  bind.UseDefaultWebProxy = true;
  bind.Security.Message.ClientCredentialType =
                        BasicHttpMessageCredentialType.UserName;

 

  //construct client
  ServiceClient myServiceRef = new ServiceClient(bind, ep);

  //pass custom credentials
  myServiceRef.ClientCredentials.UserName.UserName = utility.ServiceUserName;
  myServiceRef.ClientCredentials.UserName.Password = utility.ServicePassword;
  return myServiceRef;
  
}

The weired behavior is that the second time I call this code :

 var result = sc.HasActifContract(client.Vers_AccountNumber);

there is no response the server doesn't get any call and at the end I have a timeout message.

Why the first time it works always?

Edit: I tested the same method in another seperated wpf app, it works any time I call it. The problem comes from my applicaiton I think.

Any idea, please ?

1 Answer 1

1

Based on the code provided, try raising the value of Receivetiemout first to make sure it's not an error caused by running too long.

bind.ReceiveTimeout = new TimeSpan(0, 0, 5 * 60); // 5 minutes

Then you say that the error occurred the second time you ran the code, indicating that it may have something to do with the first run. There is no part of the code that closes the client, which may cause a timeout. The following code can be left at the end.

myServiceRef.Close();
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer. I tried, It doesn't change the result. I think the problem comes from my wpf application. I am trying the same thing on a little application, it works anytime.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.