Consuming web service ... stuck :(

Baluba

Member
Joined
Dec 24, 2017
Messages
5
Programming Experience
Beginner
Hey,

During the Christmas vacation I tried to consume a web service. However I am stuck on how to parse the result,

I've been trying for some evenings now to get how to do it but I haven't managed to find something that shows me how to do it. Just examples showing simpler things like returning an int or writing something to the browser.

Could anyone show me how they would consume a web service like this to help me get out of the hole?

C#:
[FONT='inherit']HTTP/1.1 200 OK[/FONT]Content-Type: text/xml; charset=utf-8
Content-Length: [COLOR=darkblue]length[/COLOR]

<?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>
    <RoomBookingStatusByIdResponse xmlns="http://tempuri.org/">
      <RoomBookingStatusByIdResult>
        <BookingPayment>
          <RoomId>[COLOR=darkblue]int[/COLOR]</RoomId>
          <RoomType>[COLOR=darkblue]string[/COLOR]</RoomType>
          <BookingStatus>[COLOR=darkblue]string[/COLOR]</BookingStatus>
        </BookingPayment>
        <BookingPayment>
          <RoomId>[COLOR=darkblue]int[/COLOR]</RoomId>
          <RoomType>[COLOR=darkblue]string[/COLOR]</RoomType>
          <BookingStatus>[COLOR=darkblue]string[/COLOR]</BookingStatus>
        </BookingPayment>
      </RoomBookingStatusByIdResult>
    </RoomBookingStatusByIdResponse>
  </soap:Body>
</soap:Envelope>


When I try something like this it won't work. I've tried using xmldocument with no luck.. :upset:

C#:
       protected void Button1_Click(object sender, EventArgs e)
        {
            hotelService.PaymentWebService test = new hotelService.PaymentWebService();


            var result = test.RoomBookingStatusById(0345);
        }



Sorry for this coming off as a "give me the solution!" but I just can't seem to figure it out and have tried during Christmas before family duties took all the time and I've picked it up again this week, without getting any further. :disgust:

Thanks!
 
What is the data type of your 'result' variable? Put another way, what is the return type of RoomBookingStatusById?

The return type from the service is a list of objects of this BookingPayment class
C#:
    public class BookingPayment
    {
        public int RoomId { get; set; }
        public string RoomType { get; set; }
        public string BookingStatus { get; set; }
    }

Here is the entire Web service
C#:
   List<BookingPayment> objPayment = new List<BookingPayment>()
    {
        new BookingPayment {RoomId = 0567 , RoomType = "AC Double", BookingStatus = "Pending"},
        new BookingPayment {RoomId = 0345 , RoomType = "Non AC Double", BookingStatus = "Booked"},
        new BookingPayment {RoomId = 0468 , RoomType = "AC Single", BookingStatus = "Free"}
    };


    [WebMethod]
    public List<BookingPayment> RoomBookingStatusById(int id)
    {
        return objPayment.Where(b => b.RoomId == id).ToList();
    }


    public class BookingPayment
    {
        public int RoomId { get; set; }
        public string RoomType { get; set; }
        public string BookingStatus { get; set; }
    }

This is the error I get.
C#:
cannot implicitly convert type 'HotelService.hotelService.BookingPayment[]''

I've tried to make an array and a list of the same type (I just copied the BookingPayment class) in project that is consuming the web service out of desperation from running out of options and just being stuck. It doesn't "feel right", though. I feel that I am missing something and I just need to be made aware of it to get a a-ha moment.


When I use the var type like this:
C#:
var result = hotelService.RoomBookingStatusById(0345);
Then I don't get an error but I can't load it into a XmlDocument. Then I get this error

C#:
argument 1 cannot convert from 'HotelService.hotelService.BookingPayment[]' to 'string'

I thought the service would return an XML.
 
(I just copied the BookingPayment class)
That makes a different type than what the service proxy returns.
var result = hotelService.RoomBookingStatusById(0345);
Then I don't get an error but I can't load it into a XmlDocument.
Why would you? Service proxy handles all translation to and from xml.
When you use 'var' in C# the variable type is inferred from what is assigned, meaning it saves you declaring the variable type yourself. You can see the type when you mouse over the variable in VS.
Just use it as it is, when the call returns the variable contains a List<BookingPayment> as defined in the proxy. You can for example loop through it and for each item access the BookingPayment properties:
foreach (var item in result)
{
    var type = item.RoomType;

}
 
That makes a different type than what the service proxy returns.

Why would you? Service proxy handles all translatation to and from xml.
When you use 'var' in C# the variable type is inferred from what is assigned, meaning it saves you declaring the variable type yourself. You can see the type when you mouse over the variable in VS.
Just use it as it is, when the call returns the variable contains a List<BookingPayment> as defined in the proxy. You can for example loop through it and for each item access the BookingPayment properties:
foreach (var item in result)
{
    var type = item.RoomType;

}

Ah, I thought it returned an XML and you had to parse it. I was so hung up on it that just using var and for each to go through it didn't strike my mind. I didn't know the service proxy handled all the translation to and from XML for me. Thank you!

Now I need to play around with this some before trying to use a service for payments. That is the goal, to be able to consume a transaction web service in a website where I need to encrypt the data and stuff before sending the request.
 
Just a sidenote, but do not go the route of trying to "encrypt the data and stuff before sending the request" all by yourself. Use transport-layer TLS with a proper certificate, which is fully supported by ASP.NET and IIS, and ensures that the connection is properly encrypted.
 

Latest posts

Back
Top Bottom