Watch videos with subtitles in your language, upload your videos, create your own subtitles! Click here to learn more on "how to Dotsub"

[How Do I:] Create and Call a Simple Web Service in ASP.NET

0 (0 Likes / 0 Dislikes)
  • Embed Video

  • Embed normal player Copy to Clipboard
  • Embed a smaller player Copy to Clipboard
  • Advanced Embedding Options
  • Embed Video With Transcription

  • Embed with transcription beside video Copy to Clipboard
  • Embed with transcription below video Copy to Clipboard
  • Embed transcript

  • Embed transcript in:
    Copy to Clipboard
  • Invite a user to Dotsub
[♪ digital music ♪] [Microsoft] [] Hi, my name's Chris Pels. In this video we're going to learn how to create a web service in ASP.NET. To get started we're going to create what's called a web project. So I'm going to go up to new project, and underneath the web area of Visual Basic there is a project type called ASP.NET web service application. And this is used to create web services that you would host in IIS much like you would host an ASP.NET website. So we'll create this service, and I'm just going to call it, "WebServiceVB," because the sample code that you're going to get is going to have everything we'll do here in Visual Basic in C-sharp as well. So this project template creates a new web service project— in this case called, "WebServiceVB," and it automatically adds a Service1.asmx, which is much like the default .aspx that the standard website application project plate ads. Let's take a look at some of the aspects of Service1, our sample web service that was created by the project template. That will give us a better understanding of some of the aspects of how web services are implemented and created here in ASP.NET. The first thing we want to look at is up here on lines 1 and 2 there are 2 different named spaces that are imported. One is the "Services" namespace under web, and that contains the major classes related to web services. And there is also a namespace called, "Protocols," which is the various protocols that are used to transmit data to and from a web service to the calling entity. And then here's the class that represents our web service, and notice that it inherits from Web.Services.WebService, which is the base class upon which all web services are based. So by inheriting from the web service class it gives the Service1 class all the basic functionality That is needed for a web service in ASP.NET. Then within the class we have—down here—a function called, "Hello World," it's just a little template that's a very simple function. And by simply putting the web method attribute up here on line 11 and associating it with this "Hello World" function that exposes the "Hello World" function as a web method within our web service. Now the other thing we need to look at are some of these attributes up here on lines 5 and 6 that are applied to our Service1 class. And the first attribute up here on 5 is the WebService attribute, and notice there's a namespace of "" associated with the web service. And what the namespace does in our web services attribute is this specifies—much like a namespace does in a class— in Digital Basic or C-sharp a way of partitioning this web service into its own area so that none of the methods or other aspects of our web service collide with other web services. And this is just put here by the project template as a default name, and this is something you typically want to change. So in this particular sample what we'll do is we'll call this—let's call it, "" So this will differentiate our web service from other web services and this—it's probably important to use something that will be unique here such as your company URL or something similar. And one point of confusion sometimes on these namespaces and—in fact—namespaces in general in XML or web services is that this namespace is not a URL that is actually pinged or visited in any way in connection with a web service. It's just a unique identifier that separates the names—rather the web service so it doesn't collide with other web services. And one thing to remember about web services is that the contents of a web service—when it's transmitted from the server where our web service is located down to the client and from the client back to the server is transmitted as XML. And what actually happens to this namespace in our web service attribute is this is the XML namespace used within that XML structure. And if you're familiar with XML and XML structures you know that namespaces in XML are used to compartmentalize or differentiate different aspects of an XML document so that you don't have collisions or ambiguity within the document as to what certain pieces of data represent. Much like a namespace that allows you to have 2 classes in VB or C-Sharp that have the same name, but it you put them in different namespaces then they exist as separate entities. Now there's a couple other aspects that you can set inside your web service attribute and—let me just—in fact—we'll put them down at the end because then we'll get the Intellisense and you can see the choices. So if we put a comma here and we get the little Intellisense tool tip, you can see that in addition to the namespace, which we specified already you can specify a name and a description. So let's put in a description, [typing] and the description is something that appears when a potential user of your web service queries the web service to find out more about it, and we'll see how that works down the road here in just a minute. But we'll put, "This is a sample service for How Do I videos." Now the other piece of information that we could put here in our attribute is the name, and the name—as implied—is the name of the web service, and by default that is the same as the class name. If we go back over here, we remember that the name of our class is," Service1," so if we don't put name up here in the attribute, or a value for name, what's going to happen is that the name of our web service is going to be, "Service1," but if we put, "Myservice," up here what's going to happen is the name of the service is going to be mapped to, "Service1," which is the class name. And the other attribute that we have on our Service1 web service is called the, "WebServiceBinding," attribute, and this defines how the web service information will be transported over the internet. And in this particular case the default value that's put in by the project template says that this particular web service will conform to the WSI web service interoperability that is profiles—the basic profile 1.1. So those are the basic pieces of our Service1 web service, and essentially the way to think of a web service after you've seen the different pieces is it's really a class in 1 or more methods that are exposed and accessed over the internet. And you do that by adding attributes either to the class or service itself or attributes to particular methods and expose them as what are called web methods. So at a high level you can really think of web service as a class that's exposing functionality that can then be used by their aspects of— not only your application but other applications that access your web service over the internet. Now once you've created your web service one of the things you want to do is test it, and Visual Studio and ASP.NET provide a nice little test harness right here in the development environment that allows you to test the web service. So let's use that to test our little, "Hello World," web method here in Service1. And the way you do that is you just fire up the .asmx file the same way you would start a web page—an .aspx page—in Visual Studio to test your website. So you can either make this the start page and then go up and run it in the debug or non-debug mode. Or you can just view it in the browser, which is the non-debug mode. So we'll start it up that way. And what Visual Studio does is it reads the information related to the attributes that we just placed on our service class, and it generates a test page where we can test the methods in our service. In this case we only have 1 method called, "Hello World," so that's the only one listed here. So let's go ahead and test that. Click on "Hello World" and here it opens a secondary page, which is specifically for testing the "Hello World" method. And to test it all you need to do is you click on this "Invoke" button. It runs the service and shows us the result set from the service as an XML structure because that is how the service is returning it to us. And in this case all the service is doing is returning the text string "Hello World" so our XML structure is pretty simple just indicating that we have a string tag here with "Hello World" text inside of it. Now if we go back over to the test page you'll see that one of the things the test harness does is also provide you with some examples of invoking your service with SOAP 1.1, SOAP 1.2, and HTTP post. Now if we go up here and go back to the list of operations within our service one of the other things that it shows you is the WSDL for the service, and that's called the service description. Now if you're not familiar with WSDL, WSDL stands for web service discovery language. And this is a standardized method of essentially documenting how your service works. And if you want to call someone's web service and you don't know anything about it if you query the service with ?WSDL on the end of the service URL, which is what you can see up here in the address bar— that's what the Visual Studio test page has done automatically for us we could have just typed this in. What happens is then the service returns the WSDL file, which—as you can see—is also an XML structure that has various elements in it that describe all the aspects of how the service works. And if you're interested in learning more about WSDL and how that—what type of information is inside this WSDL structure then what I'll do is I'll put a URL or 2 in the sample code documentation that I provide with the download, and you can go off and follow those URLs and take a look at the information on WSDL in a little more detail. So this is an example of the test harness for our "Hello World" service. Now let's go back and look at the code a little bit more. So we've seen how to create a simple web service and then test it using the Visual Studio test harness. Now let's create a second service and look at some of the other ways that you can create methods and some of the things that you can do with those methods inside a web service. So what I'm going to do is add a second web service to our web service project. Just go up to "New item." There's a web service type in the templates so I'll use that, and we're going to call this, "CustomerService." [typing] and you don't need to call your services with the name "service." that's just a little convention that I use because it helps differentiate them as a service when you're calling them. So here this creates our standard template that we saw with Service1. Let me go up and change the namespace, and again I'll say, "" as the namespace. We'll leave the profile as the basic profile, 1.1. And I'm going to put a name for our service. And this one is going to be called "Customer related services." And that's good for our web service attribute. Now let's go down to the web method, and we're going to get rid of the default "Hello World" that was created. And we're going to create a method called, "Get customer name." And that's going to take an inbound argument ID as an integer— the ID of the customer. And then what we're going to do is— essentially what we would do here is we might simulate using this ID value to go off of the database and get the customer name out of the database. So I'll just put a comment here about, "Get customer name from the database." And in this particular example, we won't do that we'll hard code it, but that just gives you an indication of what you might be able to do here in a web service method. So we're going to return the name of the customer, and in this case we'll return "Microsoft Corporation" as the name of the customer. And one thing I'd like to point out here as we're doing this— again a pretty simple method of getting the customer name and that is one of the decisions that you're going to have to make is whether or not you put logic here in your—right here in your web methods, and by that I mean there is nothing to prevent you from putting quite a bit of logic right here in the, "Getcustomername," web method in terms of—you could create a sequel command object in the connection string and all that kind of functionality to get the customer name and put it right here in the web method. However, it's probably a better practice if you were to create a separate class such as a customer class for data access and then the only thing you'd put here in your web method would be the call to class, and the class would actually go off and perform the data access. You really don't want to get your web methods to be heavy on the actual logic of the operations they have to perform because then that restricts you from either using the separate class for other aspects of your website and it'll make it harder to maintain. So that's just a little design tip. So now that we have this method let's add a second one in here. So I'm just going to go down and create another public function called, "InsertCustomer." And in this particular one we're just going to take a name— let me just call it, "custName as a string." And it returns a Boolean indicating whether or not we were successful in inserting the new customer. And here you, "Call data access class to insert the new customer." And in this case, we're just going to return true that the customer was inserted to simulate that data access operation occurring successfully. So now that we have 2 different web methods and—in fact—we need to take this web method attribute and put it here so this "InsertCustomer" method is also a web method and can be accessible through our service. And that brings up another point and that is when you have methods inside of a web service class such as this customer service class, they don't all have to be exposed publicly as web methods. In fact, we could have left this attribute off of "InsertCustomer" and then "InsertCustomer" would be available for calling from another method such as the web method "GetCustomerName" if there was a reason to do that. But again this is kind of a designed approach and that is that typically you don't have too many non-web methods inside of your service. You may have some helper methods, and you don't want bloat this customer service class with all kinds of non-service-related functionality. So now that we have a couple methods here in our customer service class let's fire that up with the test harness. And what I wanted to do by creating several methods is just to show you a little different slant on the test harness here. And as you can see, we're getting some of our descriptive information being pulled and we have both of our methods here. Now we go to "GetCustomerName." What's a little different here from the first one is that rather than just having the invoke button you also see that it provides us with the ability to put in values for the in-bound parameters for our service calls. So if I get customer number 34 and then we invoke it what happens is if it had gone off to the database for real, it would have looked up customer number 34 and returned the name of the customer. And the same thing will be true for our other method, which is the "InsertCustomer," which provides us with the ability to type the customer name and we'll just put "Test Customer" here. Click invoke and back comes the return value of Boolean true in this case indicating that we were successful in inserting a customer. So you can really do a lot of different things here in your web services. In this case, if we were to fully flush out this example we could do just about anything data access related for our customers here that we could do in a regular class that we would reference as an assembly in our website. We could insert customers, we could get customers, we could update customers. And one of the last things I want to show you here in relation to some of the things that you can do in a web service class is I'm going to go over here and I'm going to add another project. And what I'm going to add is—let me go up here— and we're going to add a class library project. And we're going to call this "Customer" and let me just make sure that gets in the VB folder for your sample code download. So here we have another class called "Customer"— And let me just call this class "Customer," and let's just put a public property, and we'll call it "CustomerName as string." And the get will just return—oh no let me—I was just going to hard code it, but— tell you what we'll do, we're going to go right up here, and we're going to go "private_customername as string." We'll just really flush this out a little bit more and we'll "return_customername." And then we'll go down here and we'll say, "customername = value," and that will set the value of the property. And that's all we'll do here—now if this was a full-fledged customer class, you could flush it out with all the other properties of a customer such as their ID and their address. Now I want to go back over to our web server class—web service class—I should say. And I want to add a reference. And what we're going to do is just reference the customer class library that I created a moment ago. And let's just go to projects and reference that. So now it's included as a reference in our web service project. And let me just go back over here—I see that— let me just rename that class so things are a little more clear. And now let's go back over into the customer service web service and I'm going to go down, and I'm going to add a new function called "GetCustomer." And let's just put the "custID" as an integer. And what we're going to return here is an instance of the customer class. And do that so they all just—"dim cust" as an instance of the customer class equals new. Customer.Customer. And we'll just say, "CustomerName equals"—in this case— Sun Microsystems. [typing] Now we'll just type that in there. And then we'll return that instance of the object. We'll make this a web method—just go up and copy the attribute and paste it on our new function. And so let's fire this up and see what we get in the test harness. So we'll go over and view the browser, and—in fact— what I'm going to do this time is I'm going to set it up as the start page and go back over to our customer class— and I'm going to put a breakpoint right here on line 6. So now when we're in our "CustomerService" web service I'm going to start it with debugging. And this time the test harness comes up, and there's our new method, "GetCustomer." We'll click that, enter the customer ID. We'll just put 45. We'll invoke it. And what happens is we hit the breakpoint in our customerName class. We're in customerName, line 6. And what's happened is we've come in here to our web service we're instantiating the new instance of the customer class, and we're setting the value of the customerName property. So what we've been able to do is actually put a break point in our code that is hit when we're running the web service and the test harness. So this is a great debugging tool because you actually can run your web services and debug them. So we'll let this run through back to the test page, and you can see that what we're getting here in the test page is an XML structure that represents our customer class with a customer name property. So what you can do here when you're creating your web services is that you actually can get—let me just shut this down for a sec— there we go—you actually can use complex objects both as inbound parameters and as return types as long as they are serializable. And if they're serializable then ASP.NET knows how to take your class, convert it down, serialize it to XML, and send it over the wire. So this makes web services a very powerful tool for remote access of information. So far we've been testing our web service by using the test harness page right here in the web service project. Now what I'd like to do is actually go up, create a separate website, and call our web service from that new website. Just create a regular ASP.NET website. Put it in the Visual Basic sample code folder. Now here on this page—the default .aspx page— let me just add a simple label—go up and add a label to the page. Now what we need to do is from our website we need to reference our web service. So in order to do that, what you do is you go to the "Add Web Reference" choice. What that does is brings up a couple of options— we can use web services in a solution—that's what we're going to do— other web services here in the local machine. Or actually you can type any URL up here in the address box of a web service out anywhere on the internet. So we're going to use web services in the solution. It pulls up Service1 and CustomerService. CustomerService is what we want. It shows us—essentially this is the same little test harness page— it shows us the different methods that are available in case we want to try them out right here. Then you need to have a web reference name— this is essentially like a namespace in the sense that this is going to be where the proxy methods for accessing our web service are located in the web site project. We'll just leave it as "localhost," Add the reference. You can see under App_WebReferences, added several files related to our web service. Now if we go over to default .aspx— we'll go into the code behind. And let's go into—say the—PreRender event. And here we'll dimension a new variable— we'll just call it "wsCustomer," for web service customer as a new local host.customer related services. Customerrelatedservices inside this local host namespace is the set up proxy classes or methods that relate to calling our customer services. So we'll create a new instance of that then we'll take our label1.text and what we'll do in that is we'll take our wsCustomer instance of our proxy class and here we have all the methods that are in our web service object and you can see we're going to call "GetCustomerName," but what's also in here is "getcustomer" and the other methods that we created. And you also see that there's a "begingetcustomername," "begingetcustomer," and then corresponding "endgetcustomername." And these are the ability in the proxy class to call your web service methods asynchronously, which if they're long-running you might want to consider doing. But in this particular example, we're just going to call customer— or "GetCustomerName" synchronously. And so now, we can call this—what we have forgotten here is it takes the customer ID as an integer. So I'll just put a value of 33 in there. And remember we hard coded this to return the name, "Microsoft Corporation." So now we should be set to go—let me just put a breakpoint here on line 6. We'll back over and make our website the startup project. Let's start with debugging again. And this time you'll see what will happen is we'll hit the breakpoint in our PreRender event as the page starts to render. Now it's going to step down here after creating an instance of the proxy class to line 7—now it's going to call our GetCustomers web service method through the wsCustomer instance of the proxy class and return the name of the customer and assign it to the text property of our label. So we'll just let it run from here. And as you can see here we get our customer name, "Microsoft Corporation," that came from the web service call all over into our web service and back to our website. So that's both the creation of the web service over here in the web service VB web service project and the consumption of that web service in an actual website. So now we've covered the major aspects of creating a web service in ASP.NET. First we created our WebServiceVB web service type project and in that we created CustomerService web service. And inside there we had several different types of web methods that show you that you can both accept inbound parameters and return parameters of varying types including complex objects such as the customer class that we created here. And then we used the test harness built into Visual Studio to test out our web services right in the WebServiceVB web service project. And then after that—as you saw—we just created a separate website, added a web reference to our WebServiceVB web service. And then consumed that by getting the name of the customer and inserting it into a label in our default .aspx page. So I encourage you to download the sample source code that comes with this video. As I said it's available in both Visual Basic, which we used here in the demo, and in C-Sharp. And then use this code as a starting point to create some web services for your projects and consume them, either in your own website pages or you can potentially expose them for others to consume from your website. This is Chris Pels. Thanks for watching this video on how to create a web service in ASP.NET. [♪ digital music ♪] [Microsoft] []

Video Details

Duration: 33 minutes and 22 seconds
Country: United States
Language: English
License: All rights reserved
Genre: None
Views: 6
Posted by: neudesicasp on Aug 27, 2013

In this video Chris Pels will show how to create a web service in ASP.NET and then call the service from another ASP.NET web site. First, learn how to create an .asmx component which represents a web service in an ASP.NET web site. When creating the web service learn how to control several aspects of the web service functionality with web service attributes. Next, see how to test the web service in the Visual Studio web service test page. Then learn how ASP.NET allows many .NET data types to be returned from a web service. Create a business class library project and then use a class in that project as a return value from a web service by serializing the custom business object instance. Finally, see how to consume a web service from an ASP.NET web site.

Caption and Translate

    Sign In/Register for Dotsub to translate this video.