Search code examples
c#asp.netwcfsoap

Call SOAP api form c# using action, location, endpoint & namespace


I have never used soap api.

I have requirements that i have to call soap api & send response as a json(REST) api.

I have Web Service API Location(...?wsdl), Endpoint, Namespace & Soap action.

I also have username, password & other input parameters.

I am not sure how to create soap Envelope using above info & call api from c#.

Can anyone suggest me how to do it.

This is service GetRxHistory i am trying to call https://pharmacy.esihealthcaresolutions.com:9100/v4.0/RxHistoryService.svc?wsdl/GetRxHistory


Solution

  • First add service reference to your project using References > Add > Service Reference. In the address field enter the url for your wsdl file:

    https://pharmacy.esihealthcaresolutions.com:9100/v4.0/RxHistoryService.svc?singleWsdl

    You can create the client for calling this API using:

    RxHistoryServiceContractClient client = new RxHistoryServiceContractClient();
    

    You can then call various operations on the service using the client object.

    client.xxxx = xxx;
    client.xxx = xxx;
    

    In your case, it would be something like this for your username and password:

    client.ClientCredentials.UserName.UserName = "your username";
    client.ClientCredentials.UserName.Password = "your password";
    

    Finally, to get a response you'd write something like this:

    try
        {
          _Client.Open();
    

    You'd pass your request or client object here:

    GetRxHistoryResponse _Response = _Client.{MethodToGetResponse}(client);

          _Client.Close();
        }
    catch (Exception ex)  
        { 
    
        }