How To Call WCF Service Method From PostMan In C#


I am developing XML SOAP services in WCF using Visual Studio 2022. My requirement is to delete and update some database records. I have one method to delete and update values in the database. 
Now, I am trying to call a service using a WCF endpoint in Postman, but I am not able to call it. If you are also facing the same issue, then you are in the right place. Let's discuss the solution for that.

Calling a WCF service method from Postman in C#

we can do that this by following these steps:

  1. We need to know the URL of the service endpoint that we want to call. This URL typically looks like http://localhost:5572/Product/Service.svc and now open Postman and create a new request of type POST. In the request URL, paste the service endpoint URL.
  2. Set Headers: If the service requires any specific headers (like authentication tokens or content type), we should add them to the request. This can typically be done in the Headers section of the Postman request.
  3. If the service method expects any data in the request body, we should add it in the Body section of the Postman request. This can be in JSON format, XML, or any other format that the service expects.

Now, let's see an example of how we can call a WCF service method from C# code:


using System;
using System.Net.Http;
using System.Threading.Tasks;

public class Program
{
    public static async Task Main(string[] args)
    {
        // Define the URL of the WCF service endpoint
        string serviceUrl = "http://localhost:5572/Product/Service.svc";

        // Create an instance of HttpClient
        using (HttpClient client = new HttpClient())
        {
            try
            {
                // Prepare data if needed
                string requestData = "{\"Name\":\"Phone\", \"Price\":\"122\"}";

                // Create a StringContent object with request data
                var content = new StringContent(requestData, System.Text.Encoding.UTF8, "application/json");

                // Send a POST request to the service endpoint
                HttpResponseMessage response = await client.PostAsync(serviceUrl, content);

                // Check if the request was successful
                if (response.IsSuccessStatusCode)
                {
                    // Read the response content
                    string responseContent = await response.Content.ReadAsStringAsync();

                    // Output the response
                    Console.WriteLine("Response from service: " + responseContent);
                }
                else
                {
                    // Output the error message
                    Console.WriteLine("Error: " + response.ReasonPhrase);
                }
            }
            catch (Exception ex)
            {
                // Handle any exceptions
                Console.WriteLine("Exception: " + ex.Message);
            }
        }
    }
}
2
How can I test a WCF SOAP service using Postman?, First, create a WCF SOAP service that returns a product list. Then, show me the configuration for that service and explain how to call it in Postman.
Create a WCF SOAP Service:
We need to create a WCF service that returns a product list. Here's an example of a simple WCF service:

using System.Collections.Generic;
using System.ServiceModel;

[ServiceContract]
public interface IProductService
{
    [OperationContract]
    List<Product> GetProductList();
}

[DataContract]
public class Product
{
    [DataMember]
    public int Id { get; set; }
    
    [DataMember]
    public string Name { get; set; }
    
    [DataMember]
    public double Price { get; set; }
}

public class ProductService : IProductService
{
    public List<Product> GetProductList()
    {
        // In a real application, this method would retrieve the product list from a database or another source
        return new List<Product>
        {
            new Product { Id = 1, Name = "Product A", Price = 10.99 },
            new Product { Id = 2, Name = "Product B", Price = 20.49 },
            new Product { Id = 3, Name = "Product C", Price = 5.99 }
        };
    }
}
 
Configure the Service: 
After creating the service, we need to configure it in a hosting environment, creating a WCF project in Visual Studio, adding the service file (ProductService.svc), and configuring the service endpoints in the web.config file. 
<system.serviceModel>
    <services>
        <service name="AppService.ProductService">
            <endpoint address=""
                      binding="basicHttpBinding"
                      contract="YourNamespace.IProductService" />
        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior>
                <serviceMetadata httpGetEnabled="true" />
                <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
</system.serviceModel>

Call the Service in Postman:
Once the service is configured and running, we can call it using Postman by following these steps:

  • Open Postman and create a new request.
  • Enter the URL of the service endpoint (http://localhost:4578/ProductService/ProductService.svc).
  • Add the required SOAP headers if any.In the body section, construct a SOAP envelope with the appropriate SOAP action for the GetProductList method.
  • Send the request and inspect the response for the product list.
This step allows us to test the WCF SOAP service using Postman to ensure that it is functioning correctly.
3

Before calling the URL in Postman, ensure that you are using the correct URL, we recommend enabling the help document for that.

        
            <endpointBehaviors>
                <behavior name="ESEndPointBehavior">
                    <webHttp helpEnabled="true"/>
                </behavior>
            </endpointBehaviors>
        
    

We need to apply the ESEndPointBehavior to the endpoint and then, we can access the help document in our browser by passing a SOAP message to the service endpoint.

Enabling the help document, which can be a helpful resource for understanding the available operations and their parameters in the SOAP service. By setting helpEnabled="true" in the webHttp behavior, we expose a help page that describes the service and its operations. 

4

In our project we need to obtain a SOAP message, we using the service endpoint definition and some tooling to generate a valid request. Furthermore, it's important not to include ?wsdl as part of the endpoint address when sending data to the endpoint.

We suggest installing a tool called Fiddler and then using the following raw request:

        
            URL: http://localhost:4578/ProductService/ProductService.svc
            User-Agent: Fiddler
            Content-Type: application/json
            Host: localhost

            {"id":"1","Product":"Tv"}