แก้ไข

แชร์ผ่าน


SoapExtension.GetInitializer Method

Definition

When overridden in a derived class, allows a SOAP extension to initialize data specific to an XML Web service method at a one time performance cost.

Overloads

GetInitializer(Type)

When overridden in a derived class, allows a SOAP extension to initialize data specific to a class implementing an XML Web service at a one time performance cost.

GetInitializer(LogicalMethodInfo, SoapExtensionAttribute)

When overridden in a derived class, allows a SOAP extension to initialize data specific to an XML Web service method using an attribute applied to the XML Web service method at a one time performance cost.

GetInitializer(Type)

When overridden in a derived class, allows a SOAP extension to initialize data specific to a class implementing an XML Web service at a one time performance cost.

public:
 abstract System::Object ^ GetInitializer(Type ^ serviceType);
public abstract object GetInitializer (Type serviceType);
abstract member GetInitializer : Type -> obj
Public MustOverride Function GetInitializer (serviceType As Type) As Object

Parameters

serviceType
Type

The type of the class implementing the XML Web service to which the SOAP extension is applied.

Returns

The Object that the SOAP extension initializes for caching.

Examples

The following code demonstrates how one can save SOAP extension specific data on a per XML Web service basis. If the SOAP extension is configured using a configuration file instead of an attribute, the SOAP extension can store data for each class to which the SOAP extension is applied. This example saves the name of a file in which to log the SOAP messages sent to and from the XML Web service method based on the name of the class implementing the XML Web service into the cache. This code example is part of a full code example for a TraceExtension SOAP extension that can be found in the SoapExtension class overview.

   // The extension was configured to run using a configuration file instead of an attribute applied to a 
   // specific XML Web service method. Return a file name based on the class implementing the XML Web service's type.
public:
   virtual Object^ GetInitializer( Type^ WebServiceType ) override
   {
      // Return a file name to log the trace information to based on the passed in type.
      return String::Format( "C:\\{0}.log", WebServiceType->FullName );
   }
// The extension was configured to run using a configuration file instead of an attribute applied to a
// specific XML Web service method. Return a file name based on the class implementing the XML Web service's type.
public override object GetInitializer(Type WebServiceType)
{
   // Return a file name to log the trace information to based on the passed in type.
   return "C:\\" + WebServiceType.FullName + ".log";
}
' The extension was configured to run using a configuration file instead of an attribute applied to a 
' specific XML Web service method.  Return a file name based on the class implementing the XML Web service's type.
Public Overloads Overrides Function GetInitializer(WebServiceType As Type) As Object
   ' Return a file name to log the trace information to based on the passed in type.
    Return "C:\" + WebServiceType.FullName + ".log"
End Function

Remarks

The overload of GetInitializer that gets called by ASP.NET depends on how the SOAP extension was specified. There are two methods for specifying a SOAP extension:

  • Apply a custom attribute, deriving from SoapExtensionAttribute, to the individual XML Web service method.

  • Add a reference in either the web.config or app.config configuration files.

If you add a reference to one of the configuration files, the SOAP extension runs for all XML Web services within the scope of that configuration file. When specifying, a SOAP extension by referencing a configuration file, ASP.NET invokes the GetInitializer overload that passes in a Type. When specifying an extension by applying a custom attribute, ASP.NET invokes the GetInitializer that passes in a LogicalMethodInfo and a SoapExtensionAttribute.

For details on adding SOAP extensions to a configuration file, see Configuration Options for XML Web Services Created Using ASP.NET.

Applies to

GetInitializer(LogicalMethodInfo, SoapExtensionAttribute)

When overridden in a derived class, allows a SOAP extension to initialize data specific to an XML Web service method using an attribute applied to the XML Web service method at a one time performance cost.

public:
 abstract System::Object ^ GetInitializer(System::Web::Services::Protocols::LogicalMethodInfo ^ methodInfo, System::Web::Services::Protocols::SoapExtensionAttribute ^ attribute);
public abstract object GetInitializer (System.Web.Services.Protocols.LogicalMethodInfo methodInfo, System.Web.Services.Protocols.SoapExtensionAttribute attribute);
abstract member GetInitializer : System.Web.Services.Protocols.LogicalMethodInfo * System.Web.Services.Protocols.SoapExtensionAttribute -> obj
Public MustOverride Function GetInitializer (methodInfo As LogicalMethodInfo, attribute As SoapExtensionAttribute) As Object

Parameters

methodInfo
LogicalMethodInfo

A LogicalMethodInfo representing the specific function prototype for the XML Web service method to which the SOAP extension is applied.

attribute
SoapExtensionAttribute

The SoapExtensionAttribute applied to the XML Web service method.

Returns

The Object that the SOAP extension initializes for caching.

Examples

The following code demonstrates how you can obtain SOAP extension-specific data passed in using a class that derives from SoapExtensionAttribute, and then cache that data in GetInitializer. This code example is part of a full code example for a TraceExtension SOAP extension that can be found in the SoapExtension class overview. This code example relies on a TraceExtensionAttribute being passed into the attribute parameter. In the full code example, TraceExtensionAttribute derives from SoapExtensionAttribute and adds a Filename property, which is what GetInitializer stores in the cache.

public:
   // When the SOAP extension is accessed for the first time, cache the 
   // file name passed in by the SoapExtensionAttribute.    
   virtual Object^ GetInitializer( LogicalMethodInfo^ /*methodInfo*/, SoapExtensionAttribute^ attribute ) override
   {
      return (dynamic_cast<TraceExtensionAttribute^>(attribute))->Filename;
   }
// When the SOAP extension is accessed for the first time, cache the
// file name passed in by the SoapExtensionAttribute.
public override object GetInitializer(LogicalMethodInfo methodInfo,
    SoapExtensionAttribute attribute)
{
    return ((TraceExtensionAttribute) attribute).Filename;
}
' When the SOAP extension is accessed for the first time, 
' cache the file name passed in by the SoapExtensionAttribute.

Public Overloads Overrides Function GetInitializer( _
    methodInfo As LogicalMethodInfo, _
    attribute As SoapExtensionAttribute) As Object    
    Return CType(attribute, TraceExtensionAttribute).Filename
End Function

Remarks

If the SOAP extension is configured using a configuration file see the GetInitializer overload that accepts a Type.

A SOAP extension has three opportunities to initialize data and they all have different purposes:

  • Class constructor - The class constructor is called every time a SOAP extension is instantiated and is typically used to initialize member variables.

  • GetInitializer - GetInitializer, however, is called just once, the first time a SOAP request is made to an XML Web services method. If a custom attribute is applied to the XML Web service method, the GetInitializer method is invoked. This allows the SOAP extension to interrogate the LogicalMethodInfo of an XML Web service method for prototype information or to access extension-specific data passed by a class deriving from SoapExtensionAttribute. The return value is cached by ASP.NET and passed into subsequent Initialize methods. Therefore, initialization done in GetInitializer is encapsulated essentially into a one-time performance hit.

  • Initialize - Initialize is called every time a SOAP request is made to an XML Web service method, but has an advantage over the class constructor, in that the Object initialized in GetInitializer is passed to it.

See also

Applies to