none
svcutil.exe and making async calls RRS feed

  • Question

  • I'm moving from Silverlight to WPF and am porting all of my web service proxy wrappers created from slsvcutil.exe to svcutil.exe.

    I know I can use the /async parameter to create Asynchronous methods, but I'm not sure how to consume them.  For Silverlight I would do something like this:

    public event System.EventHandler<ix_Trace_GetByIdCompletedEventArgs> ix_Trace_GetByIdCompleted;
    
    public void Begin_ix_Trace_GetById(Guid Id)
    {
        Guid? traceId = Guid.NewGuid();
        try
        {
            Iix_TraceServiceClient proxy = new Iix_TraceServiceClient();
            AssignCredentials(proxy);
            proxy.ix_Trace_GetByIdCompleted += new EventHandler<ix_Trace_GetByIdCompletedEventArgs>(proxy_ix_Trace_GetByIdCompleted);
            proxy.ix_Trace_GetByIdAsync(Id);
        }
        catch (Exception ex)
        {
            //if (IfxTrace._traceCatch) IfxEvent.PublishTrace(traceId, _as, _cn, "Begin_ix_Trace_GetById", ex);
    		throw IfxWrapperException.GetError(ex, _as, _cn, "Begin_ix_Trace_GetById");
        }
        finally
        {
            //if (IfxTrace._traceLeave) IfxEvent.PublishTrace(traceId, _as, _cn, "Begin_ix_Trace_GetById", IfxTraceCategory.Leave);
        }
    }
    
    void proxy_ix_Trace_GetByIdCompleted(object sender, ix_Trace_GetByIdCompletedEventArgs e)
    {
        Guid? traceId = Guid.NewGuid();
        try
        {
            System.EventHandler<ix_Trace_GetByIdCompletedEventArgs> handler = ix_Trace_GetByIdCompleted;
            if (handler != null)
            {
                handler(sender, e);
            }
        }
        catch (Exception ex)
        {
            //if (IfxTrace._traceCatch) IfxEvent.PublishTrace(traceId, _as, _cn, "ix_Trace_GetByIdCompleted", ex);
            ExceptionHelper.WriteToIsolatedStorage(ex, _as, _cn, "ix_Trace_GetByIdCompleted");
        }
        finally
        {
            //if (IfxTrace._traceLeave) IfxEvent.PublishTrace(traceId, _as, _cn, "ix_Trace_GetByIdCompleted", IfxTraceCategory.Leave);
        }
    }

    But now that I'm using svcutil.exe, how to I make Asynchronous calls?

    Thanks.

    Thursday, July 20, 2017 5:40 AM

Answers

  • Hi moondaddy,

    Do you want to generate Task-based or asynchronous operations?

    For Task-based which is much convenience, you could run below command,

    svcutil.exe http://localhost:9159/Service1.svc /a /tcv:version35

    And then call it like below code.

            private async void button_Click(object sender, RoutedEventArgs e)
            {
                WCFSimpleSvc.Service1Client client = new WCFSimpleSvc.Service1Client();            
                string result = await client.GetDataAsync(123);
                MessageBox.Show(result);
            }

    If you want to achieve like in Silverlight, I think you could try below command

    svcutil.exe http://localhost:9159/Service1.svc /a

    And then call like below:

            private void button1_Click(object sender, RoutedEventArgs e)
            {
                WCFSimpleSvc.Service1Client client = new WCFSimpleSvc.Service1Client();
                client.GetDataCompleted += Client_GetDataCompleted;
                client.GetDataAsync(123);
            }
    
            private void Client_GetDataCompleted(object sender, WCFSimpleSvc.GetDataCompletedEventArgs e)
            {
                MessageBox.Show(e.Result);
            }
    

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, July 20, 2017 7:10 AM
  • Thank you Edward. 

    OK this helped.  the part I was missing was the switch "/tcv:Version35".  After adding this the "Completed" event where added and now almost all of my existing code works As-Is.

    Thanks again.  His is huge as I have 100s of code files to regenerate using svcutil.exe.

    • Edited by moondaddy Thursday, July 20, 2017 9:46 PM found update
    • Marked as answer by moondaddy Friday, July 21, 2017 4:00 AM
    Thursday, July 20, 2017 5:26 PM

All replies

  • Hi moondaddy,

    Do you want to generate Task-based or asynchronous operations?

    For Task-based which is much convenience, you could run below command,

    svcutil.exe http://localhost:9159/Service1.svc /a /tcv:version35

    And then call it like below code.

            private async void button_Click(object sender, RoutedEventArgs e)
            {
                WCFSimpleSvc.Service1Client client = new WCFSimpleSvc.Service1Client();            
                string result = await client.GetDataAsync(123);
                MessageBox.Show(result);
            }

    If you want to achieve like in Silverlight, I think you could try below command

    svcutil.exe http://localhost:9159/Service1.svc /a

    And then call like below:

            private void button1_Click(object sender, RoutedEventArgs e)
            {
                WCFSimpleSvc.Service1Client client = new WCFSimpleSvc.Service1Client();
                client.GetDataCompleted += Client_GetDataCompleted;
                client.GetDataAsync(123);
            }
    
            private void Client_GetDataCompleted(object sender, WCFSimpleSvc.GetDataCompletedEventArgs e)
            {
                MessageBox.Show(e.Result);
            }
    

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, July 20, 2017 7:10 AM
  • Thank you Edward. 

    OK this helped.  the part I was missing was the switch "/tcv:Version35".  After adding this the "Completed" event where added and now almost all of my existing code works As-Is.

    Thanks again.  His is huge as I have 100s of code files to regenerate using svcutil.exe.

    • Edited by moondaddy Thursday, July 20, 2017 9:46 PM found update
    • Marked as answer by moondaddy Friday, July 21, 2017 4:00 AM
    Thursday, July 20, 2017 5:26 PM
  • Hi moondaddy,

    i am glad it works now, and I would suggest you mark the helpful reply as answer to close this thread.

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by moondaddy Friday, July 21, 2017 4:00 AM
    • Unmarked as answer by moondaddy Friday, July 21, 2017 4:01 AM
    Friday, July 21, 2017 1:56 AM