Xem mẫu

Table 6-11: Selected Properties, Methods, and Events of the SoapHttpClientProtocol Class Property CookieContainer Credentials PreAuthenticate Proxy RequestEncoding Timeout Url UserAgent Method Abort Discover Event Disposed Description group to use when connecting to the Web service. A connection group provides a mechanism for allowing multiple clients within the same application to share connections open to a given Web server. Used to access the cookies maintained by the proxy. Also provides a mechanism for setting cookies for a particular domain. Specifies authentication credentials that can be used to log into the Web server. The supported methods of authentication include Basic Authentication, Windows NT Challenge/Response, Kerberos, and Digest. Specifies whether the authentication credentials should be sent immediately or as a result of receiving a 401 access denied error. Contains the information necessary to connect to the proxy server. This includes the URL, port, and user name/domain/password. Specifies the type of encoding that will be used when serializing the request message. The default is UTF-8. Specifies the period of time, in milliseconds, that a synchronous Web request has to complete before the request is aborted. The default is infinity (−1). Specifies the address of the Web service endpoint. Specifies the value of the user agent header in the HTTP request. Description Used to abort any asynchronous method calls that are currently executing. Used to dynamically discover the location of the Web service via the DISCO file referenced by the Url property. Description Used to provide notification when the proxy has been disposed. Let’s step through the code generated by WSDL.exe to see how the proxy class is implemented: //----------------------------------------------------------------------------- // // This code was generated by a tool. // Runtime Version: 1.0.xxxx.xx 192 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. // //----------------------------------------------------------------------------- // // This source code was auto generated by WSDL, Version=1.0.xxxx.xx. // WSDL.exe first generates comments that document the version of the runtime as well as the version of WSDL.exe that was used to create the proxy. If the proxy will be included within a code base that is released to production, you might also want to record the date and time that the WSDL was generated along with a copy of the WSDL document itself. The date and time can be recorded by promptly checking the file into a source code repository or by adding it as a comment to the generated file. The WSDL document can be obtained by WSDL.exe itself. You can accomplish this by using one of the optional command-line parameters I discuss later in this section. namespace BrokerageFirm { using System.Diagnostics; using System.Xml.Serialization; using System; using System.Web.Services.Protocols; using System.Web.Services; /// [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Web.Services.WebServiceBindingAttribute(Name="SecuritiesSoap ", Namespace="http://woodgrovebank.com/Securities")] [System.Xml.Serialization.SoapIncludeAttribute(typeof(SoapReceiptHea der))] [System.Xml.Serialization.SoapIncludeAttribute(typeof(SoapPaymentHea der))] public class Securities : System.Web.Services.Protocols. SoapHttpClientProtocol { public SoapPaymentHeader SoapPaymentHeaderValue; 193 public SoapReceiptHeader SoapReceiptHeaderValue; The Securities class is defined within the BrokerageFirm namespace. It is derived from the SoapHttpClientProtocol class. SoapHttpClientProtocol serves as the base class for all ASP.NET proxies and contains the implementation necessary to communicate with most HTTP-based Web services. The Securities class is also decorated with three attributes. The first is the WebServiceBinding attribute, which serves the exact same role on the client as it does on the Web service. This attribute allows you to formally reference a particular binding defined within another namespace. The two other attributes are SoapInclude attributes. They tell the XML Serializer to include the SoapPaymentHeaderValue and the SoapReceiptHeaderValue member variables within the SOAP message. /// public Securities() { string urlSetting = System.Configuration.ConfigurationSettings.AppSettings ["SecuritiesWebServiceUrl"]; if ((urlSetting != null)) { this.Url = urlSetting; } else { this.Url = "http://localhost/BrokerageFirm/Securities.asmx"; } } The constructor sets the object’s Url property to the value of the SecuritiesWebServiceUrl application configuration parameter defined in the application’s configuration file. If the value is not found, the Url property is set to the value contained within the HTTP extension element that defines the address of the endpoint within the WSDL document’s service definition. You should consider modifying the else logic to throw an exception instead of defaulting to a hard-coded value. This will make it easier to diagnose some problems within your application. For example, when you are trying to debug your application, it would be easy to overlook the fact that the SecuritiesWebServiceUri parameter is misspelled within your configuration file. (Did you catch the misspelling?) You might need to dynamically modify the Url property at run time. For example, the application might want to reissue its request to another server in the event of failure. The Url property is a publicly exposed read/write property, so it can be modified by the client at run time. You can also set the Url property to point to a DISCO file containing a reference to the targeted Web service. You can then call the Discover method to dynamically bind to the Web service contained within the DISCO file. I will cover DISCO files in more detail in Chapter 10. Within the proxy class definition, methods are defined for each of the operations exposed by the Web service. For each operation, three methods are defined. The first method definition 194 is for synchronously invoking the Web method, and the other two are used in combination to invoke the Web method asynchronously. Here is the synchronous definition for the InstantQuote method: [System.Web.Services.Protocols.SoapHeaderAttribute "SoapReceiptHeaderValue", Direction=System.Web.Services. Protocols.SoapHeaderDirection.Out)] [System.Web.Services.Protocols.SoapHeaderAttribute ("SoapPaymentHeaderValue")] /// [System.Web.Services.Protocols.SoapRpcMethodAttribute ("http://woodgrovebank.com/Securities/InstantQuote", RequestNamespace="http://woodgrovebank.com/Securities", ResponseNamespace="http://woodgrovebank.com/Securities")] public System.Double InstantQuote(string symbol, CurrencyType targetCurrency) { object[] results = this.Invoke("InstantQuote", new object[] { symbol, targetCurrency}); return ((System.Double)(results[0])); } The InstantQuote method is decorated with the SoapHeader, DebuggerStepThrough, and SoapRpcMethod attributes. The DebuggerStepThrough attribute is used by the Visual Studio .NET debugger. The Visual Studio .NET debugger will not stop within the method marked with this attribute. The SoapHeader and SoapRpcMethod attributes serve the same purpose as they do when applied to a Web method. The SoapHeader attribute indicates which member variable should be serialized into the header of the SOAP message. The SoapRpcMethod attribute indicates the encoding style and the format of the message as well as the value of the SOAP HTTPAction header. The signature of the method itself is composed of .NET types that match their XML counterparts described within the types section of the WSDL document. This wrapper method definition allows code written against the proxy to take full advantage of the features provided by the .NET platform. For example, if a client attempts to pass invalid parameters, such as passing two strings to the Add method instead of two integers, the compiler will generate errors at compile time. Developers using Visual Studio .NET will also have full IntelliSense capabilities when they write code against the proxy. The implementation of the InstantQuote method packages the parameters into an array of objects and calls the Invoke method. Because this method is publicly exposed, you can call it directly. However, using the method exposed by the WSDL.exe-generated proxy provides a more convenient and natural calling convention. In many circumstances, making a synchronous call to a Web method is not ideal. This is especially true for Web services accessed via the Internet, where quality and speed of the connection might be uncertain. This might also be true for Web services hosted within the walls of a corporate data center. For example, a Web service might be used to expose data 195 contained within a mainframe. A significant amount of initialization might need to be done to set up a connection to the mainframe, or the Web service might be accessed during times of peak load. The next two methods defined for the InstantQuote operation are BeginInstantQuote and EndInstantQuote. These methods are used to make an asynchronous call to the Securities Web service’s InstantQuote Web method: /// public System.IAsyncResult BeginInstantQuote(string symbol, CurrencyType targetCurrency, System.AsyncCallback callback, object asyncState) { return this.BeginInvoke("InstantQuote", new object[] {symbol, targetCurrency}, callback, asyncState); } /// public System.Double EndInstantQuote(System.IAsyncResult asyncResult) { object[] results = this.EndInvoke(asyncResult); return ((System.Double)(results[0])); } } By convention, the method used to invoke the asynchronous call is prefixed with Begin and the method used to retrieve the parameters returned by the Web service is prefixed with End. The implementation invokes the BeginInvoke and EndInvoke methods, respectively. The asynchronous methods are not decorated with attributes used to describe the formatting of the message. The methodName parameter contains the name of the method that the ASP.NET runtime will use to retrieve the formatting information. If the asynchronous message is decorated with any attributes such as SoapDocumentMethod, these attributes will be ignored. [System.Xml.Serialization.SoapTypeAttribute("SoapReceiptHeader", "http://woodgrovebank.com/Securities/encodedTypes")] public class SoapReceiptHeader : SoapHeader { public System.Double Amount; public int ReferenceNumber; } [System.Xml.Serialization.SoapTypeAttribute("SoapPaymentHeader", "http://woodgrovebank.com/Securities/encodedTypes")] public class SoapPaymentHeader : SoapHeader { 196 ... - tailieumienphi.vn
nguon tai.lieu . vn