Export (0) Print
Expand All

XmlFormatExtensionPrefixAttribute Class

Specifies the XML namespace and XML namespace prefix to use for a service description format extension within a service description. This class cannot be inherited.

Namespace: System.Web.Services.Configuration
Assembly: System.Web.Services (in system.web.services.dll)

[AttributeUsageAttribute(AttributeTargets.Class, AllowMultiple=true)] 
public sealed class XmlFormatExtensionPrefixAttribute : Attribute
/** @attribute AttributeUsageAttribute(AttributeTargets.Class, AllowMultiple=true) */ 
public final class XmlFormatExtensionPrefixAttribute extends Attribute
AttributeUsageAttribute(AttributeTargets.Class, AllowMultiple=true) 
public final class XmlFormatExtensionPrefixAttribute extends Attribute
Not applicable.

A service description format extension extends how a service description is generated for an XML Web service created using ASP.NET. Specifically, a service description format extension adds XML elements to the service description. This is useful when a SOAP extension is built to run on both the client and server side of an XML Web service, as information about SOAP extensions is not placed in the service description. If you add information about the SOAP extension to the service description, a client can interpret that it must run the specific SOAP extension. An example of a SOAP extension that must run on both the client and server is an encryption SOAP extension. If an encryption SOAP extension runs on only the server, and it encrypts return values before sending them back to the client, the client must have the SOAP extension running to decrypt the SOAP message. Otherwise, the client cannot process the return value.

Use the following steps to build a service description format extension:

  1. Build a class that derives from ServiceDescriptionFormatExtension.

  2. Apply an XmlFormatExtensionAttribute to the class and specify the extension points at which the service description format extension should run.

  3. Optionally, apply an XmlFormatExtensionPointAttribute to the class and specify a member within the class that acts as a new extension point.

  4. Optionally, apply an XmlFormatExtensionPrefixAttribute to the class and specify the XML namespace prefix to be associated with XML elements generated by the service description format extension.

  5. Configure the service description format extension to run within the serviceDescriptionFormatExtensionTypes section of the configuration file.

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

import System.*;
import System.CodeDom.*;
import System.IO.*;
import System.Text.*;
import System.Web.Services.Configuration.*;
import System.Web.Services.Description.*;
import System.Web.Services.Protocols.*;
import 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. 
/** @attribute AttributeUsage(AttributeTargets.Method, AllowMultiple = false)
 */
public class YMLAttribute extends SoapExtensionAttribute
{
    private int priority = 0;
    private boolean disabled = false;

    public YMLAttribute()
    {
      this(false);
    } //YMLAttribute

    public YMLAttribute(boolean disabled)
    {
        this.disabled = disabled;
    } //YMLAttribute

    /** @property 
     */
    public Type get_ExtensionType()
    {
        return YMLExtension.class.ToType();
    } //get_ExtensionType

    /** @property 
     */
    public int get_Priority()
    {
        return priority;
    } //get_Priority

    /** @property 
     */
    public void set_Priority(int value)
    {
        priority = value;
    } //set_Priority

    /** @property 
     */
    public boolean get_Disabled()
    {
        return disabled;
    } //get_Disabled

    /** @property 
     */
    public void set_Disabled(boolean value)
    {
        disabled = value;
    } //set_Disabled
} //YMLAttribute

public class YMLExtension extends SoapExtension
{
    private boolean disabled = false;
    private Stream oldStream;
    private Stream newStream;

    public Object GetInitializer(LogicalMethodInfo methodInfo, 
        SoapExtensionAttribute attribute)
    {
        YMLAttribute attr =(YMLAttribute)attribute;
        if (attr != null) {
            return (System.Boolean)attr.get_Disabled();
        }
        return (System.Boolean)false;
    } //GetInitializer

    public Object GetInitializer(Type serviceType)
    {
        return (System.Boolean)false;
    } //GetInitializer

    public void Initialize(Object initializer)
    {
        if (initializer instanceof Boolean) {
            disabled = System.Convert.ToBoolean(initializer);
        }
    } //Initialize

    public Stream ChainStream(Stream stream)
    {
        if (disabled) {
            return super.ChainStream(stream);
        }
        oldStream = stream;
        newStream = new MemoryStream();
        return newStream;
    } //ChainStream

    public void ProcessMessage(SoapMessage message) throws Exception
    {
        if (disabled) {
            return;
        }
        switch (message.get_Stage()) {
            case SoapMessageStage.BeforeSerialize:
                Encode(message);
                break;

            case SoapMessageStage.AfterSerialize:
                newStream.set_Position(0);
                Reverse(newStream, oldStream);
                break;

            case SoapMessageStage.BeforeDeserialize:
                Decode(message);
                break;

            case SoapMessageStage.AfterDeserialize:
                break;
        }
    } //ProcessMessage

    void Encode(SoapMessage message)
    {
        message.set_ContentType("text/yml");
    } //Encode

    void Decode(SoapMessage message) throws Exception
    {
        if (!(message.get_ContentType()).Equals("text/yml")) {
            throw new Exception("invalid content type:" 
                + message.get_ContentType());
        }
        Reverse(oldStream, newStream);
        newStream.set_Position(0);
        message.set_ContentType("text/xml");
    } //Decode

    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.get_Length() - 1; i >= 0; i--) {
                builder.Append(line.GetEnumerator().get_Current());
            }
            writer.WriteLine(builder.ToString());
        }
        writer.Flush();
    } //Reverse
} //YMLExtension

// The YMLReflector class is part of the YML SDFE, as it is
// called during the service description generation process.
/** @attribute System.Security.Permissions.PermissionSet(System.Security.
    Permissions.SecurityAction.Demand, Name = "FullTrust")
 */
public class YMLReflector extends SoapExtensionReflector
{
    public void ReflectMethod()
    {
        ProtocolReflector reflector = get_ReflectionContext();
        YMLAttribute attr = (YMLAttribute)(reflector.get_Method().
            GetCustomAttribute(YMLAttribute.class.ToType()));
        // 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.set_Reverse(new Boolean(!(attr.get_Disabled())));
            //yml.set_Reverse( new Boolean(!(attr.get_Disabled())));
            reflector.get_OperationBinding().get_Extensions().Add(yml);
        }
    } //ReflectMethod
} //YMLReflector

// 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.
/** @attribute System.Security.Permissions.PermissionSet(System.Security.
    Permissions.SecurityAction.Demand, Name = "FullTrust")
 */
public class YMLImporter extends SoapExtensionImporter
{
    public void ImportMethod(CodeAttributeDeclarationCollection metadata)
    {
        SoapProtocolImporter importer = get_ImportContext();
        // Check whether the XML specified in the YMLOperationBinding 
        // is in the service description.
        YMLOperationBinding yml = (YMLOperationBinding)(importer.
            get_OperationBinding().get_Extensions().
            Find(YMLOperationBinding.class.ToType()));
        if (yml != null) {
            // Only apply the YMLAttribute to the method when the XML should
            // be reversed.
            if (System.Boolean.Parse(yml.get_Reverse().ToString())) {
                CodeAttributeDeclaration attr 
                    = new CodeAttributeDeclaration(YMLAttribute.class.ToType().
                    get_FullName());

                attr.get_Arguments().Add(new CodeAttributeArgument(new                                         CodePrimitiveExpression((System.Boolean)true)));
                metadata.Add(attr);
            }
        }
    } //ImportMethod
} //YMLImporter

// 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.
/** @attribute XmlFormatExtension("action", YMLOperationBinding.YMLNamespace, 
    OperationBinding.class)
 */
/** @attribute XmlFormatExtensionPrefix("yml", YMLOperationBinding.YMLNamespace)
 */
public class YMLOperationBinding extends ServiceDescriptionFormatExtension
{
    private Boolean reverse;
    public final static String YMLNamespace = "http://www.contoso.com/yml";

    /** @attribute XmlElement("Reverse")
     */
    /** @property 
     */
    public Boolean get_Reverse()
    {
        return reverse;
    } //get_Reverse

    /** @property 
     */
    public void set_Reverse(Boolean value)
    {
        reverse = value;
    } //set_Reverse
} //YMLOperationBinding

System.Object
   System.Attribute
    System.Web.Services.Configuration.XmlFormatExtensionPrefixAttribute

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft