1. Trang chủ
  2. » Công Nghệ Thông Tin

Assembly Language: Step-by-Step - chapter 16 pptx

87 235 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 87
Dung lượng 3,18 MB

Nội dung

example, in the case of the RenderText method, the URL to the image is required before the page can be rendered properly, so the code could be set up to wait for a returned value at the last possible moment, which would be in the PreRender method of the Web page. This allows the rest of the page processing to be accomplished while the BeginRenderText Web method is being executed. When calling the Begin method, the return value will be a WebClientAsync- Result data type that implements the IAsyncResult interface. To get the origi- nal return value from the Web method, the End method can be used. The IAsyncResult contains the following members: AsyncState. The object that was provided in the last parameter to the Begin method call. This is useful for passing an object from the Begin to the End method. This object is not required. AsyncWaitHandle. This WaitHandle type can be used to wait for an asynchronous operation to complete. The WaitAll, WaitAny, or WaitOne methods of the WaitHandle may be executed to block the current thread until this method has completed. CompletedSynchronously. This value indicates whether the Begin call completed synchronously or not. IsCompleted. This value indicates whether the asynchronous method has completed. Abort. In addition to the members that IAsyncResult provides, the Web- ClientAsyncResult also contains the Abort method. The return value must cast as a WebClientAsyncResult using the CType command to exe- cute this method. The Abort method cancels an asynchronous XML Web service request. The following code is an example of asynchronously executing the Render- Text method: Dim RenderResult As IAsyncResult RenderResult = ti.BeginRenderText( _ “Regular”, “36”, “Impact”, “My Home Page”, “LightBlue”, “Blue”, _ Nothing, Nothing) ‘Do some work .RenderResult.AsyncWaitHandle.WaitOne() Image1.ImageUrl = ti.EndRenderText(RenderResult) In this example, the BeginRenderText method requires the same parameters as the RenderText method plus an additional parameter for a callback function and another parameter for the AsynchState object. These added parameters are not used with this approach, but are used in the following callback func- tion approach. After the BeginRenderText method has been executed, the code continues to execute without waiting for the return value. Additional work can be done, 688 Chapter 16 r 430234 Ch16.qxd 7/1/03 9:06 AM Page 688 and when the additional work has been completed, the thread can be paused to wait for the asynchronous method to complete by executing the WaitOne method of the AsyncWaitHandle. Once the asynchronous method has com- pleted, a call to the EndRenderText method retrieves the return value. Asynchronous Execution Using a Callback Function The use of the callback function is best suited for situations in which the exe- cuting code never needs to pause to wait for a result, but some processing may need to be executed upon return. For example, when printing a document, there is no need to wait for the document to print before continuing the code execution, but if the printing fails, a code in the callback can be used to notify the user of the failure. When calling the Begin method, two addition parameters must be included; the callback and the AsyncState. In the previous example, these were set to nothing. In this example, a callback function will be supplied to handle the return value, and the AsyncState will be supplied to pass an object from the Begin method to the End method. The AsyncState object can be any object type, although it is usually the original object that called the Begin method. The return value will be a WebClientAsyncResult data type that implements the IAsyncResult interface, which, optionally, may be used. To get the original return value from the Web method, the End method can be used. The End method should be executed within the callback function. The following code can be used to execute a Web method asynchronously using a callback function to handle the returned value: Dim ti As TextImaging.TextToImage Public Sub RenderTextCallback(ByVal resAr As IAsyncResult) Dim i As System.Web.UI.WebControls.Image = _ CType(resAr.AsyncState, System.Web.UI.WebControls.Image) i.ImageUrl = ti.EndRenderText(resAr) End Sub Private Sub Page_Load( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load ti = New TextImaging.TextToImage() Dim callback As New AsyncCallback(AddressOf RenderTextCallback) ti.BeginRenderText( _ “Regular”, “36”, “Impact”, _ “My Home Page”, “LightBlue”, “Blue”, _ callback, Image1) End Sub In this example, a method called RenderTextCallback has been created to handle the assignment of the return value to the Image control’s ImageURL property. The BeginRenderText method requires the same parameters as the Creating Web Services 689 r 430234 Ch16.qxd 7/1/03 9:06 AM Page 689 RenderText method plus an additional parameter for a callback function and another parameter for the AsynchState object. The callback parameter is assigned an instance of AsyncCallback, which is a reference to the Render- TextCallback method. The AsynchState parameter can contain a reference to any object and is used to pass an object from the Begin to the End method. In this case, the Image1 control was passed to the End method, which allows the End method to populate the ImageURL of Image1. Building a Visual Studio .NET Web Service The balance of this chapter is devoted to building a Web service and register- ing the Web service with the Microsoft UDDI provider. Create the Project A Web service can be created by starting Visual Studio .NET and creating an ASP.NET Web service Application, as shown in Figure 16.12. This example cre- ates the Web service that was consumed in the previous session. Create the TextToImage Class A new class file, called TextToImage.asmx, has been added to the project. The TextToImage class contains the following code at the top of the file: Imports System.Web.Services Imports System.Drawing <WebService(Namespace:=”http://teachatechie.com/”, _ Description:=”<table border=’0’><tr><td> <img src=’http://teachatechie.com/GJTTVBWebServices/images/logo.gif’ Figure 16.12 Create the Web service by selecting the ASP.NET Web service. 690 Chapter 16 r 430234 Ch16.qxd 7/1/03 9:06 AM Page 690 border=’0’></td><td><font size=’4’> Glenn Johnson Technical Training<br> Text to Image Web Service.</font></td></tr><tr><td colspan=’2’> Be sure to visit <a href=’http://gjtt.com’>GJTT.com</a> for additional .NET support. If any questions, feel free to <a href=’mailto:glenn@GJTT.com?subject=Zip Code Web Service’> email me.</a></td></tr></table>”)> _ Public Class TextToImage Inherits System.Web.Services.WebService In this code, the System.Drawing namespace is used. This also requires a reference to the System.Drawing.dll file. The attribute that is called WebService has been placed before the TextToIm- age class. This attribute offers the ability to set the Web service namespace and assign a description to the Web service. The namespace assignment is a requirement for public Web services, but the Web service will still operate even if the WebService attribute does not exist. The Description property of the attribute displays on the Web page that is automatically created when this .asmx page is displayed. Notice that the description may contain HTML tags. The following methods are helper methods for the RenderText method, which performs the real work. Each method that contains a WebMethod attribute will automatically be exposed as a Web method. This is the required attribute to enable Web services: <WebMethod(Description:=”Returns a DataSet containing all “ + _ “of the settings that can be used”)> _ Public Function LoadSettings() As DataSet Dim ds As New DataSet(“TextSettings”) ‘Create the Color table. ds.Tables.Add(MakeTable(“Colors”, “ColorName”, LoadColors())) ‘Create the Font Family table. ds.Tables.Add(MakeTable(“FontFamily”, “FontName”, _ LoadFontFamily())) ‘Create the Font Styles table. ds.Tables.Add(MakeTable(“FontStyles”, “StyleName”, _ LoadFontStyles())) ‘Create the Font Sizes table. ds.Tables.Add(MakeTable(“FontSizes”, “Size”, LoadFontSizes())) Return ds End Function Private Function MakeTable(ByVal TableName As String, _ ByVal ColumnName As String, _ ByVal StringArray As String()) As DataTable Dim dt As New DataTable(TableName) Dim dc As New DataColumn(ColumnName, GetType(String)) dt.Columns.Add(dc) Dim stringValue As String For Each stringValue In StringArray Creating Web Services 691 r 430234 Ch16.qxd 7/1/03 9:07 AM Page 691 Dim dr As DataRow = dt.NewRow() dr.Item(0) = stringValue dt.Rows.Add(dr) Next Return dt End Function <WebMethod(Description:= _ “Returns an string array of colors that can be used”)> _ Public Function LoadColors() As String() Return System.Enum.GetNames(GetType(KnownColor)) End Function <WebMethod(Description:= _ “Returns an string array of fonts that can be used”)> _ Public Function LoadFontFamily() As String() Dim b As New Bitmap(1, 1) Dim g As Graphics = Graphics.FromImage(b) Dim a As New ArrayList() Dim arFamily() As FontFamily = FontFamily.GetFamilies(g) Dim x As Integer Dim FontFamilyList(arFamily.Length - 1) As String For x = 0 To arFamily.Length - 1 FontFamilyList(x) = arFamily(x).Name Next Return FontFamilyList End Function <WebMethod(Description:= _ “Returns an string array of font styles that can be used”)> _ Public Function LoadFontStyles() As String() Return System.Enum.GetNames(GetType(FontStyle)) End Function <WebMethod(Description:= _ “Returns an string array of font sizes that can be used”)> _ Public Function LoadFontSizes() As String() Dim startSize As Integer = 6 Dim endSize As Integer = 100 Dim FontSizeList(endSize - startSize) As String Dim X As Integer For X = 0 To endSize - startSize FontSizeList(X) = (startSize + X).ToString() Next Return FontSizeList End Function Each helper method is exposed as a Web method, which simplifies the task of populating drop-down boxes with valid data. Most of these methods return an array of strings, except the LoadSettings method. The LoadSettings method will execute each of the other load methods and populate a dataset with this information, which can be returned to the client as a single call. The MakeTable method is a helper method for the LoadSettings method and does not need to be exposed as a Web method. 692 Chapter 16 r 430234 Ch16.qxd 7/1/03 9:07 AM Page 692 The last Web method is the RenderText method. This method requires para- meters for the font, background and foreground color, font size, font style, and the text to render. In the following code, this method creates a bitmap and returns a string containing the URL to the bitmap. This bitmap will be cached for 10 minutes. <WebMethod(Description:=”<font color=’#FF0000’><b>” + _ “This is the main function.</b></font><br>” + _ “This returns an string containing the URL of the “ + _ “image that has been rendered. The image URL is “ + _ “cached for 10 minutes.”)> _ Public Function RenderText(ByVal FontStyle As String, _ ByVal FontSize As String, _ ByVal FontFamily As String, _ ByVal ImageText As String, _ ByVal BackgroundColor As String, _ ByVal ForegroundColor As String) As String Dim imgBitmap As New Bitmap(1, 1) Dim fStyle As FontStyle fStyle = System.Enum.Parse(GetType(FontStyle), FontStyle) Dim fSize As Single fSize = Single.Parse(FontSize) Dim strFont As Font strFont = New Font(FontFamily, fSize, fStyle) Dim str As String = ImageText Dim cBackground As Color cBackground = Color.FromName(BackgroundColor) Dim cForeground As Color cForeground = Color.FromName(ForegroundColor) ‘Get the size of the text string. If str = “” Then str = “No text defined.” Dim g As Graphics = Graphics.FromImage(imgBitmap) Dim strSize As Size strSize = g.MeasureString(str, strFont).ToSize() ‘Create the bitmap. imgBitmap = New Bitmap(strSize.Width, strSize.Height) g = Graphics.FromImage(imgBitmap) g.Clear(cBackground) g.DrawString(str, strFont, New SolidBrush(cForeground), 0, 0) Dim imgGuid As String imgGuid = GUID.NewGuid().ToString() imgGuid = imgGuid.Replace(“-”, “”) Context.Cache.Insert(imgGuid, imgBitmap, Nothing, _ DateTime.Now.AddMinutes(10), Nothing) Dim url As String If Context.Request.ServerVariables(“https”) = “off” Then url = “http://” Else url = “https://” End If Creating Web Services 693 r 430234 Ch16.qxd 7/1/03 9:07 AM Page 693 url &= Context.Request.ServerVariables(“HTTP_HOST”) url &= Context.Request.ApplicationPath() url &= “/ImageURL.aspx” Return url & “?ImageID=” & imgGuid End Function End Class The RenderText method creates the image based on the parameters that have been passed into the method. Chapter 11, “Working with GDI+ and Images,” contains a more detailed explanation of the rendering process. Once the bitmap has been created, a Globally Unique ID (GUID) is created to repre- sent this image. The dashes were removed from the GUID since they only add more length to the URL. The image is cached for 10 minutes. The last section of this method creates the URL to the cached bitmap. Creating the ImageURL Page The previous code was responsible for creating an image from the parameters supplied to the RenderText method. Because HTML image tags require a URL to the image, the RenderText creates the URL. The URL points to the ImageURL.aspx page, which expects to receive an ImageID GUID. This page will extract the image from the cache and deliver it to the client. Imports System.Drawing Imports System.Drawing.Imaging Public Class ImageURL Inherits System.Web.UI.Page Private Sub Page_Load( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load If Not Request(“ImageID”) Is Nothing Then If Not Cache(Request(“ImageID”)) Is Nothing Then Dim b As Bitmap b = CType(Cache(Request(“ImageID”)), Bitmap) Response.Clear() Response.ContentType = “image/jpeg” b.Save(Response.OutputStream, _ ImageFormat.Jpeg) Response.End() Return End If End If End Sub End Class If the image is not in the cache, a Not Found image could be created. Also, an image could be generated with any exception message as well. It’s important to 694 Chapter 16 r 430234 Ch16.qxd 7/1/03 9:07 AM Page 694 call Response.End after saving the image to the response stream, because this command will end the page’s processing to ensure that none of the .aspx page is mistakenly sent to the client. Registering the Web Service with a UDDI Registry Registering a Web service with a UDDI business registry (UBR) requires a bit of work, but once the Web service is registered, users can locate and use the Web service. This section covers the registration process at Microsoft’s UDDI site. Before starting the registration process, the Web service should be created and deployed on a server that is accessible from the Internet. Visual Studio .NET makes it easy for developers to find Web services using UDDI, and then add the Web service into a project through the Add Web Ref- erence feature. For a Web service to be visible via this feature, the Web service must be properly registered in the UDDI registry. The WSDL file must be reg- istered as a tModel, and the Web service itself must be registered as a binding that refers to your tModel. To follow the best practice and correctly register a Web service using the Microsoft Web interface at http://uddi.microsoft.com, a UDDI account must be obtained at the http://uddi.microsoft.com/register.aspx site. After an account has been registered, the following steps must be followed. Create the Technology Model (tModel) The term tModel is short for technology model. A tModel is typically used to pro- vide technical information about a programmatic interface, such as a Web Ser- vice Description Language (WSDL) file, which describes the conventions that are supported by an interface. Figure 16.13 shows the tModel screen, which contains several tabs. The Details tab contains the tModel name and description. The Identifiers tab contains a list of Identifiers, which are optional descrip- tions intended to enhance the discovery of tModels and providers in Search. The Categories tab contains categorization schemes, which are predefined sets of categories that are represented in a hierarchical fashion. Searches for tMod- els may be performed by drilling the hierarchy. Add one or more categories by selecting from available schemes. To ensure Web service visibility when using Visual Studio .NET to search for a Web service, be sure the following category is added to the tModel: Categorization Scheme: uddi-org:types. Key Name: Specification for a Web service described in WSDL. Key Value: wsdlSpec. Creating Web Services 695 r 430234 Ch16.qxd 7/1/03 9:07 AM Page 695 Figure 16.13 The tModel screen showing two tModels. The Details tab contains the tModel name and description. The Document Overview tab is an HTTP-accessible resource, such as a WSDL file or specification document, that usually contains technical informa- tion for implementing or interacting with an interface. For Visual Studio .NET Web services, this should be the URL to the Web service. The following URL points to a Visual Studio .NET Web service: http://teachatechie.com/GJTTVBWebservices/TextToImage.asmx This URL will be displayed as a hyperlink that the prospective user will select to view the Web service information. This tab also includes the ability to add Descriptions of the Web service, each in a different language. The descrip- tion will be displayed with the URL to the Web service. Add the Service Information After the tModel has been defined, the service must be defined and bound to the tModel. Figure 16.14 shows the services screen, which is available under the provider’s node. Clicking the Add Service button displays the Details tab window, which allows the service name to be entered in any languages. Ser- vice descriptions may also be entered. 696 Chapter 16 r 430234 Ch16.qxd 7/1/03 9:07 AM Page 696 Figure 16.14 The Services tab. Services must be defined here and bound to the tModel. The Bindings tab is used to bind, or connect, to a tModel. The Bindings sec- tion has three tabs: Details, Bindings, and Categories. The Details tab holds the name and description of the service. The Bindings tab holds information that represents an access point for this service. A bind- ing must be created to reference the tModel that has been created. The tModel is referenced in the Instance Info tab of the Binding. Figure 16.15 shows the link to the tModel. The Web service will not be found in a search from Visual Studio .NET until the reference to the tModel has been set. Figure 16.15 The reference from the service to the tModel must be set before the Web service will be located in a Visual Studio .NET search. Creating Web Services 697 r 430234 Ch16.qxd 7/1/03 9:07 AM Page 697 [...]... defined in Figure 16. 16 Services Service Service Key: 6ef14a9 6-7 a6 5-4 1c 7-8 cf 8-5 21f97acec3b 2 Names (per Language) Name - en Text To Image Descriptions (per Language) Description - en This is a Text to Image Service Bindings Binding Binding Key: 8c19ee3a-1e5e-414 7-8 5bf-92df58d2e738 Access Point http://teachatechie.com/GJTTVBWebServices/TextToImage.asmx Descriptions (per Language) Description - en This is... when it shows more information than required To help simplify this process, Figure 16. 16, Figure 16. 17, and Figure 16. 18 show the menu hierarchy of the required elements tModels Instance Info - to a tModel 1 3 tModel uuid:4d21fe73-c9d 1-4 b7b-a696-cdd30c44dde3 Name: Text To Image Categories Categorization Scheme: uddi-org:types Key Name: Specification for a web service described in WSDL Key Value: wsdlSpec...698 Chapter 16 My UDDI Provider Provider Key: 58eaf9c 1-3 1dd-480 3-8 c67-ffe440cba519 Name: Glenn Johnson Technical Training Providers Contacts Contact Glenn Johnson tModels Services 2 1 Figure 16. 16 The registration properties must be set up first Understanding the UDDI Menu Hierarchy The UDDI menu hierarchy... (per Language) Description - en This is a Text to Image Conversion Figure 16. 17 Second, the tModel properties must be set up Creating Web Services The registration items are set up first, as shown in Figure 16. 16 (Descriptions that contain the en label denote English.) This simply involves the creation of a provider and a contact A tModel must be set up next, as shown in Figure 16. 17 This involves creating... Point http://teachatechie.com/GJTTVBWebServices/TextToImage.asmx Descriptions (per Language) Description - en This is a Text to Image Service Instance Info - to a tModel 3 Figure 16. 18 The Service properties must be set up last 699 700 Chapter 16 Lab 16. 1: Creating a Web Service In this lab, you will create a Visual Basic NET Web service that can be used to return the orders that a customer has placed... service data types and methods and transparently performs the assembly and disassembly of the SOAP messages ■ ■ The term tModel is short for technology model A tModel is typically used to provide technical information about a programmatic interface, such as a WSDL file that describes the conventions supported by an interface 705 706 Chapter 16 Review Questions 1 What are two methods that create a Web... interoperability This comes with its own set of challenges The first part of this chapter explores some methods for migrating from ASP code to ASP.NET Later, chapter examines early and late binding techniques for using COM components Finally, the chapter covers some methods for deploying ASP.NET Web applications 709 710 Chapter 17 Classroom Q & A Q: We currently have a Web application that was developed... details tables, but that code will be ignored for the purpose of demonstrating the Web service implementation 1 Right-click the Customer project, and click Set As StartUp Project 2 Right-click the CustomerList.aspx page, and click Set As Start Page 3 Open the CustomerList.aspx.vb code-behind page Creating Web Services 4 Add a public variable to the top of the CustomerList class that will expose the... operates as expected prior to implementing this service in your application 1 Right-click the Order project in the Solution Explorer and click Set As StartUp project 2 Right-click the CustomersOrders.asmx page and click Set As Start Page 3 Run the Web Application The Web service page should be displayed, as shown in Figure 16. 19 Although the page appears to be functioning, a message is displayed that states... functioning, a message is displayed that states that the namespace needs to be changed from tempura.org to a custom namespace Figure 16. 19 The Web service page This page is functioning but displays a message to change the namespace from tempura.org to a custom namespace 701 702 Chapter 16 4 Click the GetOrders hyperlink A test page will be displayed with a TextBox that allows a CustomerID to be entered Enter . defined in Figure 16. 16. Figure 16. 18 The Service properties must be set up last. Service Key: 6ef14a9 6-7 a6 5-4 1c 7-8 cf 8-5 21f97acec3b Service Binding Key: 8c19ee3a-1e5e-414 7-8 5bf-92df58d2e738 Binding http://teachatechie.com/GJTTVBWebServices/TextToImage.asmx Access. Figure 16. 16, Figure 16. 17, and Figure 16. 18 show the menu hierarchy of the required elements. Figure 16. 17 Second, the tModel properties must be set up. uuid:4d21fe73-c9d 1-4 b7b-a696-cdd30c44dde3 Name:. UDDI Services Contact Glenn Johnson 2 1 tModels 698 Chapter 16 r 430234 Ch16.qxd 7/1/03 9:07 AM Page 698 The registration items are set up first, as shown in Figure 16. 16. (Descrip- tions that contain the en label

Ngày đăng: 12/08/2014, 08:23

TỪ KHÓA LIÊN QUAN