Wcf/web Service Code

Source: Internet
Author: User
Tags reflector
Using System;
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 this YML SOAP//extension run on a per-method basis. 

The Disabled property//Turns reversing the XML in and off. [AttributeUsage (AttributeTargets.Method, Allowmultiple=false)] public class Ymlattribute:soapextensionattribute {in
  T 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, Soapextensiona
    Ttribute attribute) {Ymlattribute attr = attribute as Ymlattribute; if (attr!= null) return attr.
       Disabled;
  return false;
  Override Object GetInitializer (Type servicetype) {return false; override void Initialize (object initializer) {if (initializer is Boolean) disabled = (BOOL) Initialize
  R Override Stream ChainStream (stream stream) {if (disabled) return base.
     ChainStream (stream);
     Oldstream = stream;
     Newstream = new MemoryStream ();
  return newstream;
    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;
It is//called during the service description generation process. public class Ymlreflector:soapextensionreflector {public override void ReflectMethod () {ProtocolReflector re
    Flector = Reflectioncontext; Ymlattribute attr = (ymlattribute) reflector.
    Method.getcustomattribute (typeof (Ymlattribute));  If the Ymlattribute has been applied to this WEB service//method, adds 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;  It is called at//a proxy class is generated for each Web service method the proxy//class communicates with. The class checks whether the service//description contains The XML this is SDFE adds to a service//description.
If it exists, then the ymlextension is applied to the//method in the proxy class. 
                                    public class Ymlimporter:soapextensionimporter {public override void ImportMethod (codeattributedeclarationcollection
   Metadata) {SoapProtocolImporter importer = ImportContext;
   Checks whether the XML specified in the ymloperationbinding are in//the service description. Ymloperationbinding yml = (ymloperationbinding) importer.
   OperationBinding.Extensions.Find (typeof (Ymloperationbinding));
     if (yml!= null) {//only applies the ymlattribute to the ' method ' when the XML should//is reversed. if (yml. Reverse) {codeattributedeclaration attr = new CodeAttributeDeclaration (typeof).
       FullName); attr.
       Arguments.add (New CodeAttributeArgument (New CodePrimitiveExpression (true)); Metadata.
     ADD (attr);
 }  }}//The Ymloperationbinding class is part of the YML SDFE;
It is the//class This is serialized to XML and 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;}
 }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.