XmlFormatExtensionAttribute 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
指定服務描述格式延伸在一或多個延伸點上執行。 此類別無法獲得繼承。
public ref class XmlFormatExtensionAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Class)]
public sealed class XmlFormatExtensionAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Class, Inherited=true)]
public sealed class XmlFormatExtensionAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Class)>]
type XmlFormatExtensionAttribute = class
inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Class, Inherited=true)>]
type XmlFormatExtensionAttribute = class
inherit Attribute
Public NotInheritable Class XmlFormatExtensionAttribute
Inherits Attribute
- 繼承
- 屬性
範例
using System;
using System.Security.Permissions;
using System.CodeDom;
using System.IO;
using System.Text;
using System.Web.Services.Configuration;
using System.Web.Services.Description;
using System.Web.Services.Protocols;
using System.Xml.Serialization;
// The YMLAttribute allows a developer to specify that the YML SOAP
// extension run on a per-method basis. The Disabled property
// turns reversing the XML on and off.
[AttributeUsage(AttributeTargets.Method, AllowMultiple=false)]
public class YMLAttribute : SoapExtensionAttribute {
int priority = 0;
bool disabled = false;
public YMLAttribute() : this(false) {}
public YMLAttribute(bool disabled)
{
this.disabled = disabled;
}
public override Type ExtensionType
{
get { return typeof(YMLExtension); }
}
public override int Priority
{
get { return priority; }
set { priority = value; }
}
public bool Disabled
{
get { return disabled; }
set { disabled = value; }
}
}
public class YMLExtension : SoapExtension {
bool disabled = false;
Stream oldStream;
Stream newStream;
public override object GetInitializer(LogicalMethodInfo methodInfo,
SoapExtensionAttribute attribute)
{
YMLAttribute attr = attribute as YMLAttribute;
if (attr != null) return attr.Disabled;
return false;
}
public override object GetInitializer(Type serviceType)
{
return false;
}
public override void Initialize(object initializer)
{
if (initializer is Boolean) disabled = (bool)initializer;
}
public override Stream ChainStream(Stream stream)
{
if (disabled) return base.ChainStream(stream);
oldStream = stream;
newStream = new MemoryStream();
return newStream;
}
public override void ProcessMessage(SoapMessage message)
{
if (disabled) return;
switch (message.Stage)
{
case SoapMessageStage.BeforeSerialize:
Encode(message);
break;
case SoapMessageStage.AfterSerialize:
newStream.Position = 0;
Reverse(newStream, oldStream);
break;
case SoapMessageStage.BeforeDeserialize:
Decode(message);
break;
case SoapMessageStage.AfterDeserialize:
break;
}
}
void Encode(SoapMessage message)
{
message.ContentType = "text/yml";
}
void Decode(SoapMessage message)
{
if (message.ContentType != "text/yml")
throw new Exception(
"invalid content type:" + message.ContentType);
Reverse(oldStream, newStream);
newStream.Position = 0;
message.ContentType = "text/xml";
}
void Reverse(Stream from, Stream to)
{
TextReader reader = new StreamReader(from);
TextWriter writer = new StreamWriter(to);
string line;
while ((line = reader.ReadLine()) != null)
{
StringBuilder builder = new StringBuilder();
for (int i = line.Length - 1; i >= 0; i--)
{
builder.Append(line[i]);
}
writer.WriteLine(builder.ToString());
}
writer.Flush();
}
}
// The YMLReflector class is part of the YML SDFE, as it is
// called during the service description generation process.
[PermissionSet(SecurityAction.Demand, Name="FullTrust")]
public class YMLReflector : SoapExtensionReflector
{
public override void ReflectMethod()
{
ProtocolReflector reflector = ReflectionContext;
YMLAttribute attr =
(YMLAttribute)reflector.Method.GetCustomAttribute(
typeof(YMLAttribute));
// If the YMLAttribute has been applied to this XML Web service
// method, add the XML defined in the YMLOperationBinding class.
if (attr != null)
{
YMLOperationBinding yml = new YMLOperationBinding();
yml.Reverse = !(attr.Disabled);
reflector.OperationBinding.Extensions.Add(yml);
}
}
}
// The YMLImporter class is part of the YML SDFE, as it is called when a
// proxy class is generated for each XML Web service method the proxy class
// communicates with. The class checks whether the service description
// contains the XML that this SDFE adds to a service description. If it
// exists, then the YMLExtension is applied to the method in the proxy class.
[PermissionSet(SecurityAction.Demand, Name="FullTrust")]
public class YMLImporter : SoapExtensionImporter
{
public override void ImportMethod(
CodeAttributeDeclarationCollection metadata)
{
SoapProtocolImporter importer = ImportContext;
// Check whether the XML specified in the YMLOperationBinding
// is in the service description.
YMLOperationBinding yml =
(YMLOperationBinding)importer.OperationBinding.Extensions.Find(
typeof(YMLOperationBinding));
if (yml != null)
{
// Only apply the YMLAttribute to the method when the XML should
// be reversed.
if (yml.Reverse)
{
CodeAttributeDeclaration attr =
new CodeAttributeDeclaration(typeof(YMLAttribute).FullName);
attr.Arguments.Add(
new CodeAttributeArgument(new CodePrimitiveExpression(true)));
metadata.Add(attr);
}
}
}
}
// The YMLOperationBinding class is part of the YML SDFE, as it is the
// class that is serialized into XML and is placed in the service
// description.
[XmlFormatExtension("action", YMLOperationBinding.YMLNamespace,
typeof(OperationBinding))]
[XmlFormatExtensionPrefix("yml", YMLOperationBinding.YMLNamespace)]
public class YMLOperationBinding : ServiceDescriptionFormatExtension
{
private Boolean reverse;
public const string YMLNamespace = "http://www.contoso.com/yml";
[XmlElement("Reverse")]
public Boolean Reverse
{
get { return reverse; }
set { reverse = value; }
}
}
Imports System.Security.Permissions
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.IO
Imports System.Text
Imports System.Web.Services.Configuration
Imports System.Web.Services.Description
Imports System.Xml.Serialization
Imports System.CodeDom
' The YMLAttribute allows a developer to specify that the YML SOAP
' extension run on a per-method basis. The disabled property
' turns reversing the XML on and off.
<AttributeUsage(AttributeTargets.Method, AllowMultiple:=False)> _
Public Class YMLAttribute
Inherits SoapExtensionAttribute
Dim _priority As Integer = 0
Dim _disabled As Boolean = False
Public Sub New()
Me.New(False)
End Sub
Public Sub New(ByVal Disabled As Boolean)
_disabled = Disabled
End Sub
Public Overrides ReadOnly Property ExtensionType() As Type
Get
Return GetType(YMLExtension)
End Get
End Property
Public Overrides Property Priority() As Integer
Get
Return _priority
End Get
Set(ByVal Value As Integer)
_priority = Value
End Set
End Property
Public Property Disabled() As Boolean
Get
Return _disabled
End Get
Set(ByVal Value As Boolean)
_disabled = Value
End Set
End Property
End Class
Public Class YMLExtension
Inherits SoapExtension
Dim _disabled As Boolean = False
Dim oldStream As Stream
Dim newStream As Stream
Public Overloads Overrides Function GetInitializer( _
ByVal methodInfo As LogicalMethodInfo, _
ByVal attribute As SoapExtensionAttribute) As Object
Dim attr As YMLAttribute = attribute
If (Not attr Is Nothing) Then
Return attr.Disabled
End If
Return False
End Function
Public Overloads Overrides Function GetInitializer( _
ByVal WebServiceType As Type) As Object
Return False
End Function
Public Overrides Sub Initialize(ByVal initializer As Object)
If (TypeOf initializer Is Boolean) Then
_disabled = CBool(initializer)
End If
End Sub
Public Overrides Function ChainStream(ByVal streamref As Stream) As Stream
If (_disabled) Then
Return CType(Me, SoapExtension).ChainStream(streamref)
End If
oldStream = streamref
newStream = New MemoryStream()
Return newStream
End Function
Public Overrides Sub ProcessMessage(ByVal message As SoapMessage)
If (_disabled) Then Return
Select Case (message.Stage)
Case SoapMessageStage.BeforeSerialize
Encode(message)
Case SoapMessageStage.AfterSerialize
newStream.Position = 0
Reverse(newStream, oldStream)
Case SoapMessageStage.BeforeDeserialize
Decode(message)
Case SoapMessageStage.AfterDeserialize
End Select
End Sub
Sub Encode(ByRef message As SoapMessage)
message.ContentType = "text/yml"
End Sub
Sub Decode(ByVal message As SoapMessage)
If (message.ContentType <> "text/yml") Then
Throw New Exception("invalid content type:" & message.ContentType)
End If
Reverse(oldStream, newStream)
newStream.Position = 0
message.ContentType = "text/xml"
End Sub
Sub Reverse(ByVal source As Stream, ByVal dest As Stream)
Dim reader As TextReader = New StreamReader(source)
Dim writer As TextWriter = New StreamWriter(dest)
Dim line As String
line = reader.ReadLine()
While (Not line Is Nothing)
writer.WriteLine(StrReverse(line))
line = reader.ReadLine()
End While
writer.Flush()
End Sub
End Class
' The YMLReflector class is part of the YML SDFE, as it is
' called during the service description generation process.
<PermissionSet(SecurityAction.Demand, Name:="FullTrust")> _
Public Class YMLReflector
Inherits SoapExtensionReflector
Public Overrides Sub ReflectMethod()
Dim reflector As ProtocolReflector = ReflectionContext
Dim attr As YMLAttribute = _
reflector.Method.GetCustomAttribute(GetType(YMLAttribute))
' If the YMLAttribute has been applied to this XML Web service
' method, add the XML defined in the YMLOperationBinding class.
If (Not attr Is Nothing) Then
Dim yml As YMLOperationBinding = New YMLOperationBinding()
yml.Reverse = Not attr.Disabled
reflector.OperationBinding.Extensions.Add(yml)
End If
End Sub
End Class
' The YMLImporter class is part of the YML SDFE, as it is called when a
' proxy class is generated for each XML Web service method the proxy class
' communicates with. The class checks whether the service description
' contains the XML that this SDFE adds to a service description. If it
' exists, then the YMLExtension is applied to the method in the proxy class.
<PermissionSet(SecurityAction.Demand, Name:="FullTrust")> _
Public Class YMLImporter
Inherits SoapExtensionImporter
Public Overrides Sub ImportMethod( _
ByVal metadata As CodeAttributeDeclarationCollection)
Dim importer As SoapProtocolImporter = ImportContext
' Check whether the XML specified in the YMLOperationBinding is
' in the service description.
Dim yml As YMLOperationBinding = _
importer.OperationBinding.Extensions.Find( _
GetType(YMLOperationBinding))
If (Not yml Is Nothing) Then
' Only apply the YMLAttribute to the method when the XML
' should be reversed.
If (yml.Reverse) Then
Dim attr As CodeAttributeDeclaration = _
New CodeAttributeDeclaration(GetType(YMLAttribute).FullName)
attr.Arguments.Add( _
New CodeAttributeArgument(New CodePrimitiveExpression(True)))
metadata.Add(attr)
End If
End If
End Sub
End Class
' The YMLOperationBinding class is part of the YML SDFE, as it is the
' class that is serialized into XML and is placed in the service
' description.
<XmlFormatExtension("action", YMLOperationBinding.YMLNamespace, _
GetType(OperationBinding)), _
XmlFormatExtensionPrefix("yml", YMLOperationBinding.YMLNamespace)> _
Public Class YMLOperationBinding
Inherits ServiceDescriptionFormatExtension
Private _reverse As Boolean
Public Const YMLNamespace As String = "http://www.contoso.com/yml"
<XmlElement("Reverse")> _
Public Property Reverse() As Boolean
Get
Return _reverse
End Get
Set(ByVal Value As Boolean)
_reverse = Value
End Set
End Property
End Class
備註
服務描述格式延伸模組會擴充如何使用 ASP.NET 建立的 XML Web 服務產生服務描述。 具體而言,服務描述格式延伸模組會將 XML 元素新增至服務描述。 這在建置 SOAP 擴充功能以在 XML Web 服務的用戶端和伺服器端上執行時很有用,因為 SOAP 延伸模組的相關資訊不會放在服務描述中。 如果您想要將 SOAP 延伸模組的相關資訊新增至服務描述,用戶端可以解譯它必須執行特定的 SOAP 擴充功能。 必須同時在用戶端和伺服器上執行的 SOAP 擴充功能例子,就是加密 SOAP 擴充功能。 如果加密 SOAP 擴充功能只在伺服器上執行,而且它會在將值傳回給用戶端之前加密,用戶端必須執行 SOAP 延伸模組才能解密 SOAP 訊息。 否則,用戶端無法處理傳回值。
使用下列步驟來建置服務描述格式延伸模組:
建置衍生自 ServiceDescriptionFormatExtension 的類別。
XmlFormatExtensionAttribute將 套用至 類別,並指定服務描述格式延伸模組應該執行所在的擴充點。
選擇性地將 套用 XmlFormatExtensionPointAttribute 至 類別,並在類別中指定做為新擴充點的成員。
選擇性地將 套用 XmlFormatExtensionPrefixAttribute 至 類別,並指定要與服務描述格式延伸所產生的 XML 元素建立關聯的 XML 命名空間前置詞。
設定服務描述格式延伸模組,以在
serviceDescriptionFormatExtensionTypes
組態檔的 區段中執行。
建構函式
XmlFormatExtensionAttribute() |
初始化 XmlFormatExtensionAttribute 類別的新執行個體。 |
XmlFormatExtensionAttribute(String, String, Type) |
初始化 XmlFormatExtensionAttribute 類別的新執行個體,指定執行於特定延伸點時要加入的 XML 項目和命名空間。 |
XmlFormatExtensionAttribute(String, String, Type, Type) |
初始化 XmlFormatExtensionAttribute 類別的新執行個體,指定執行於特定延伸點時要加入的 XML 項目和命名空間。 |
XmlFormatExtensionAttribute(String, String, Type, Type, Type) |
初始化 XmlFormatExtensionAttribute 類別的新執行個體,指定執行於特定延伸點時要加入的 XML 項目和命名空間。 |
XmlFormatExtensionAttribute(String, String, Type, Type, Type, Type) |
初始化 XmlFormatExtensionAttribute 類別的新執行個體,指定執行於特定延伸點時要加入的 XML 項目和命名空間。 |
XmlFormatExtensionAttribute(String, String, Type[]) |
初始化 XmlFormatExtensionAttribute 類別的新執行個體,指定執行於特定延伸點時要加入的 XML 項目和命名空間。 |
屬性
ElementName |
取得或設定由服務描述格式延伸加入至服務描述的 XML 項目。 |
ExtensionPoints |
服務描述格式延伸要執行的階段。 |
Namespace |
取得或設定由服務描述格式延伸加入至服務描述之 XML 項目的 XML 命名空間。 |
TypeId |
在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。 (繼承來源 Attribute) |
方法
Equals(Object) |
傳回值,這個值指出此執行個體是否與指定的物件相等。 (繼承來源 Attribute) |
GetHashCode() |
傳回這個執行個體的雜湊碼。 (繼承來源 Attribute) |
GetType() |
取得目前執行個體的 Type。 (繼承來源 Object) |
IsDefaultAttribute() |
在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。 (繼承來源 Attribute) |
Match(Object) |
在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。 (繼承來源 Attribute) |
MemberwiseClone() |
建立目前 Object 的淺層複製。 (繼承來源 Object) |
ToString() |
傳回代表目前物件的字串。 (繼承來源 Object) |
明確介面實作
_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) |
將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。 (繼承來源 Attribute) |
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) |
擷取物件的類型資訊,可以用來取得介面的類型資訊。 (繼承來源 Attribute) |
_Attribute.GetTypeInfoCount(UInt32) |
擷取物件提供的類型資訊介面數目 (0 或 1)。 (繼承來源 Attribute) |
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
提供物件所公開的屬性和方法的存取權。 (繼承來源 Attribute) |