locked
Problem with Multiple Async Calls to WCF RRS feed

  • Question

  • Hello,

    I'm making multiple async calls to WCF from my UI with a loop. The code at the client works fine for these multipe calls with different data. However at the server side, only gets the last call made (the data at the server end gets last data posted).

    I'm not sure if i'm doing anything wrong, please help!

    My code looks like this:

     

    public void SaveImportTemplateAsync(IObjectDTO BusinessObjectData, long requestID)
    
    {
    
       BusinessObjectDataDTO businessobject = (BusinessObjectDataDTO)BusinessObjectData;
    
       importRequestID = requestID;
    
       try
    
       {
    
          ServiceContractClient ServiceClient = CreateServiceClient();
    
          ServiceClient .SaveImportBusinessObjectCompleted += new EventHandler<SaveImportBusinessObjectCompletedEventArgs>(ServiceClient_SaveImportBusinessObjectCompleted);
    
    
    
          ServiceClient .SaveImportBusinessObjectAsync(businessobject, requestID); 
    
       }
    
    
    
       catch (System.ServiceModel.FaultException<BusinessObjectService.WCFError> error)
    
       {
    
            // do something
    
       }
    
    
    
    } 
    
     
    
     
    
    private void businessObjectServiceClient_SaveImportBusinessObjectCompleted(object sender, SaveImportBusinessObjectCompletedEventArgs args)
    
    {
    
       batchCount++;
    
       if (batchCount == totalBatches)
    
       {
    
          // Do something to say that all requests are processed
    
       } 
    
    }
    
    
    
    
    
    public static ServiceContractClient CreateServiceClient()
    
    {
    
    BusinessObjectService.ServiceContractClient clientInstance = null;
    
    try
    
    {
    
    lock (lockerInstance)
    
    {
    
    using (var clientproxy = new CreateServerProxy())
    
    {
    
    clientInstance =clientproxy.CreateServiceClient();
    
    if (clientInstance == null) throw new ApplicationException("Could not connect");
    
    }
    
    }
    
    }
    
    catch 
    
    {
    
    throw;
    
    }
    
    return clientInstance;
    
    } 
    
    

     

    The method SaveImportTemplateAsync() gets called from the Front end in a loop with pages of data.

    One interesting thing i noted here is, if I introduce a sleep after the WCF call (SaveImportBusinessObjectAsync()), then this seems to work fine. But having a sleep in between, dosen't seem to be a good idea, as you never know the size and processing time of the server. I'm sure there should be a better way to write this code.

    Any thoughts on this piece of would be of really great help.

    Thanks in advance

     

    Thursday, August 13, 2009 3:42 PM

Answers

  • Thanks for your reply and the clarification Aloysius,

    The behavior does sound quite unexpected. This remind me of a former case in which the WCF client operation call's parameter always has some fixed value(for one of the parameter). And that issue is caused by the service-side Servicecontract/OperationContract has changed a parameter's name(without update the client proxy) which cause the underlying message content mismatch between service and client. 

    I suggest try updating the client-side proxy class or you can try creating a new client proxy or even using a new generated proxy to call the service synchronously to see whether it works.



    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Wednesday, August 19, 2009 9:38 AM

All replies

  • Where is your Async call to WCF here? Generally the methods have a BeginXXX(AsycCallback callback,Object state) signature?

    http://blogs.msdn.com/sajay
    Monday, August 17, 2009 7:01 PM
  • Hi Aloysius,

    From your description, you're using async method to call WCF service operation via a loop(passing different data via parameter), however, you found that only the last method call(in loop) get executed at server-side, correct?

    Regarding on the issue, here are some of my suggestions:

    ** As you mentioned that "the data at the server end gets last data posted", do you mean that among all the async method calls in your client loop, only the last one get arrived on server-side and executed? If so, I suggest you either turn on the WCF message logging/tracing or using some http(if you're using http binding) trace tool(like fiddler) to monitor the network traffic to verify that only one service call request is issued.

    #tracing and Message Logging
    http://msdn.microsoft.com/en-us/library/ms751526.aspx

    ** In the code you provided, I found that your service proxy at client-side is created via a factory method and you create a new proxy via that method in each loop entry. Is there any particular implementation in the loop? I suggest you also try directly creating the client proxy(you generated via add service reference) to see whether it behaves different.

    ** For general troubleshooting, you can also switch to call a simplified hello word method(accept a simple string and return string value) multiple times via async pattern to see whether the problem is specific to the original service operations.






    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Tuesday, August 18, 2009 3:48 AM
  • Hi Steven,

    Thanks for your response. Yes you are right, the data gets passed through parameter and the onCompleted method gets called after every completion.

    I did do the trace and interestingly the data from the client seems to be good, but at the server end only the last submitted data gets there. That is, If i make 10 calls with 10 different type of data

    At the client, data sent is 1, 2,3,4,5,6,7,8,9,10

    At the server the data that gets recieved is: 10,10,10,10,10,10,10,10,10,10

    Also the callback is called 10 times with the data being always the last one ("10").
    Note: Here the data depicted is for sample purposes only. The data i send is DTO with about 2K data in each batch.

    As you have suggested I switched to creating the proxy only once and making multiple calls in a loop after it, but I still have the same problem.

    Hope i'm clear, any help would be greatly appriciated.

    Thanks


    Tuesday, August 18, 2009 1:59 PM
  • Thanks for your reply and the clarification Aloysius,

    The behavior does sound quite unexpected. This remind me of a former case in which the WCF client operation call's parameter always has some fixed value(for one of the parameter). And that issue is caused by the service-side Servicecontract/OperationContract has changed a parameter's name(without update the client proxy) which cause the underlying message content mismatch between service and client. 

    I suggest try updating the client-side proxy class or you can try creating a new client proxy or even using a new generated proxy to call the service synchronously to see whether it works.



    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Wednesday, August 19, 2009 9:38 AM