intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Get Started with XML Web Services

Chia sẻ: Bui Tuan | Ngày: | Loại File: PDF | Số trang:9

199
lượt xem
82
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Bắt đầu với 13,1 XML Web Services Tôi có nghe nói một số điều tuyệt vời về XML Web Services, nhưng tôi không biết làm thế nào để bắt đầu. Tôi phải làm gì để bắt đầu làm việc với XML Web Services?

Chủ đề:
Lưu

Nội dung Text: Get Started with XML Web Services

  1. 13.1 Get Started with XML Web Services I have heard some great things about XML Web Services, but I don't know how to get started. What do I do to get started working with XML Web Services? Technique The best way to get started with XML Web Services is to simply create your own XML Web Service and start playing with it. Visual Studio .NET makes it extremely easy to do just that. To achieve this task, you will be creating your first Web Service, which, of course, will be Hello World. Now, before you start groaning, this example will show you the basics of creating a Web Service without a lot of other fluff that gets in the way and confuses things. Creating a Web Service The way you start to create a Web Service is to choose ASP.NET Web Service for a new project. When you have done this, VS will create the Web Service project, and you will be brought to what looks like a blank Web Form, as displayed in Figure 13.3. Figure 13.3. Here is a newly created Web Service project.
  2. Note Remember: Web Services can be created in various ways. Because you are creating this one using Visual Studio .NET, you will be using ASP.NET to create it. For the most part, you will be using Web Services to provide central processes or functionality that you want to be consistent regardless of where you consume it from. This means that you will not be creating a user interface that users will see; you are basically creating a class-like interface that will provide methods (hence the use of Service1.asmx instead of *.aspx). You will use *.asmx as an entry point for your Web Service. To really get going with working with your first Web Service, you will click on the View Code icon in the Solution Explorer. When you do so, you will see the first method to create. That's right-it is Hello World. The first thing you should do now is uncomment the lines of code that read as follows: ' Public Function HelloWorld() As String ' HelloWorld = "Hello World" 'End Function The screen should look like Figure 13.4. Figure 13.4. Your first Web Service method.
  3. That's it! You have created your first Web Service method. Now it's time to test it. Testing the Web Service After you have created your Web Service, click the Start toolbar button. Visual Studio will then compile and build your Web Service. After which, Visual Studio will create a test page for your Web Service, as shown in Figure 13.5. Figure 13.5. This page that Visual Studio created gives you instructions on how to specify a namespace for your Web Service within the calling application.
  4. Note As mentioned on this Web page, when you are developing your Web Service, Visual Basic uses a temporary namespace called http://tempuri.org/. When you are going to make the Web service public, you will want to create different namespace for the Web Service. I would recommend just using the examples they give on your test Web page. One of the items you see on this test Web page is the list of operations, otherwise known as methods you created. If you click on the Hello World method, you will see another page that you will use to invoke the actual method (see Figure 13.6.) Figure 13.6. This page allows you to test your Hello World method.
  5. You also will see example code for calling the method using SOAP, HTTP GET, and HTTP POST. However, you will just be using this test page to check out the Hello World example, and you will be learning how to call methods from Visual Basic behind Web Forms in How-To 3.3. If you click on the Invoke button, you will see the following: Hello World This is the value that the method you created returns. That's it! That's all there is to the test page. Note Before telling yourself that this test page doesn't do much, remember that if you did have a problem with the code in your Web Service, the problem would have shown up here. This quick testing saves a lot of hassle of writing code that actually calls the methods and then making sure the calling code wasn't causing errors. Using the test page allows you to debug your Web Service before you integrate it. Okay, I am now off my SOAPbox. Pun intended.
  6. Looking at the Description of the Web Service Visual Studio .NET saves you from a lot of work. If you click the Back button and go to the main test page, you will see a link to Description in the top sentence. If you click on this link, you will see the SOAP code shown in Listing 13.1. Listing 13.1 http://localhost/WebService1/Service1.asmx?WSDL: Soap Definition for Your First Web Service - - - - - - - - -
  7. - - - - - - - - - - - - - -
  8. - - - - - - - - - - The really nice thing to remember is that Visual Studio .NET generates all this code for you, so you don't have to. Steps For this How-To, you will create the example Web Service that was discussed in the "Technique" section.
  9. 1. Open up Visual Studio .NET to the Start Page, with no projects or solutions opened. 2. From the File menu, choose New, Project. You will see the New Project dialog box. Highlight the ASP.NET Web Service template (see Figure 13.7.) Then click OK. You will then be given a blank *.asmx file. Figure 13.7. You will be using the ASP.NET Web Service template for this project. 3. Click on the in the Solution Explorer. You will see the commented out method called Hello World. Uncomment the lines of code that read as follows: 4. ' Public Function HelloWorld() As String 5. ' HelloWorld = "Hello World" 6. 'End Function 7. Click the Start button. You are shown the test page as described in the "Technique" ssection. Comments Although this is definitely the simplest example to be given while creating a Web Service, if you look at the description of even this simple Web Service and you see all the SOAP that is generated, you can appreciate all the work Microsoft has done to make the generation of Web Services using Visual Studio as painless as possible.
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2