none
Building & Consuming Async Web Service RRS feed

  • Question

  • I'm fairly new to web services.  I built a WCF Web Service.  It works fine when making synchronous calls.  I'm stuck trying to make an async call.  On the client side in Visual Studio, I get a "Cannot await TestWebServices.QuotingEngineService.SmallGroupQuote[]" error when trying to make the call.  Below is the pic of client configuration, the client code followed by the web service code. If you can point me in the right direction it would be greatly appreciated:

    Client Web Config:

            <bindings>
                  <basicHttpBinding>
                    <binding name="BasicHttpBinding_IQuotingService" maxReceivedMessageSize="2147483647" maxBufferPoolSize="524288" maxBufferSize="2147483647" />
                  </basicHttpBinding>
            </bindings>
            <client>
                  <endpoint address="http://10.32.0.99/QuotingWebServiceHMO/Service.svc"
                        binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IQuotingService"
                        contract="QuotingEngineService.IQuotingService" name="BasicHttpBinding_IQuotingService" />
            </client>

    Screen Shot of Configuration:

    Page Load:

            protected void Page_Load(object sender, EventArgs e)
            {
                List<int> corpIDs = new List<int>() { 57 };
                fetchRenewalAsync(corpIDs);
    }

    fetchRenewalAsync - Where I get the error in VS:

            private static async void fetchRenewalAsync(List<int> corpIDs)
            {
                using (QuotingServiceClient client = new QuotingServiceClient())
                {
      
                    var task1 = client.GetSmallGroupRenewals(corpIDs.ToArray(), "3/1/2015", "2/23/2015", 121);
                    return await task1;
                }       
            }


    Quoting Service Interface:

        [ServiceContract]
        public interface IQuotingService
        {
    
            // Small Group Renewals:
            [OperationContract]
            List<SmallGroupQuote> GetSmallGroupRenewals(List<int> corpIDs, string effectiveDate, string activeDate, int censusBatchID);
    
        }

    Quoting Service:

    namespace QuotingServices
    {
        public class QuotingService : IQuotingService
        {
    
            #region List<SmallGroupQuote> GetSmallGroupRenewals
            public List<SmallGroupQuote> GetSmallGroupRenewals(List<int> corpIDs, string effectiveDate, string activeDate, int censusBatchID)
            {
                return SmallGroup.GetSmallGroupRenewals(corpIDs, effectiveDate, activeDate, censusBatchID);
    
            }
            #endregion
    
    
    
        }
    }
    

    Another thing I noticed is the Async option is not available when I run the Web Service in debug mode, if that indicates anything.

    Thanks

    Thursday, February 26, 2015 10:07 PM

Answers

  • Hi Jamezn2013,

    I see that you have used the following code:

    private static async void fetchRenewalAsync(List<int> corpIDs)
            {
                using (QuotingServiceClient client = new QuotingServiceClient())
                {
      
                    var task1 = client.GetSmallGroupRenewals(corpIDs.ToArray(), "3/1/2015", "2/23/2015", 121);
                    return await task1;
                }       
            }

    If you want to make the asynchronous WCF call, please try to modify the client.GetSmallGroupRenewals as client.GetSmallGroupRenewalsAsync:

    private static async void fetchRenewalAsync(List<int> corpIDs)
            {
                using (QuotingServiceClient client = new QuotingServiceClient())
                {
      
                    var task1 = client.GetSmallGroupRenewalsAsync(corpIDs.ToArray(), "3/1/2015", "2/23/2015", 121);
                    return await task1;
                }       
            }

    For more information, please try to refer to the following article: http://www.codeguru.com/columns/experts/building-and-consuming-async-wcf-services-in-.net-framework-4.5.htm .

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Jamezn2013 Friday, February 27, 2015 4:23 PM
    Friday, February 27, 2015 5:07 AM
    Moderator

All replies

  • Hi Jamezn2013,

    I see that you have used the following code:

    private static async void fetchRenewalAsync(List<int> corpIDs)
            {
                using (QuotingServiceClient client = new QuotingServiceClient())
                {
      
                    var task1 = client.GetSmallGroupRenewals(corpIDs.ToArray(), "3/1/2015", "2/23/2015", 121);
                    return await task1;
                }       
            }

    If you want to make the asynchronous WCF call, please try to modify the client.GetSmallGroupRenewals as client.GetSmallGroupRenewalsAsync:

    private static async void fetchRenewalAsync(List<int> corpIDs)
            {
                using (QuotingServiceClient client = new QuotingServiceClient())
                {
      
                    var task1 = client.GetSmallGroupRenewalsAsync(corpIDs.ToArray(), "3/1/2015", "2/23/2015", 121);
                    return await task1;
                }       
            }

    For more information, please try to refer to the following article: http://www.codeguru.com/columns/experts/building-and-consuming-async-wcf-services-in-.net-framework-4.5.htm .

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Jamezn2013 Friday, February 27, 2015 4:23 PM
    Friday, February 27, 2015 5:07 AM
    Moderator
  • Thanks, that worked.  I had to set the timeout, so I'm looking into that.  I'm now getting results back though:

    Working Code:

            private static async void fetchRenewalAsync(List<int> corpIDs)
            {
                using (QuotingServiceClient client = new QuotingServiceClient())
                {
                    client.Endpoint.Binding.SendTimeout = new TimeSpan(0, 5, 00);
                    
                    var task1 = client.GetSmallGroupRenewalsAsync(corpIDs.ToArray(), "3/1/2015", "2/23/2015", 121);
                   
                    var renewals  = await task1;
                    renderRenewals(renewals, "C:\\Temp\\");
                    //return await task1;
                }
                return;
            }

    Friday, February 27, 2015 4:24 PM