Ankur
Ankur

Reputation: 3209

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

Upvotes: 0

Views: 2234

Answers (2)

Abraham Qian
Abraham Qian

Reputation: 7522

Isn't there any way to create soap envelop from data that i have?

We could use the Message class(System.ServiceModel.Channels) static method, CreateMessage method. I have made a demo, wish it is useful to you.

class Program
    {
        static void Main(string[] args)
        {
            Product p = new Product()
            {
                ID = 1,
                Name = "Mango"
            };
            Message m=Message.CreateMessage(MessageVersion.Soap12, "mymessage", p);
            MessageHeader hd = MessageHeader.CreateHeader("customheader", "mynamespace", 100);
            m.Headers.Add(hd);
            Console.WriteLine(m);
        }
    }
    public class Product
    {
        public int ID { get; set; }
        public string Name { get; set; }
}

Result. enter image description here Here is an official document.
https://learn.microsoft.com/en-us/dotnet/api/system.servicemodel.channels.message.createmessage?view=netframework-4.7.2

Upvotes: 0

MAK
MAK

Reputation: 1288

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)  
    { 

    }

Upvotes: 1

Related Questions