0

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

Ankur Akvaliya
  • 2,989
  • 4
  • 28
  • 53
  • [This should get you started](https://learn.microsoft.com/en-us/dotnet/framework/wcf/accessing-services-using-a-wcf-client) – Crowcoder Nov 12 '18 at 14:23

2 Answers2

1

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

    }
MAK
  • 1,250
  • 21
  • 50
  • Just wondering. Isn't there any way to create soap envelop from data that i have? Then i want to try something like this https://stackoverflow.com/questions/4791794/client-to-send-soap-request-and-received-response – Ankur Akvaliya Nov 12 '18 at 16:07
0

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

Abraham Qian
  • 7,117
  • 1
  • 8
  • 22