Results 1 to 6 of 6

Thread: Consuming web service ... stuck :(

  1. #1
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2017
    Posts
    5
    Rep Power
    0

    Consuming web service ... stuck :(

    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?

    Code:
    HTTP/1.1 200 OKContent-Type: text/xml; charset=utf-8
    Content-Length: length
    
    <?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>int</RoomId>
              <RoomType>string</RoomType>
              <BookingStatus>string</BookingStatus>
            </BookingPayment>
            <BookingPayment>
              <RoomId>int</RoomId>
              <RoomType>string</RoomType>
              <BookingStatus>string</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..

    Code:
           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.

    Thanks!

  2. #2
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2011
    Posts
    1,778
    Rep Power
    137
    What is the data type of your 'result' variable? Put another way, what is the return type of RoomBookingStatusById?

  3. #3
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2017
    Posts
    5
    Rep Power
    0
    Quote Originally Posted by jmcilhinney View Post
    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
    Code:
        public class BookingPayment
        {
            public int RoomId { get; set; }
            public string RoomType { get; set; }
            public string BookingStatus { get; set; }
        }
    Here is the entire Web service
    Code:
       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.
    Code:
    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:
    Code:
    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

    Code:
    argument 1 cannot convert from 'HotelService.hotelService.BookingPayment[]' to 'string'
    I thought the service would return an XML.

  4. #4
    .NET Framework
    .NET 4.5
    Join Date
    Apr 2011
    Location
    Norway
    Posts
    397
    Rep Power
    102
    (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;

    }
    [xcode=c#] code here [/xcode] - see bbcode list or use formatting buttons in posting editor.

    Visual Studio Community 2017

  5. #5
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2017
    Posts
    5
    Rep Power
    0
    Quote Originally Posted by JohnH View Post
    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.

  6. #6
    .NET Framework
    .NET 4.5
    Join Date
    May 2016
    Posts
    8
    Rep Power
    0
    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.

Similar Threads

  1. Question Create an ASMX web service and a web client to simulate a stock exchange
    By Hassan258 in forum C# General Discussion
    Replies: 1
    Last Post: 04-17-2017, 6:24 PM
  2. Question WSDL Web Service Configuration
    By onur in forum Web Services
    Replies: 0
    Last Post: 06-24-2016, 6:31 PM
  3. web service + win service
    By ytzkey in forum Windows Services
    Replies: 1
    Last Post: 08-08-2014, 4:12 AM
  4. Question Web Service
    By Contissi in forum Web Services
    Replies: 3
    Last Post: 04-17-2012, 11:19 AM
  5. dataset to Web Service?
    By shersu in forum Web Services
    Replies: 0
    Last Post: 10-30-2011, 3:59 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •