⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 compressionlibrary.cs

📁 Microsoft Mobile Development Handbook的代码,有C#,VB,C++的
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Web.Services.Protocols;
using ICSharpCode.SharpZipLib.GZip;

namespace CompressionSOAPExtensionLibrary
{
    /// <summary>
    /// Define a class for the CompressionsSoapExtension attribute
    /// </summary>
    [AttributeUsage(AttributeTargets.Method)]
    public class CompressionSoapExtensionAttribute : SoapExtensionAttribute
    {
        private int priority;

        /// <summary>
        /// Returns the type of the object that actually performs the logic associated
        /// with this SOAP extension
        /// </summary>
        public override Type ExtensionType
        {
            get { return typeof(CompressionSoapExtension); }
        }

        /// <summary>
        /// The Priority property indicates the order of processing when there are 
        /// several SOAP extensions applied simultaneously
        /// </summary>
        public override int Priority
        {
            get
            {
                return priority;
            }
            set
            {
                priority = value;
            }
        }
    }

    public class CompressionSoapExtension : SoapExtension
    {
        Stream oldStream;
        Stream newStream;

        public override Stream ChainStream(Stream stream)
        {
            oldStream = stream;
            newStream = new MemoryStream();
            return newStream;
        }

        public override object GetInitializer(LogicalMethodInfo methodInfo, SoapExtensionAttribute attribute)
        {
            return attribute;
        }

        public override object GetInitializer(Type serviceType)
        {
            return typeof(CompressionSoapExtension);
        }

        public override void Initialize(object initializer)
        {
            CompressionSoapExtensionAttribute attribute =
                (CompressionSoapExtensionAttribute)initializer;
        }

        public override void ProcessMessage(SoapMessage message)
        {
            Byte[] buffer = new Byte[2048];
            int size;

            switch (message.Stage)
            {
                case SoapMessageStage.AfterSerialize:
                    // This is called after a SOAP message is serialized, but before it goes over the wire
                    newStream.Seek(0, SeekOrigin.Begin);
                    GZipOutputStream zipOutputStream = new GZipOutputStream(oldStream);
                    size = 2048;
                    while (true)
                    {
                        size = newStream.Read(buffer, 0, buffer.Length);
                        if (size > 0)
                            zipOutputStream.Write(buffer, 0, size);
                        else
                            break;
                    }
                    zipOutputStream.Flush();
                    zipOutputStream.Close();
                    break;

                case SoapMessageStage.BeforeDeserialize:
                    // This is called when the incoming message has been received
                    GZipInputStream zipInputStream = new GZipInputStream(oldStream);
                    size = 2048;
                    while (true)
                    {
                        size = zipInputStream.Read(buffer, 0, buffer.Length);
                        if (size > 0)
                            newStream.Write(buffer, 0, size);
                        else
                            break;
                    }
                    newStream.Flush();
                    newStream.Seek(0, SeekOrigin.Begin);
                    break;
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -