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 ref class XmlFormatExtensionPrefixAttribute sealed : public Attribute

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.

No code example is currently available or this language may not be supported.
#using <mscorlib.dll>
#using <System.dll>
#using <System.Web.Services.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::CodeDom;
using namespace System::IO;
using namespace System::Text;
using namespace System::Web::Services::Configuration;
using namespace System::Web::Services::Description;
using namespace System::Web::Services::Protocols;
using namespace System::Xml::Serialization;

public __gc class YMLExtension;

// 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 __gc class YMLAttribute : public SoapExtensionAttribute
    int priority;
    bool disabled;

    YMLAttribute() : disabled(false) {
        priority = 0;
        disabled = false;

    YMLAttribute(bool disabled) {
        this->disabled = disabled;

    __property Type* get_ExtensionType() { return __typeof(YMLExtension); }

    __property int get_Priority() { return priority; }
    __property void set_Priority(int value) { priority = value; }

    __property bool get_Disabled() { return disabled; }
    __property void set_Disabled(bool value) { disabled = value; }


public __gc class YMLExtension : public SoapExtension {
    bool disabled;
    Stream* oldStream;
    Stream* newStream;

        disabled = false;

    Object* GetInitializer(LogicalMethodInfo* /*methodInfo*/, SoapExtensionAttribute* attribute) {
        YMLAttribute* attr = dynamic_cast<YMLAttribute*>(attribute);
        if (attr != 0) 
            return __box(attr->get_Disabled());
        return __box(false);

    Object* GetInitializer(Type* /*serviceType*/) {
        return __box(false);

    void Initialize(Object* initializer) {
        if (Boolean * pb = dynamic_cast<System::Boolean*>(initializer)) 
            disabled = *pb;

    Stream* ChainStream(Stream* stream) {
        if (disabled) return __super::ChainStream(stream);
        oldStream = stream;
        newStream = new MemoryStream();
        return newStream;

    void ProcessMessage(SoapMessage* message) {
        if (disabled) return;
        switch (message->Stage) {
            case SoapMessageStage::BeforeSerialize:
            case SoapMessageStage::AfterSerialize:
                newStream->Position = 0;
                Reverse(newStream, oldStream);
            case SoapMessageStage::BeforeDeserialize:
            case SoapMessageStage::AfterDeserialize:

    void Encode(SoapMessage* message) {
        message->ContentType = S"text/yml";

    void Decode(SoapMessage* message) {
        if (String::Compare(message->ContentType, S"text/yml") != 0) 
            throw new Exception(String::Concat(S"invalid content type: {0}",message->ContentType));
        Reverse(oldStream, newStream);
        newStream->Position = 0;
        message->ContentType = S"text/xml";

    void Reverse(Stream* from, Stream* to) {
        TextReader* reader = new StreamReader(from);
        TextWriter* writer = new StreamWriter(to);
        String* line;
        while ((line = reader->ReadLine()) != 0) {
            StringBuilder* builder = new StringBuilder();
            for (int i = line->Length - 1; i >= 0; i--) {

// The YMLOperationBinding class is part of the YML SDFE, as it is the
// class that gets serialized into XML and gets placed in the service
// description.
[XmlFormatExtension(S"action", S"YMLOperationBinding::YMLNamespace", __typeof(OperationBinding))]
[XmlFormatExtensionPrefix(S"yml", S"YMLOperationBinding::YMLNamespace")]
public __gc class YMLOperationBinding : public ServiceDescriptionFormatExtension {
    Boolean reverse;

    const String* YMLNamespace;
    YMLOperationBinding() : YMLNamespace(S"http://www.contoso.com/yml") {}

    __property Boolean get_Reverse() { return reverse; }
    __property void set_Reverse(Boolean value) { reverse = value; }

// The YMLReflector class is part of the YML SDFE, as it gets
// called during the service description generation process.
public __gc class YMLReflector : public SoapExtensionReflector {
    void ReflectMethod() {
        ProtocolReflector *reflector = ReflectionContext;
        YMLAttribute *attr = dynamic_cast<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 != 0) {
            YMLOperationBinding* yml = new YMLOperationBinding();
            yml->Reverse = !(attr->Disabled);

// The YMLImporter class is part of the YML SDFE, as it gets called when
// a proxy class is generated for each XML Web service method the proxy class
// communictes 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.
public __gc class YMLImporter : public SoapExtensionImporter {
    void ImportMethod(CodeAttributeDeclarationCollection *metadata) {
        SoapProtocolImporter *importer = ImportContext;
        // Check whether the XML specified in the YMLOperationBinding is in the
        // service description.
        YMLOperationBinding *yml = dynamic_cast<YMLOperationBinding*>(importer->OperationBinding->Extensions->Find(__typeof(YMLOperationBinding)));
        if (yml != 0) {
            // 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(__box(true))));


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 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

Community Additions

© 2014 Microsoft