Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 110 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
110
Dung lượng
6,33 MB
Nội dung
That’s pretty much all there is to Web Services from an implementation perspective in .NET. .NET deals with all of the plumbing that was discussed in the first part of this chapter (SOAP, WSDL, and so on), which means that all there is to do is add properly decorated methods to the service. A Realistic Example Although the previous example was very easy to implement, it doesn’t demonstrate a real-world applica- tion of Web Services. Let’s take a look at a more realistic example by building a Web Service that sends out a richer set of data from a database instead. For the sake of example, imagine that a third-party provider hosts the site. The SQL server is behind a firewall, and the IIS server is in a demilitarized zone—a safe, though exposed, network position. This is illustrated in Figure 23-6. To get the data from your site to the remote site, call a Web Service on the remote Web server from your intranet. Since the SOAP envelope is sent via HTTP, the firewall will allow it through, and ADO.NET on the IIS server will handle the actual database manipulation. The remote firewall will allow database calls only from the IIS server, and the data will be updated safely because of the security. In real life, the class file GetCustomers would be local to your intranet server, and the database file would be an SQL server on a second PC. Across the Internet, as shown in the diagram, the Web Service would be on an IIS server sitting outside the network firewall. The DLL that actually provides the data functions would be on an application server inside the firewall, and the database would again be on a separate machine. For this application, though, you will create a Web Service that will expose the Customers table from the sample Northwind database, across the intranet, that will then later be consumed by a Web application. Just remember that Web Services are not only about exposing simple values, but also about exposing a richer dataset of values such as entire tables from a data store (for example, SQL Server). Start this example by first creating a new Web Service project in Visual Studio called MyWebService. Using Visual Studio 2005 to Build Web Services The Visual Studio 2005 IDE shows a marked improvement from the add-ins provided for Visual Studio 6 in the SOAP Toolkit. For instance, Web Services are shown as references on a project, rather than in a separate dialog box. The discovery process, discussed later, is used to its fullest, providing much more information to the developer. In short, it is nearly as easy to consume a Web Service with Visual Basic as it is to use DLLs. 848 Chapter 23 26_575368 ch23.qxd 10/7/05 11:11 PM Page 848 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Figure 23-6 7. Update operation for publishing Enterprise data to remote server Firewall 5. Call routed to ISP via HTTP The Internet 4. Web Service makes call to ISP for update Local Enterprise Server 3. DLL provides Web service with data 2. SP Returns Recordset 0. Get operation for retrieving data from Enterprise database SQL DB SQL DB 8. Remote Database uploaded with Enterprise data Firewall The DMZ 9. Web site visitors see current Enterprise data Remote Internet Server 6. Web Service Calls Update DLL 849 XML Web Services 26_575368 ch23.qxd 10/7/05 11:11 PM Page 849 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Produce a Typed DataSet For simplicity, you’ll use Visual Studio to first create a typed DataSet, which will be returned from the WebMethod that you will later produce. This IDE enables you to quickly and easily create the needed data access without having to dig through lots of ADO.NET code. To do this, right-click the MyWebService project in the Solution Explorer and select Add➪ Add New Item. One of the options is a new DataSet. Change the name of this file to MyDataComponent.xsd. This creates a typed DataSet on the fly, and it’s already strongly typed. In addition to this, Visual Studio will request to place this file in the App_Code directory of your solution. Confirm this request, because having it in the App_Code directory allows for programmatic access to the DataSet (shown in Figure 23-7). Figure 23-7 Once created, the MyDataComponent.xsd file will open itself in Visual Studio. This file will appear as a blue screen in the Document window. In addition to the file opening up, the TableAdapter Configuration Wizard will also open up, as shown in Figure 23-8. Figure 23-8 850 Chapter 23 26_575368 ch23.qxd 10/7/05 11:11 PM Page 850 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com The first step in the TableAdapter Configuration Wizard is to establish a data connection. If there is not a connection already in place, create a new connection by clicking on the New Connection button. Using this dialog, make a new connection to the sample Northwind database in Microsoft’s SQL Server. Once the connection is in place, you will be able to see that the wizard will use the System.Data .SqlClient provider. Click the Next button, which calls up a dialog that enables you to pick the com- mand type that you are going to want to work with. Typically, the options are working with either direct SQL commands, existing stored procedures, or stored procedures that you can create directly in the wiz- ard. For this example though, choose the first option: Use SQL Statements. The next page in the wizard asks for the query that you want to use to load the table data. Input the following: SELECT dbo.Customers.* FROM dbo.Customers Clicking the Next button s results in a page that will allow you to pick the methods that the wizard will create. These are the methods used in your Web Service to load data into datasets for transmission. In this case, just accept the default and click the Next button again. Now you will be at the last page of the wizard. This final page just shows the results of all the actions taken (shown in Figure 23-9). Figure 23-9 851 XML Web Services 26_575368 ch23.qxd 10/7/05 11:11 PM Page 851 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Once you click the Finish button, notice that the design surface of the MyDataComponent.xsd file changes to reflect the data that comes from the Customers table of the Northwind database. These results are shown in Figure 23-10. Figure 23-10 The typed dataset is now in place and ready to use by the Web Service. Looking at the results on the design surface of the .xsd file, you can see that indeed the typed Customers dataset is in place, but in addition to this there is also a CustomersTableAdapter object with Fill() and GetData() methods in place. Build the Service Right-click Service.asmx from within Solution Explorer in Visual Studio, and select View Code. Rename the HelloWorld function to GetCustomers. From here, simply retrieve data from the CustomersTableAdapter that was created when you created the .xsd file earlier. <%@ WebService Language=”VB” Class=”Service” %> Imports System.Web Imports System.Web.Services Imports System.Web.Services.Protocols <%@ WebService Language=”VB” Class=”Service” %> Imports System.Web Imports System.Web.Services Imports System.Web.Services.Protocols <WebService(Namespace:=”http://localhost/mywebservice”)> _ <WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _ <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _ Public Class Service Inherits System.Web.Services.WebService <WebMethod()> _ Public Function GetCustomers() As MyDataComponent.CustomersDataTable Dim da As New MyDataComponentTableAdapters.CustomersTableAdapter() 852 Chapter 23 26_575368 ch23.qxd 10/7/05 11:11 PM Page 852 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Dim ds As New MyDataComponent.CustomersDataTable() da.Fill(ds) Return ds End Function End Class Right-click the Service.asmx file in Solution Explorer and select View in Browser. If there are no errors, a simple screen listing GetCustomers as the sole method of the service appears. Click the Service Description line, and you’ll get a screen like that shown earlier in the “Web Services Description Language” section. Consuming the Service For this consuming application, provide a Web application called WSCustomers by creating a new ASP.NET Web site project with that name. The first step is to create a Web reference to the remote XML Web Service. Adding a Web Reference The only bit of magic here is the adding of a Web reference to the project with the Visual Studio IDE. As discussed later, you are really creating a proxy based upon the WSDL file of the service and referencing the proxy in the project, but the IDE makes this all very easy. To create the proxy that is needed by the consuming application, right-click the WSCustomers project in Solution Explorer and select Add Web Reference from the list of options. In this form, enter in the WSDL file of the Web Service that you are wishing to make a reference to. If the Web Service is a .NET Web Service (with an .asmx file extension), simply input the URL of the .asmx file and nothing more because the wizard will know to put ?wsdl at the end of the input. If you are referencing a Java Web Service, then place the URL for the .wsdl file in this wizard. Enter the URL of your service in the address bar. (This would be at the ISP in your real-life scenario, but if you’ve been following along it’ll be http://local host/mywebservice/service.asmx . If you are using IIS, click on the Web Services in this solution link). The dialog box should appear as displayed in Figure 23-11. Figure 23-11 853 XML Web Services 26_575368 ch23.qxd 10/7/05 11:11 PM Page 853 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com The service description page you’ve just seen when you built your service appears in the left pane of the wizard, with .NET specific information in the right. Click the Add Reference button at the bottom of the window to add this to the project. The service appears in a new folder in Solution Explorer, Web References, as illustrated in Figure 23-12. Figure 23-12 Building the Consumer The COM architecture continually promised “one line of code” to generate great results. Web Services live up to the promise, minus the declarations. Now the only thing left to do is call the referenced Web Service and pass the generated DataSet. Compared to the scores of lines of XML needed to pass the DataSet in the existing Microsoft technologies, this is a breeze. The first step of your consuming an .aspx page is simply to make a reference to the proxy that Visual Studio created and then call the GetCustomers WebMethod through this instantiated object. The results pulled from the GetCustomers method will then be displayed in a GridView control, which should be placed on the Web form. There are a couple of ways to achieve this. The first method is to use an ObjectDataSource control, which does the work of invoking the GetCustomers WebMethod and then displaying the results in the GridView control. (The second method, discussed a bit later, is to manually write the required code.) To work through this example, drop a GridView and an ObjectDataSource server control onto the design surface of the Web Form. Open the smart tag of the ObjectDataSource control and select the option to Configure Data Source. You will then be presented with the Configure Data Source Wizard. In the first page of this wizard, uncheck the Show Only Data Components check box and select localhost.Service from the drop-down list. Click the Next button to choose the Select method that for ObjectDataSource control to use (shown in Figure 23-13). From the drop-down list on this page of the wizard, select GetData(), returns CustomerDataTable. Once this is selected, click the Finish button to progress to the next step of binding the GridView control to the returned dataset from this ObjectDataSource control. 854 Chapter 23 26_575368 ch23.qxd 10/7/05 11:11 PM Page 854 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Figure 23-13 Now, focus on the GridView control. In configuring this control, open the control’s smart tag and select ObjectDataSource1 as the data source control for this control from the drop-down list. Notice that once you do this, the GridView control expands to include all the appropriate columns from the Customers table of the Northwind database. Then in the same smart tag, enable paging and sorting by selecting the appropriate check boxes. The end code that is generated by Visual Studio is shown here: <%@ Page Language=”VB” %> <html xmlns=”http://www.w3.org/1999/xhtml” > <head runat=”server”> <title>Consuming Application</title> </head> <body> <form id=”form1” runat=”server”> <div> <asp:GridView ID=”GridView1” Runat=”server” DataSourceID=”ObjectDataSource1” AutoGenerateColumns=”False” AllowPaging=”True” AllowSorting=”True”> <Columns> <asp:BoundField HeaderText=”CustomerID” DataField=”CustomerID” SortExpression=”CustomerID”></asp:BoundField> <asp:BoundField HeaderText=”CompanyName” DataField=”CompanyName” SortExpression=”CompanyName”></asp:BoundField> <asp:BoundField HeaderText=”ContactName” DataField=”ContactName” 855 XML Web Services 26_575368 ch23.qxd 10/7/05 11:11 PM Page 855 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com SortExpression=”ContactName”></asp:BoundField> <asp:BoundField HeaderText=”ContactTitle” DataField=”ContactTitle” SortExpression=”ContactTitle”></asp:BoundField> <asp:BoundField HeaderText=”Address” DataField=”Address” SortExpression=”Address”></asp:BoundField> <asp:BoundField HeaderText=”City” DataField=”City” SortExpression=”City”></asp:BoundField> <asp:BoundField HeaderText=”Region” DataField=”Region” SortExpression=”Region”></asp:BoundField> <asp:BoundField HeaderText=”PostalCode” DataField=”PostalCode” SortExpression=”PostalCode”></asp:BoundField> <asp:BoundField HeaderText=”Country” DataField=”Country” SortExpression=”Country”></asp:BoundField> <asp:BoundField HeaderText=”Phone” DataField=”Phone” SortExpression=”Phone”></asp:BoundField> <asp:BoundField HeaderText=”Fax” DataField=”Fax” SortExpression=”Fax”></asp:BoundField> </Columns> <asp:ObjectDataSource ID=”ObjectDataSource1” Runat=”server” SelectMethod=”GetData” TypeName=”MyDataComponentTableAdapters.CustomersTableAdapter”> </asp:ObjectDataSource> </div> </form> </body> </html> Once it is complete, just build and run the page. That’s it, there is a table in the Web form with all the data from a remote SQL server that can be paged and sorted —and you didn’t have to write any code to achieve this functionality! The end page results are shown in Figure 23-14. Now look at doing the same thing, but instead spend a little time writing some code. This is a good move because it offers more control over the situation (if desired), and it teaches you more about what is going on. Start by creating a page that only includes a GridView control. From here, you get at the data that comes from the Web Service in the Page_Load event. This is illustrated in the following example: <%@ Page Language=”VB” %> <script runat=”server”> Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Dim ws As New localhost.Service GridView1.DataSource = ws.GetCustomers() GridView1.DataBind() End Sub </script> <html xmlns=”http://www.w3.org/1999/xhtml” > <head runat=”server”> 856 Chapter 23 26_575368 ch23.qxd 10/7/05 11:11 PM Page 856 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com <title>Consuming Application</title> </head> <body> <form id=”form1” runat=”server”> <div> <asp:GridView ID=”GridView1” Runat=”server” AllowPaging=”True” AllowSorting=”True”> </asp:GridView> </div> </form> </body> </html> Figure 23-14 The first line of code contained in the Page_Load event instantiates the proxy object that was created for you. The next line assigns the DataSource property of the GridView control to the result set from the GetCustomers() WebMethod call. Finally, close everything by calling the DataBind() method of the GridView control. By compiling and running the XML Web Service, you are able to pull out of the database the entire Customers table from the Northwind database. A returned dataset contains a wealth of information, including: 857 XML Web Services 26_575368 ch23.qxd 10/7/05 11:11 PM Page 857 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com [...]... since its inception that can be fulfilled with Web Services and XML It won’t stop there, though The power of listening devices will bring Web Services development into user-to-user markets from business-to-business ones It sounds far-fetched, but the hope is that you can see how the power of Web Services on NET could make this possible SOAP isn’t just about replacing the RPC architecture already out... rewrite it from scratch when you upgrade to Indigo Figures 2 4-1 and 2 4-2 show how these concepts fit together 873 Chapter 24 Application Domain client Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Hello() transparent proxy Invokes() real proxy Process Message() formatter envoy sink Serialize() channel Figure 2 4-1 Figure 2 4-1 shows how a client calls the Hello method of a transparent... to the same server-side Activated object If this is the case in your application, then you’ll have to deal with shared data and synchronization issues as discussed in Chapter 22 877 Chapter 24 While long-lived, stateful, per-client objects can be useful in some specialized scenarios, they are not commonly used in most client/server or n-tier application environments By storing per-client state in an... they - http://www.simpopdf.com Merge and Split Unregistered Versioncan run even when no user is logged in to the server It is also possible to have any NET application be a remoting host, which can allow you to emulate ActiveX EXE behaviors to some degree This last technique is most commonly used when creating peer-to-peerstyle applications A channel is a way of communicating between two machines Out-of-the-box,... directories within the hierarchy, so that the dynamic discovery process knows where not to go to gather information about Web Services: In order for the dynamic... discovery services for the business-to-business (B2B) Web Services market, hosted throughout the world For instance, software companies can build applications that customize themselves based on services information in the UDDI registry on installation Online marketplaces can back their market sites with UDDI technology to better serve the growing needs of B2B value-added services Future services planned... concern Fortunately, the wire transport of choice — HTTPS — provides a 128-bit solution to these problems Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Also, as mentioned earlier, now by using Microsoft’s Web Services Enhancements (WSE) capabilities, you can easily apply security standards such as WS-Security to your SOAP messages The Secure Sockets Layer The Secure Sockets... System.Web.Services.Protocols namespace include (among others): ❑ ❑ HTML forms ❑ HTTP request and response ❑ MIME ❑ Server ❑ 860 Cookies per RFC 20 19 SOAP, including SoapException, the only error-handling mechanism XML Web Services ❑ URIs and URLs ❑ XML Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com The System.Web.Services.Protocols namespace is particularly handy for managing the connection type... about the network as the platform Summar y This chapter looked at the need for an architecturally neutral, ubiquitous, easy-to-use, and interoperable system to replace DCOM, RMI, and CORBA It discussed how Web Services fill the gaps successfully because HTTP is used as the language-independent protocol, XML is its language (in WSDL) and transport mechanism, and SOAP allows you to package messages for... System.Web.Services.Description, System.Web.Services Discovery, and System.Web.Services.Protocols namespaces 8 69 Chapter 23 Next, it took a high-level look at some of the technologies supporting Web Services — namely DISCO and UDDI — before briefly covering security in Web Services Finally, it talked about some of the downsides to using - http://www.simpopdf.com Simpo PDF Merge and Split Unregistered Versionany distributed . actions taken (shown in Figure 2 3 -9 ). Figure 2 3 -9 851 XML Web Services 26_575368 ch23.qxd 10/7/05 11:11 PM Page 851 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Once. that is generated by Visual Studio is shown here: <%@ Page Language= VB %> <html xmlns=”http://www.w3.org/ 199 9/xhtml” > <head runat=”server”> <title>Consuming Application</title> </head> <body> <form. xmlns=”http://www.w3.org/ 199 9/xhtml” > <head runat=”server”> 856 Chapter 23 26_575368 ch23.qxd 10/7/05 11:11 PM Page 856 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com <title>Consuming