How to Call a Soap Web Service from Asp.Net Core


Question: How do you call a web service that uses XML Soap Envelope as Payload Body?


Login to See the Rest of the Answer

Answer:
When working with Old Protocols like the Soap Web Service Protocol in Asp.Net Core 3.1, you can craft the Web Request just the same as would when calling an API, however, different Web Services require different Header Parameters to be passed, it would only help if you find out the requirement for making a Soap Web Request in Asp.Net Core 3.1. For example, code, see the code below:

 var client = new RestClient("https://FirstLevelDomain/urlEndPoint.asmx?op=NameOfFunction");
            client.Timeout = -1;
            var request = new RestRequest(Method.POST);
            request.AddHeader("Host", "DomainNameWithoutHttps");
            request.AddHeader("Content-Type", "text/xml");
            request.AddHeader("charset", "utf-8");
            request.AddHeader("SOAPAction", "\"http://DomainWithoutHttps/NameOfFunction\"");
            request.AddParameter("text/xml", "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\r\n  <soap:Body>\r\n    <NameOfFunction xmlns=\"http://domainName.com/\">\r\n      <ParameterYouWantToPass>"+ paramValue + "</clientId>\r\n  </soap:Body>\r\n</soap:Envelope>", ParameterType.RequestBody);
            IRestResponse response = await client.ExecuteAsync(request);


However, if the above did not help to call a web service that uses XML Soap Envelope as Payload Body, perhaps the steps outlined below might help.

To call a SOAP web service from an ASP.NET Core 3.1 application, you can follow these steps:

  1. Add a service reference to the SOAP web service:

    • Right-click on your ASP.NET Core project in Visual Studio.
    • Select "Add" -> "Connected Service" from the context menu.
    • Choose "Microsoft WCF Web Service Reference Provider" from the options.
    • Enter the URL of the WSDL file or the web service endpoint.
    • Click "Go" and then "Finish" to generate the proxy classes.

    This will generate the necessary client proxy classes to communicate with the SOAP web service.

  2. Use the generated client proxy to call the web service methods:

    • Instantiate the generated client proxy class in your code.
    • Set any required properties or parameters for the web service call.
    • Invoke the desired method on the client proxy.

    Here's an example of calling a method on the SOAP web service:

    // Instantiate the generated client proxy
    var client = new YourWebServiceClient();
    
    try
    {
        // Set any required properties or parameters
        // ...
    
        // Call the web service method
        var response = await client.YourWebServiceMethodAsync(/* method parameters */);
    
        // Process the response from the web service
        // ...
    }
    catch (Exception ex)
    {
        // Handle any exceptions that occur during the web service call
        // ...
    }
    finally
    {
        // Dispose the client proxy to release resources
        client.Close();
    }
    
    
    1. Replace YourWebServiceClient with the actual generated client proxy class name, and YourWebServiceMethodAsync with the desired web service method you want to call.

    2. Handle any exceptions that may occur during the web service call:

      • Use try-catch blocks to catch and handle any exceptions that may occur during the web service call.
      • You can handle specific exceptions, such as FaultException or CommunicationException, or catch the base Exception type to handle any unexpected errors.

      It's important to properly handle exceptions to handle error conditions and ensure graceful error handling in your application.

    By following these steps, you can call a SOAP web service from your ASP.NET Core application using the generated client proxy classes.

 

 







For peering opportunity Autonomouse System Number: AS401345 Custom Software Development at ErnesTech Email AddressContact: [email protected]