WCF web service in my existing web application

bigFan 21 Reputation points
2021-06-30T04:37:19.463+00:00

Hello everyone, I have been using web service .asmx for a while now. Now the requirement changes and I need to use WCF service. I have my existing web application. Normally if I need to add a web service, I will just right click and add it to a folder that I want in my existing web application. I can utilize the classes I have created straight away. A lot of tutorial online is related to creating a separate WCF project, then host and then add service reference and so on.

Is there a better way to do this with my existing application? I know I can right click and add WCF service. But I found consuming it is very tricky and I have not successfully consume it with my angularjs script.

Thank you very much if you could help and share with me some links.

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,417 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Yijing Sun-MSFT 7,071 Reputation points
    2021-06-30T07:42:31.76+00:00

    Hi @bigFan ,

    But I found consuming it is very tricky and I have not successfully consume it with my angularjs script.

    Your project is a angular?
    The most important task when consuming a WCF Service in an ASP.Net Web Application is adding the WCF Service reference into the ASP.Net web application.

    1. Right-click on the ASP.Net Web Application and click on "Add Service Reference"
    2. Paste or type the WCF Service URL address.
    3. Discover the WCF Services available related to that URL address and you see that in that related URL one WCF Service is found message is displayed along with the WCF Service name, "WCF Services" in the preceding right hand side window.

    More details,you could refer to below article:
    https://www.c-sharpcorner.com/UploadFile/0c1bb2/consuming-wcf-service-in-a-web-application/
    Best regards,
    Yijing Sun


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. bigFan 21 Reputation points
    2021-06-30T21:35:12.393+00:00

    @AgaveJoe yes I really don't want to switch from ASMX to WCF. I have been having issues with calling Asynchronous function. I need to call the public api for accounting application. They only have asynchronous method. My page froze if I use web service until I close visual studio and reopen it. It seems to be in a deadlock. So here is how my code looks.

    [WebMethod(EnableSession = true)]  
    public async Task<string> syncToAccountingAppAsync(string serviceType)  
    {  
       if(serviceType == "ABC"){  
         //return await InsertXeroService(id, serviceType); //This doesn't work either.  
          List<Task<string>> tasks = new List<Task<string>>();  
          tasks.Add(InsertXeroService(serviceType));  
      
          var results = await Task.WhenAll(tasks);  
          return results[0];  
       }  
    }  
    

    And here is the InsertXeroService function looks like:

    private async Task<string> InsertXeroService(string serviceType)  
        {  
            var invoice = new Xero.NetStandard.OAuth2.Model.Accounting.Invoice  
            {  
                ......  
            };  
      
            var invoiceList = new List<Xero.NetStandard.OAuth2.Model.Accounting.Invoice>();  
            invoiceList.Add(invoice);  
      
            var invoices = new Invoices();  
            invoices._Invoices = invoiceList;  
      
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;  
            var AccountingApi = new AccountingApi();  
            var response = await AccountingApi.CreateInvoicesAsync(xeroken.AccessToken, tenantId, invoices);  
      
      
            return response._Invoices[0].UpdatedDateUTC.ToString();  
        }  
    

    The data was synced to the accounting application just fine. But it never returned the response. Have you been able to call asynchronous function successfully using ASMX? Thanks

    0 comments No comments