XmlFormatExtensionAttribute 类

定义

指定服务说明格式扩展在一个或多个扩展点运行。 此类不能被继承。

C#
[System.AttributeUsage(System.AttributeTargets.Class)]
public sealed class XmlFormatExtensionAttribute : Attribute
继承
XmlFormatExtensionAttribute
属性

示例

C#
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; }
    }
}

注解

服务说明格式扩展扩展扩展了为使用 ASP.NET 创建的 XML Web 服务生成服务说明的方式。 具体而言,服务说明格式扩展会将 XML 元素添加到服务说明。 当生成 SOAP 扩展以在 XML Web 服务的客户端和服务器端运行时,这非常有用,因为有关 SOAP 扩展的信息未放在服务说明中。 如果要将 SOAP 扩展的相关信息添加到服务说明中,客户端可以解释它必须运行特定的 SOAP 扩展。 必须在客户端和服务器上同时运行的 SOAP 扩展的一个示例是加密 SOAP 扩展。 如果加密 SOAP 扩展仅在服务器上运行,并在将返回值发送回客户端之前对其进行加密,则客户端必须运行 SOAP 扩展才能解密 SOAP 消息。 否则,客户端无法处理返回值。

使用以下步骤生成服务说明格式扩展:

  1. 生成派生自 ServiceDescriptionFormatExtension的类。

  2. XmlFormatExtensionAttribute将 应用于 类,并指定服务说明格式扩展应在其上运行的扩展点。

  3. (可选)将 应用于 XmlFormatExtensionPointAttribute 类,并在 类中指定充当新扩展点的成员。

  4. (可选)将 应用于 XmlFormatExtensionPrefixAttribute 类,并指定要与服务说明格式扩展生成的 XML 元素关联的 XML 命名空间前缀。

  5. 将服务说明格式扩展配置为在 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)

将一组名称映射为对应的一组调度标识符。

(继承自 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。

(继承自 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。

(继承自 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。

(继承自 Attribute)

适用于

产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0

另请参阅