Error when calling a SOAP servie using "OperationContextScope" is failing while succeed on PostMan

john john Pter 1,040 Reputation points
2025-03-16T15:10:28.7966667+00:00

I want to make a call to external SOAP service, now the call works well inside Postman, as follow:-

User's image

User's image

But when i tried to do the call inside our .net core 8.0 application i got "Server Error", here is the code:- private async Task<string> CallSoapServiceAsync()

{

string endpointUrl = "https://service.leads360.com/ClientService.asmx";

BasicHttpsBinding binding = new BasicHttpsBinding

{

Security = new BasicHttpsSecurity

{

Mode = BasicHttpsSecurityMode.Transport // Ensures HTTPS

},

MaxReceivedMessageSize = 2147483647 // Handle large responses

};

EndpointAddress endpointAddress = new EndpointAddress(endpointUrl);

using (var factory = new ChannelFactory<ILeadsService>(binding, endpointAddress))

{

ILeadsService client = factory.CreateChannel();

// Create SOAP Message

string soapRequest =

"<?xml version="1.0" encoding="utf-8"?>" +

"<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/">"+

"<soap:Body>"+

"<GetReportResults xmlns="https://service.leads360.com">"+

" <username></username>"+

"<password>**</password>"+

"<reportId>**</reportId>"+

"<templateValues>xml</templateValues>"+

"</GetReportResults>"+

"</soap:Body>"+

"</soap:Envelope>";

using (var scope = new OperationContextScope((IClientChannel)client))

{

HttpRequestMessageProperty requestMessage = new HttpRequestMessageProperty();

requestMessage.Headers["Content-Type"] = "application/soap+xml; charset=utf-8";

requestMessage.Method = "POST";

OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = requestMessage;

return await client.SendSoapRequestAsync(soapRequest);

}

}

}

So why this is working on postman but not on the .net application? what is wring with my code? Here is a screen shot of the code inside visual studio:-

User's image

Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 30,126 Reputation points
    2025-03-16T16:15:11.26+00:00

    Visual Studio provides a feature to automatically generate code for interacting with SOAP web services. This is done by adding a 'Service Reference' to your .NET Core project. You can access this by right-clicking your project, selecting 'Add Service Reference', and then choosing the WCF option. You'll then input the WSDL URL, such as https://service.leads360.com/ClientService.asmx?WSDL, which describes the service's operations. This process creates a 'channel factory' in your code, which manages the communication with the SOAP service.

    This differs significantly from using Postman. Postman directly sends a raw HTTP POST request with the SOAP message. In contrast, the 'Add Service Reference' approach generates code that handles the SOAP protocol intricacies, including message formatting and communication management, through the channel factory.

    For a more direct approach, similar to Postman, you can use the HttpClient class in your .NET Core code. This allows you to construct and send a POST request with your hardcoded SOAP message, bypassing the automatically generated channel factory.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.