Create a simple Web Service client with WSDL in C# Visual Studio 2017

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to see how to consume a web service in c-sharp and Visual Studio 2017 so what we need is a web service with WSDL or a web service definition language and then we need to create a service reference within visual studio and then we need some place where we can invoke the web service frankly in Visual Studio it is really easy to do this one thing I really like about Visual Studio is the tight integration that it has in the ease of just consuming a web service no libraries to import or anything it's all built in it so let's start by finding a web service web service X net is a great directory of a lot of freely available web services and think about this when you want to design an app and you're thinking about integrating data from different sources I'll pick a fairly simple one I'm going to go to value manipulation unit converter and you see here there's a length distance unit converter so I open this up and there's a demo that I can run where it says change length unit now here's the trick is length value let's say one from length unit to length unit it's not very obvious what they want us to put in here because it's just a text field and I could really type in anything at all and hit invoke and it probably would say I don't understand well web services will come with this WSDL link so we can open this up and take a look at it and I've already opened it in a window here and this is a description of the API the application programming interface for the web service or in other words the things that the web service can do so it describes it in this fairly standard format WSDL web services definition language if we take a look at this we see something very interesting first of all it's all XML which we saw in a previous video series but look at the very top under WSDL types there is an inline x SD schema and if you saw the videos or you listened to my lecture on schemas this will look somewhat familiar we have a length value from length unit to length unit min occurs and max occurs remember what that means min occurs means one is required max occurs means no more than one but what I really want to point out is this simple type called length because take a look at the restriction base it needs to be a string but then you see in here there's an enumeration and remember what an enumeration means it means you have to pick something from this list so this tells us what we're allowed to use notice inches with a capital I noticed centimeters with a capital C in both of these are plural so now armed with that information we can go back and say 1 inches and then to length unit CIN 2 meters spelled correctly now those need to be in that enumeration that we saw just a moment ago and you don't always have to jump to the WSDL to find that there might be some documentation with a web service that tells you what's it what it's expecting but at worst case you can always look in that WSDL and get an idea of what it's looking for so now I choose invoke and it comes back and it tells me a double value of 2.5 4 which sounds about right there are about 2.5 4 centimeters in an inch so with this let's implement this in our website I'm going to right click and choose copy and now I'm going to run back to visual studio and I'll just make a new quick and dirty page here we'll do add web form will be fine and we'll call this web service aggregator something like that and choose ok ok so more work to come on that but first let's go ahead and add a reference to this web service then after we do that we'll go back and edit the page that we just created so I right click on connected service and I choose add service reference now I have connected service here because I created this project initially is a WCF project or a windows communication foundation project so I paste in that WSDL link and I choose go as soon as I choose go visual studio will go out look at that WSDL and interpret it and tell me I can do with it so I choose go we give it a second as it looks it finds length unit and note that it gives us a few options here length unit HTTP GET length unit HTTP POST in length unit soap let's remember the one called length unit soap and also it's going to put this in a namespace which is kind of like a category or a folder let's not go with the default service reference too if you do that you'll end up with service reference one two three four five you'll forget which is which let's give it a meaningful name like unit converter so unit converter and then length unit and I choose okay okay know one thing that it did behind the scenes is it went into my configuration on a typical Windows app that's going to be app dot config but in a web app it's going to be web config it goes into my configuration and it adds this endpoint address and we see it is called unit converter length unit soap that's the name that we're using here so we're going to need this yeah actually it is the sorry it's the name right here length unit soap we're going to need this in just a moment when we define the web service in our aspx page so just remember to go to this end point inside of web config and you'll see this length unit soap okay with that now let's go back to the page that I made just a few moments ago the web service aggregator page and let's try to make some use of this web service so web service aggregator and aspx page this is something we can view in a browser so i'm going to go to design and i'm going to go to toolbox and what we can do is we'll just grab a couple of labels rather so we'll go to oh where'd my label go god it's right there sis hiding in plain sight so this one will call unit from okay and we might want to give this a drop-down box since we know the value of the enumeration but just for the sake of time I'll go ahead and use a plain text box but yes since we know what the enumeration values are safer if we put that on the drop-down than have a user type things in anyway you know what I mean right so okay text box we're gonna call this txt unit from okay and then I'll go back to toolbox and they'll grab a new label and we'll drop that there and we'll say Unit two okay and then we'll grab another text box and here we will say we will rename this one to Unit two sorry we'll call it txt Unit two okay and then let's say add one more which is maybe like a value or a number of units so I grab my label whoops oh well too far there grab my label from the toolbox drop on to my page and we will call this one length something like that if that helps and yet one more text box okay so click on this and rename it I generally like to anything I'm going to program against a length anything I'm going to program against I like to rename the ID very early on because it makes it a whole lot easier when I'm in the code behind when I'm actually dealing with this object so unit from unit to length and then we'll grab a late button and we'll call the button convert okay so text will say convert but not convict but convert rather okay and button will say BTN convert okay and then toolbox and then label and we'll take the text out we'll say zero point zero zero just so we remember what it is and then for the label will say LVL converted value that'll work okay at this point we have our controls on the screen so we want to make something happen when we click that convert button so I go ahead and I double click the convert button which me in here to an event handler this event handler will execute when I click on the button okay now we need a reference to our web service we need to start with that namespace remember I said don't call it service one call it something you'll remember so sure enough we have unit converter okay and then a period and then we'll say length unit so client okay we'll call it client equals new unit converter length unit soap client but if you take a look at this it requires a string in one of these in one of these calls now what's that string you see the string is the end point configuration name there might be multiple ways that we can talk to a web service may be soap may be HTTP GET or post we have to tell it our preferred mechanism here now how do we do that well remember what I said about web config remember when I said we have that endpoint address thing that's what we want to use here so either one of these will work I'll go ahead and grab this one attempted to grab the lower one but we'll use the top one for now so back to back again to the code-behind and we paste this as a string there we go length unit so okay and now now we say client dot and what do we have we have if I scroll down a bit change length unit is our best option so we send in a length value is the first argument then we have to use some constants here to represent what length were going from into so let's go ahead and do change length unit okay now the first item is simply going to be what the user entered in that txt length whoops that txt length item ok txt length dot txt okay so we'll say what we'll do is we'll parse this into a double because we know that the first argument needs to be a double type not a string type so I'll say double dbl length equals double and then we'll say dot parse take a look at the signature for parse parse converts a string type to a double type so double dot parse and then we pass in the text that is in that length text box so there's our dbl length now for the sake of time I'm going to take a short cut on the other arguments I'm not going to worry about pulling them from the form in this video we can clean that up in a later video no worries so I'm going to say unit converter take a look at the intellisense that comes up here it says unit converter dot lengths so that's a real gimme here unit converter dot lengths notice how it comes up here now I press the dot on links and look at the list that comes up here so several things that we might recognize and we might well recognize them because remember it's part of this enumeration we saw in the WSDL so these two things are very much compatible that a list of essentially constants that's giving us here on links and also that enumeration that we see in the XS D and the WSDL so lengths and then I'll say dot inches whoops one moment inches like so and then we'll say we need the two units so I'll say unit converter and then once that once again we'll say a unit converter dot lengths pic like so and then I'll say u dot centimeters like so whoops so you see where we eventually want to go here we want to make a drop-down that contains that contains each of these items that the user can just pick from a drop-down so we'll eventually get there so okay looking pretty good so far so what does it not like here then okay it thinks my car okay I see I have an extra paren no problem that's an easy fix now change length unit note that that returns a variable of type double so I'll say double dbl converted length equals bill like so and now let's update the UI with the converted length so we're going to say LVL converted value dot txt equals d vo converted length where I usually enter means yes I like the autocomplete now it doesn't like putting a double data type into a text type so we can do a little bit of a hack there to get that converted I just force it in like so by forcing it into a string and I still have a red line because I've little misspelling up here so it looks like that will take care of it okay with that let's give it a try so web service aggregator I'm going to select and I'm going to play this in Google Chrome let's see what we get now remember for this video we're not using the unit from or - we've hard-coded that I'll just put in length of one and hit convert and let's watch what happens sure enough there's 2.5 4 you see that happen relatively quickly so there are two point five four centimeters in one inch what about 12 inches or one foot we hit convert 30 point four eight so you see we're able to get this data relatively quickly on near real time on this web service which is essentially freely available now of course go to their Terms of Service to see the whole the whole description but you see it's very straightforward to implement a web service in Visual Studio with C sharp so think about the other web services that are available think about the sources of data and how you can integrate together those sources of data to really make information I hope this video was helpful there will be several more that follow and I look forward to seeing you then thank you
Info
Channel: Brandan Jones
Views: 121,849
Rating: 4.7573223 out of 5
Keywords: Visual Studio 2017, .net, web service, WSDL, Service Reference, WCF project, Connected Service, Consume, implement
Id: jmJW822larw
Channel Id: undefined
Length: 14min 57sec (897 seconds)
Published: Tue Nov 07 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.